• [SQL]CASE用户数据统计


    create table tb(id int ,class varchar)--class种类就只有三种,如果不固定就需要存储过程来实现
    insert tb 
    select 1,'a' union all
    select 1,'a' union all
    select 1,'b' union all
    select 1,'c' union all
    select 2,'a' union all
    select 2,'b' union all
    select 2,'b' 
    select * from tb
    
    --想查找出按id分组得到的 a  ,b  ,c 的数量
    --  如下
    --id   a   b    c
    --1   2   1     1
    --2   1   2    0 
    
    
    select 
     id,
     
     a=sum(case class when 'a' then 1 else 0 end),
     b=sum(case class when 'b' then 1 else 0 end),
     c=sum(case class when 'c' then 1 else 0 end)
    from 
     tb
    group by
     id
  • 相关阅读:
    Flink
    数据工程师
    数据库中间件
    数据仓库
    数据库
    设计模式
    机器学习
    Collections
    Concurrency
    Java
  • 原文地址:https://www.cnblogs.com/beeone/p/3622288.html
Copyright © 2020-2023  润新知