sql SUM结果过滤器(不要为null或0)

时间:2013-03-02 08:04:42

标签: sql-server if-statement sum

我想CASE STHAR_GCKOD when 'G' THEN STHAR_GCMIK >=1 所以我不想要null或0 这可能吗 ?谢谢..

SELECT
   STOK_KODU,
   SUM(CASE STHAR_GCKOD WHEN 'G' THEN STHAR_GCMIK ELSE 0 END) // INCOMING,
   SUM(CASE STHAR_GCKOD WHEN 'C' THEN STHAR_GCMIK ELSE 0 END) // OUTGOING,
   SUM(CASE STHAR_GCKOD WHEN 'G' THEN STHAR_GCMIK ELSE -STHAR_GCMIK END) // TOTAL
FROM 
   STOKCEK
GROUP BY 
   STOK_KODU

1 个答案:

答案 0 :(得分:3)

使用having,而不是在where之前应用group by

Select STOK_KODU,
sum(CASE STHAR_GCKOD WHEN 'G' THEN STHAR_GCMIK ELSE -STHAR_GCMIK end) 
FROM STOKCEK
GROUP BY STOK_KODU
having sum(CASE STHAR_GCKOD WHEN 'G' THEN STHAR_GCMIK ELSE -STHAR_GCMIK end) > 0