我找到了一些类似的帖子,但我并不觉得它们有用。但我不知道如何对它们进行分组。
我想将“No”和“Not Set”求和到一行,并丢弃“Not Set”行。
所以:'No‘= 'No’+ 'Not Set‘
我有这样的东西:
TEST TestCount Month
'Yes' 123 March
'No' 432 March
'Not Set' 645 March
'Yes' 13 April
'No' 42 April
'Not Set' 45 April
'Yes' 133 May
'No' 41 May
'Not Set' 35 May
....
我想要这样的东西:
TEST TestCount Month
'Yes' 423 March (Should be 123? - @Dems)
'No' 410 March (Should be 1077? - @Dems)
'Yes' 154 April (Should be 13? - @Dems)
'No' 192 April (Should be 87? - @Dems)
'Yes' 130 May (Should be 133? - @Dems)
'No' 149 May (Should be 76? - @Dems)
……
有人能帮我吗,tnx?
发布于 2012-06-07 18:31:16
SELECT CASE test WHEN 'Not Set' THEN 'No' ELSE test END AS newtest,
month, SUM(testCount)
FROM mytable
GROUP BY
newtest, month
发布于 2012-06-07 18:36:56
SQL Fiddle with Demo
SELECT CASE WHEN test = 'Not Set' THEN 'No' ELSE test END AS testvalue
, SUM(testCount) as TestCount
, month
FROM test
GROUP BY testvalue, month
发布于 2012-06-07 19:11:04
SELECT TEST, TestCount, Month
FROM (
(SELECT 'No' AS TEST, SUM(TestCount) AS TestCount, Month
FROM mytable
WHERE TEST = 'No'
OR TEST = 'Not Set'
GROUP BY MONTH)
UNION
(SELECT TEST, TestCount, Month
FROM mytable
WHERE TEST = 'Yes')) AS newtable
GROUP BY Month, TEST;
https://stackoverflow.com/questions/10929997
复制相似问题