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選擇計數值負的正值和零值
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選擇計數值負的正值和零值
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
你可以;
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
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
希望它能幫助!
這也是一個很好的例子! – Butters
任何努力? [你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Lion
你的要求不明確。 [但是,查看CASE表達式] –
從表組中選擇count(值爲0時的情況),count(value值爲0的情況),count(value值爲0的情況) – Butters