我有一个 sql 查询:
SELECT
TAT,
SUM(CASE WHEN [Month Entered] = 1 THEN 1 ELSE 0 END) JAN,
SUM(CASE WHEN [Month Entered] = 2 THEN 1 ELSE 0 END) FEB,
SUM(CASE WHEN [Month Entered] = 3 THEN 1 ELSE 0 END) MAR,
SUM(CASE WHEN [Month Entered] = 4 THEN 1 ELSE 0 END) APR,
SUM(CASE WHEN [Month Entered] = 5 THEN 1 ELSE 0 END) MAY,
SUM(CASE WHEN [Month Entered] = 6 THEN 1 ELSE 0 END) JUN,
SUM(CASE WHEN [Month Entered] = 7 THEN 1 ELSE 0 END) JUL,
SUM(CASE WHEN [Month Entered] = 8 THEN 1 ELSE 0 END) AUG,
SUM(CASE WHEN [Month Entered] = 9 THEN 1 ELSE 0 END) SEP,
SUM(CASE WHEN [Month Entered] = 10 THEN 1 ELSE 0 END) OCT,
SUM(CASE WHEN [Month Entered] = 11 THEN 1 ELSE 0 END) NOV,
SUM(CASE WHEN [Month Entered] = 12 THEN 1 ELSE 0 END) [DEC]
FROM
[SalesDWH].[dbo].[TurnAround]
where DATEPART(yyyy,[datetime entered])=2010
GROUP BY TAT
ORDER BY 1;
这是它返回的内容:
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| TAT | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 0 | 0 | 0 | 0 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 7 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 18 | 28 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 19 | 39 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 53 | 0 | 0 |
| 20 | 106 | 0 | 0 | 0 | 0 | 0 | 2 | 17 | 0 | 1 | 2 | 0 |
| 21 | 113 | 12 | 0 | 0 | 0 | 22 | 1 | 81 | 137 | 27 | 1 | 110 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
我希望输出为:
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| TAT | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 0 | | | | 3 | | | | | | | | |
| 1 | 1 | | | | | | | | | | | |
| 2 | | | | | | | | | | | | |
| 3 | | | | | | | | | | | | |
| 4 | | | | | | | | | | | | |
| 5 | | | | | | | | | | | | |
| 6 | | | | | | | | | | | | |
| 7 | | | | | 1 | | | | | | | |
| 8 | | | | | | | | | | | | |
| 9 | | | | | | | | | | | | |
| 10 | | | | | | | | | | | | |
| 11 | | | | | | | | | | | | |
| 12 | | | | | | | | | | | | |
| 13 | | | | | | | | | | | | |
| 14 | | | | | | | | | | | | |
| 15 | | | | | | | | | | | | |
| 16 | | | | | | | | | | | | |
| 17 | | | | | | | | | | | | |
| 18 | 28 | | | | | | | | | | | |
| 19 | 39 | | | | | 1 | | | | 53 | | |
| 20 | 106 | | | | | | 2 | 17 | | 1 | 2 | |
| 21 | 113 | 12 | | | | 22 | 1 | 81 | 137 | 27 | 1 | 110 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
请注意,所有0
' 都已被删除,并且该TAT
列是连续的 0,1,2,3,4,......
我如何强制这两个条件?
对于 0 到 191 之间的 TAT 值,我需要这个。
对于 192 和更大的值,我希望 192 处的 TAT 是 192 和更大的所有值的总和