我对查询确实有困难。
当前查询
SELECT
min(ID),
Date,
CASE WHEN (ID) = '111' then SUM(Column1) END AS [Outcome1],
CASE WHEN (ID) = '222' then SUM(Column1) END AS [Outcome2],
CASE WHEN (ID) = '333' then SUM(Column1) END AS [Outcome3],
CASE WHEN (ID) = '444' then SUM(Column1) END AS [Outcome4]
FROM Table1
GROUP BY ID, Date
结果产生
| ID | Date | Outcome1 | Outcome2 | Outcome3 | Outcome4 |
|-----|----------|----------|----------|----------|----------|
| 111 | 01/01/18 | 20 | (null) | (null) | (null) |
| 222 | 01/01/18 | (null) | 20 | (null) | (null) |
| 333 | 01/01/18 | (null) | (null) | 20 | (null) |
| 444 | 01/01/18 | (null) | (null) | (null) | 20 |
然而,我正在寻找的结果是
| ID | Date | Outcome1 | Outcome2 | Outcome3 | Outcome4 |
|-----|----------|----------|----------|----------|----------|
| 111 | 01/01/18 | 20 | 20 | 20 | 20 |
我已经尝试过分组,但最终结果只是结果1在80.但我正在寻找它与4结果列一起显示。
任何人都可以帮我吗?感谢
SQL小提琴 - http://sqlfiddle.com/#!18/9516c/2
答案 0 :(得分:4)
在SUM
声明
case
汇总
SELECT
min(ID),
Date,
SUM(CASE WHEN (ID) = '111' then Column1 END) AS [Outcome1],
SUM(CASE WHEN (ID) = '222' then Column1 END) AS [Outcome2],
SUM(CASE WHEN (ID) = '333' then Column1 END) AS [Outcome3],
SUM(CASE WHEN (ID) = '444' then Column1 END) AS [Outcome4]
FROM Table1
GROUP BY Date
同时从ID
group by