作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个查询,我正在尝试使用 SQL Server 将其合并到 1 行。它一直迫使我按 Frequency
列分组,但我只想按 UserId
分组。谁能告诉我我做错了什么。
代码:
SELECT userid, frequency,
(select count(frequency) where frequency = 1) AS Onetime,
(select count(frequency) where frequency = 2) AS Daily,
(select count(frequency) where frequency = 3) AS Weekly,
(select count(frequency) where frequency = 4) AS Fortnightly,
(select count(frequency) where frequency = 5) AS Monthly,
(select count(frequency) where frequency = 6) AS Quarterly,
(select count(frequency) where frequency = 7) AS SemiAnnual,
(select count(frequency) where frequency = 8) AS Annual
FROM AppTasks
where userid = 'userid@gmail.com'
group by UserID, Frequency
数据:
最佳答案
你能试试这个吗?
SELECT userid,
SUM(CASE WHEN frequency = 1 THEN 1 ELSE 0 END) AS Onetime,
SUM(CASE WHEN frequency = 2 THEN 1 ELSE 0 END) AS Daily,
SUM(CASE WHEN frequency = 3 THEN 1 ELSE 0 END) AS Weekly,
SUM(CASE WHEN frequency = 4 THEN 1 ELSE 0 END) AS Fortnightly,
SUM(CASE WHEN frequency = 5 THEN 1 ELSE 0 END) AS Monthly,
SUM(CASE WHEN frequency = 6 THEN 1 ELSE 0 END) AS Quarterly,
SUM(CASE WHEN frequency = 7 THEN 1 ELSE 0 END) AS SemiAnnual,
FROM AppTasks
where userid = 'userid@gmail.com'
group by UserID;
关于sql-server - 如何将结果合并到一条线上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48805487/
我是一名优秀的程序员,十分优秀!