【问题标题】:How mysql count birth month each class and columns are classesmysql如何计算每个类和列是类的出生月份
【发布时间】:2019-05-19 05:27:24
【问题描述】:

mysql db 中的数据是这样的,并在 WPF 中的 Datagrid 中显示结果

 ---------------------------
 class student_ID birthday
 A     1          2000/1/1
 A     5          2001/4/1
 B     2          2000/1/12
 C     3          2001/8/5
 .     .          .
 .     .          .
 Z     1000       2000/12/2
---------------------------

我知道使用 group by 和 rollup 可以选择小计和总计

但我不知道像统计表那样做

----------------------------------
 month\class    A    B    C ... Z
 JAN            1    2    3 ... 0
 FEB            0    5    6 ... 7
 .              .    .    .
 .              .    .    .
 DEC            4    8    6 ... 1
----------------------------------

我的一种方法是每个月都用case,但是如果问题变成统计日期而不是月份,365列是不可能的。

google解决问题的方法是什么?

【问题讨论】:

标签: c# mysql wpf


【解决方案1】:

怎么样:

select
  monthname(birthday),
  sum(case when class = 'A' then 1 end) as a,
  sum(case when class = 'B' then 1 end) as b,
  -- add the other 24 letters here
from my_table
group by monthname(birthday)

【讨论】:

  • 但是如果类不修复,我应该使用循环来做吗?
  • 那么你需要一个真正的表“pivot”。我认为 MySQL 不支持这些。
猜你喜欢
  • 2017-03-24
  • 2021-11-25
  • 2011-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多