SQL Pivot – 将月份作为列标题

huangapple go评论105阅读模式
英文:

SQL Pivot - Months as Column headers

问题

我有一个SQL查询,如下所示:

  1. select year(recvd_date),
  2. sum(case when month(recvd_date)=1 then count(distinct(app_id)) else 0 end) as Jan,
  3. sum(case when month(recvd_date)=2 then count(distinct(app_id)) else 0 end) as Feb,
  4. sum(case when month(recvd_date)=3 then count(distinct(app_id)) else 0 end) as Mar,
  5. sum(case when month(recvd_date)=4 then count(distinct(app_id)) else 0 end) as Apr,
  6. sum(case when month(recvd_date)=5 then count(distinct(app_id)) else 0 end) as May,
  7. sum(case when month(recvd_date)=6 then count(distinct(app_id)) else 0 end) as Jun,
  8. sum(case when month(recvd_date)=7 then count(distinct(app_id)) else 0 end) as Jul,
  9. sum(case when month(recvd_date)=8 then count(distinct(app_id)) else 0 end) as Aug,
  10. sum(case when month(recvd_date)=9 then count(distinct(app_id)) else 0 end) as Sep,
  11. sum(case when month(recvd_date)=10 then count(distinct(app_id)) else 0 end) as Oct,
  12. sum(case when month(recvd_date)=11 then count(distinct(app_id)) else 0 end) as Nov,
  13. sum(case when month(recvd_date)=12 then count(distinct(app_id)) else 0 end) as Dec
  14. from schema.app_table
  15. --where year(recvd_date) = 2023
  16. group by year(recvd_date)

它会给出以下期望的格式:

  1. year(recvd_date) Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
  2. 2023 66 45 21 22 10 9 8 0 0 0 0 0

请注意,我已将每个月份的计数相加,以便它们出现在同一行,并且对于其他月份,我将它们填充为零。

英文:

I have a SQL written as below:

  1. select year(recvd_date),
  2. (case when month(recvd_date)=1 then count(distinct(app_id)) else 0 end) as Jan,
  3. (case when month(recvd_date)=2 then count(distinct(app_id)) else 0 end) as Feb,
  4. (case when month(recvd_date)=3 then count(distinct(app_id)) else 0 end) as Mar,
  5. (case when month(recvd_date)=4 then count(distinct(app_id)) else 0 end) as Apr,
  6. (case when month(recvd_date)=5 then count(distinct(app_id)) else 0 end) as May,
  7. (case when month(recvd_date)=6 then count(distinct(app_id)) else 0 end) as Jun,
  8. (case when month(recvd_date)=7 then count(distinct(app_id)) else 0 end) as Jul,
  9. (case when month(recvd_date)=8 then count(distinct(app_id)) else 0 end) as Aug,
  10. (case when month(recvd_date)=9 then count(distinct(app_id)) else 0 end) as Sep,
  11. (case when month(recvd_date)=10 then count(distinct(app_id)) else 0 end) as Oct,
  12. (case when month(recvd_date)=11 then count(distinct(app_id)) else 0 end) as Nov,
  13. (case when month(recvd_date)=12 then count(distinct(app_id)) else 0 end) as Dec
  14. from schema.app_table
  15. --where year(recvd_date) = 2023
  16. group by year(recvd_date), month(recvd_date)

It is giving me the right numbers but the format is not exactly what I expect. The numbers appear on different rows and zero filled for other months. How can I achieve the below format.

Expected Format:

  1. year(recvd_date) Jan Feb Mar Apr May Jun Jul
  2. 2023 66 45 21 22 10 9 8

答案1

得分: 1

这可以通过使用条件聚合来完成:

  1. select year(recvd_date),
  2. count(distinct case when month(recvd_date) = 1 then app_id end) as 一月,
  3. count(distinct case when month(recvd_date) = 2 then app_id end) as 二月,
  4. ....
  5. from schema.app_table
  6. -- where year(recvd_date) = 2023
  7. group by year(recvd_date)

在此处测试过MySQL:https://dbfiddle.uk/WbY_jC_W

英文:

This can be done using the conditional aggregation :

  1. select year(recvd_date),
  2. count(distinct case when month(recvd_date) = 1 then app_id end) as Jan,
  3. count(distinct case when month(recvd_date) = 2 then app_id end) as Feb,
  4. ....
  5. from schema.app_table
  6. -- where year(recvd_date) = 2023
  7. group by year(recvd_date)

Tested on mysql here : https://dbfiddle.uk/WbY_jC_W

huangapple
  • 本文由 发表于 2023年6月26日 19:13:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定