我的桌子有这个值:
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
第二个字段的前四行具有相同的值,后四行具有相同的值。 我意识到的查询应该将此记录返回给我:
SommaDiImporto | codice_ordine_data
3718,54 | 003644260517,003573190517,003740260517,003693230517
924,74 | 003644260520,003573190520,003740260520,003693230520
这就是我的期望:
SommaDiImporto | codice_ordine_data
3718,54 | 003644260517,003573190517,003740260517,003693230517
0 | 003644260520,003573190520,003740260520,003693230520
最后一行的第一个字段应该返回0,因为在该分组列表中至少有一个设置为0的记录。
这是我意识到的问题:
SELECT SUM(IIF(TIMBRATURE.importo = 0, 0, TIMBRATURE.importo)) AS SommaDiImporto, TIMBRATURE.codice_ordine_data
FROM TIMBRATURE
GROUP BY TIMBRATURE.codice_ordine_data;
我如何修改查询以返回所需的结果?
非常感谢!
答案 0 :(得分:1)
对于没有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;
对于具有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;
尝试这是否有效!
注意:函数ISNULL
取决于数据库。这个article可能有助于找到应该使用的确切函数。