2013-06-24 182 views
1
Select * from Table 

Date   Value 
2013-06-24  12 
2013-06-24  3 
2013-06-24  -4 
2013-06-24  33 
2013-06-25  12 
2013-06-25  -2 
2013-06-25  43 
2013-06-25  1 
2013-06-25  -3 

現在我將在一個SQL命令中計算所有負數,正數和零數值組。SQL選擇計數值負的正值和零值

+2

任何努力? [你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Lion

+0

你的要求不明確。 [但是,查看CASE表達式] –

+0

從表組中選擇count(值爲0時的情況),count(value值爲0的情況),count(value值爲0的情況) – Butters

回答

3
SELECT 
Date, 
SUM(CASE WHEN Value > 0 THEN 1 ELSE 0 END) AS pos, 
SUM(CASE WHEN Value < 0 THEN 1 ELSE 0 END) AS neg, 
SUM(CASE WHEN Value = 0 THEN 1 ELSE 0 END) AS zero 
FROM yourTable 
GROUP BY Date 
+0

謝謝! !!!!! – Butters

+0

你是最棒的! – Butters

+0

不客氣。 – fancyPants

2

你可以;

select 
    DATE, 
    SUM(case when value < 0 then 1 else 0 end) as NEGATIVE, 
    SUM(case when value > 0 then 1 else 0 end) as POSITIVE, 
    SUM(case when value = 0 then 1 else 0 end) as ZERO 
from 
    T 
group by date 
2
select date, count(case when value < 0 then value end) as Negative, 
count(case when value > 0 then value end) as positive 
from the_table 

希望它能幫助!

+0

這也是一個很好的例子! – Butters