zoukankan      html  css  js  c++  java
  • SQL面试题之行转列

     典型的课程表:

    mysql> select * from course;
    +----+------------+----------+------------+
    | id | teacher_id | week_day | has_course |
    +----+------------+----------+------------+
    |  1 |          1 |        2 | Yes        |
    |  2 |          1 |        3 | Yes        |
    |  3 |          2 |        1 | Yes        |
    |  4 |          3 |        2 | Yes        |
    |  5 |          1 |        2 | Yes        |
    +----+------------+----------+------------+

    对于每个教师的每一天进行判断:

    mysql> select teacher_id ,
        -> ( case week_day when '1' then 'Yes' else '' end ) as 'mon',
        -> ( case week_day when '2' then 'Yes' else '' end ) as 'tue',
        -> ( case week_day when '3' then 'Yes' else '' end ) as 'thi',
        -> ( case week_day when '4' then 'Yes' else '' end ) as 'thu',
        -> ( case week_day when '5' then 'Yes' else '' end ) as 'fri'
        -> from course
        -> ;
    +------------+-----+-----+-----+-----+-----+
    | teacher_id | mon | tue | thi | thu | fri |
    +------------+-----+-----+-----+-----+-----+
    |          1 |     | Yes |     |     |     |
    |          1 |     |     | Yes |     |     |
    |          2 | Yes |     |     |     |     |
    |          3 |     | Yes |     |     |     |
    |          1 |     | Yes |     |     |     |
    +------------+-----+-----+-----+-----+-----+

    这时候就能拿到每一条课程安排对于一周的影响(某一天有没有课)。

    可以看到有重复数据,或者说数据比较分散,这时候可以用分组来将数据进行合并:

    mysql> select teacher_id ,
        -> max( case week_day when '1' then 'Yes' else '' end ) as 'mon',
        -> max( case week_day when '2' then 'Yes' else '' end ) as 'tue',
        -> max( case week_day when '3' then 'Yes' else '' end ) as 'thi',
        -> max( case week_day when '4' then 'Yes' else '' end ) as 'thu',
        -> max( case week_day when '5' then 'Yes' else '' end ) as 'fri'
        -> from course
        -> group by teacher_id;
    +------------+------+------+------+------+------+
    | teacher_id | mon  | tue  | thi  | thu  | fri  |
    +------------+------+------+------+------+------+
    |          1 |      | Yes  | Yes  |      |      |
    |          2 | Yes  |      |      |      |      |
    |          3 |      | Yes  |      |      |      |
    +------------+------+------+------+------+------+

    要点:

    不论是列转行还是行转列,转过去的目标肯定是被“造出来”的(case...when);

    利用分组函数去重和合并。

  • 相关阅读:
    logo切图大小相应的尺寸
    logo切图大小相应的尺寸
    Linux学习笔记(二)——文件/目录/VIM
    Linux学习笔记(二)——文件/目录/VIM
    什么湖南小吃?仅1年就获得千万投资
    硕士毕业的他做生鲜电商网站,日流水3万元
    他回到家乡贷款创业,如今公司年产值100万美元
    下岗工人也能致富,她靠养殖昆虫改变人生
    他用8000元互联网创业起家,如今年营业额500万
    他卖骨头汤成为南京“汤王”,如今资产上百万
  • 原文地址:https://www.cnblogs.com/at0x7c00/p/8305094.html
Copyright © 2011-2022 走看看