sql-行转列2(转置)

行转列的常规做法是,group by+sum(if())【或count(if())】

例题
表table如下:

DDate shengfu
2015-05-09
2015-05-09
2015-05-09
2015-05-09
2015-05-10
2015-05-10
2015-05-10

如果要生成下列结果, 该如何写sql语句?

DDate
2015-05-09 2 2
2015-05-10 1 2
--建表
create table table1(DDate string, shengfu string) ;
insert overwrite table table1 values ('2015-05-09', "胜"),
       ('2015-05-09', "胜"),
       ('2015-05-09', "负"),
       ('2015-05-09', "负"),
       ('2015-05-10', "胜"),
       ('2015-05-10', "负"),
       ('2015-05-10', "负");

select 
	DDate,
    ,SUM(case when shengfu = '胜' then 1 else 0 end) ``
    ,SUM(case when shengfu = '负' then 1 else 0 end) ``
from table1
group by DDate;

相关推荐

  1. sql-2()

    2024-05-13 11:14:05       34 阅读
  2. sql-()

    2024-05-13 11:14:05       32 阅读
  3. sql-3()

    2024-05-13 11:14:05       27 阅读
  4. oracle 转行

    2024-05-13 11:14:05       28 阅读
  5. Hive数据仓库

    2024-05-13 11:14:05       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-13 11:14:05       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-13 11:14:05       74 阅读
  3. 在Django里面运行非项目文件

    2024-05-13 11:14:05       61 阅读
  4. Python语言-面向对象

    2024-05-13 11:14:05       71 阅读

热门阅读

  1. sql-行转列(转置)

    2024-05-13 11:14:05       32 阅读
  2. 母亲节祝福html源码示例

    2024-05-13 11:14:05       22 阅读
  3. Es6 Generator 生成器函数

    2024-05-13 11:14:05       31 阅读
  4. vben框架是什么

    2024-05-13 11:14:05       36 阅读
  5. 新闻标题抓取

    2024-05-13 11:14:05       33 阅读
  6. 【学习笔记】C++每日一记

    2024-05-13 11:14:05       35 阅读
  7. Python小程序 - 文件处理1(使用AI工具)

    2024-05-13 11:14:05       31 阅读
  8. 规则引擎drools Part5

    2024-05-13 11:14:05       33 阅读
  9. 开发一款抓大鹅游戏

    2024-05-13 11:14:05       45 阅读
  10. Debug: Pytorch dataloaders OSError: Bad file descriptor

    2024-05-13 11:14:05       35 阅读