How can I group by a field by a condition of that specific field?

Francesco Esposto

My table has this values:

SommaDiImporto | codice_ordine_data
828,13         | 003644260517,003573190517,003740260517,003693230517
804,73         | 003644260517,003573190517,003740260517,003693230517
357,86         | 003644260517,003573190517,003740260517,003693230517
1727,82        | 003644260517,003573190517,003740260517,003693230517
200,69         | 003644260520,003573190520,003740260520,003693230520
0              | 003644260520,003573190520,003740260520,003693230520
600,58         | 003644260520,003573190520,003740260520,003693230520
123,47         | 003644260520,003573190520,003740260520,003693230520

The second field has the first four rows with the same values and the last four rows with same values too. The query that I realized should return me this records:

SommaDiImporto | codice_ordine_data
3718,54        | 003644260517,003573190517,003740260517,003693230517
924,74         | 003644260520,003573190520,003740260520,003693230520

And this is what I expect:

SommaDiImporto | codice_ordine_data
3718,54        | 003644260517,003573190517,003740260517,003693230517
0              | 003644260520,003573190520,003740260520,003693230520

The first field of the last row should return 0 because in that grouped list there is at least a record setted to 0.

This is the query I realized:

SELECT SUM(IIF(TIMBRATURE.importo = 0, 0, TIMBRATURE.importo)) AS SommaDiImporto, TIMBRATURE.codice_ordine_data
FROM TIMBRATURE
GROUP BY TIMBRATURE.codice_ordine_data;

How I have to modify my query to return the desired result?

Many thanks!

Sanjay Rathod

For values without NULL:

SELECT IIF(MIN(TIMBRATURE.importo) = 0, 0, SUM(TIMBRATURE.importo)) AS SommaDiImporto, TIMBRATURE.codice_ordine_data
FROM TIMBRATURE
GROUP BY TIMBRATURE.codice_ordine_data;

For values with NULL:

SELECT IIF(MIN(ISNULL(TIMBRATURE.importo, 0)) = 0, 0, SUM(TIMBRATURE.importo)) AS SommaDiImporto, TIMBRATURE.codice_ordine_data
FROM TIMBRATURE
GROUP BY TIMBRATURE.codice_ordine_data;

Try if this works!

Note: The function ISNULL depends on the database. This article may help to find the exact function that should be used.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I update a field of a table with random numbers of specific range

How to group documents with specific field in aggregation of mongodb

How can I access a specific field of a queryset in Django?

How to group a specific field in array?

How can I remove a specific field on MongoDB using java?

How can I take the SUM of an alias field based on a condition?

How can I sum a group of a custom calculated field in SQL?

How can i check if the Look Up Field User is the Same user in the Specific Queue using Formula Field

How can I limit the result to a field with array that has a specific value?

how can I update a field with 2 possible values according to a condition?

How to group documents by a specific field in MongoDB

SQL - How can I check to see if a FIELD A has all 3 specific values in FIELD B?

How can I print a specific field with an expression using awk -F?

How can I add a WHERE condition on a Text field in a SQLite query?

How can I group by a field emitted events?

How Can I Pull In A Specific Posts Custom Field?

How to change a value on a specific field with specific condition

How can I Group By Month from a Date field

How can i update a specific field using foreign key relationship

MongoDB Group aggregation multiple field can push specific array field?

How can I group an array of objects by field values multiple times?

How can I delete a document in Firebase after sorting by a specific field?

How to group a queryset by a specific field in Django?

How can I get the data of student by searching specific field?

How can I give very specific borders on a text field in Flutter?

How can I group by same field with multiple values?

How can I get a specific field into Firestore in Flutter

how i can apply an if confition on a field of formBuilder.group Angular

How to group and sort array of data by their specific field