邱明 2015-10-07 09:14 采纳率: 0%
浏览 4277

SQL 按条件统计各个字段次数

 ID value1  value2  value3  value4  value5 DATETIME_col
A   50  75  0   0   0   2015-10-07 12:27:00.000
A   50  75  0   0   0   2015-10-07 12:29:00.000
A   51  75  0   0   0   2015-10-07 12:39:00.000
B   36  42  29  12  0   2015-10-07 16:58:00.000
B   36  43  30  19  0   2015-10-07 16:57:00.000
B   36  43  30  12  0   2015-10-07 16:56:00.000
C   28  33  29  10  89  2015-10-07 16:54:00.000
C   29  34  29  10  16  2015-10-07 16:55:00.000
C   29  35  29  15  18  2015-10-07 16:56:00.000
C   29  35  30  16  18  2015-10-07 16:57:00.000
D   31  55  34  35  19  2015-10-07 00:10:00.000
D   31  54  34  38  31  2015-10-07 00:11:00.000
D   31  54  34  33  15  2015-10-07 00:12:00.000

条件 在2015-10-07这一天统计 value1>30,value2>30,value3>30,value4<15,value5<40 的结果

结果希望这样显示
ID  vcount1 vcount2 vcount3 vcount4 vcount5 DATE_col
A   3   3   0   3   3   2015-10-07 
B   3   3   0   2   3   2015-10-07 
C   0   4   0   2   3   2015-10-07 
D   3   3   3   0   3   2015-10-07 


  • 写回答

1条回答 默认 最新

  • chinamobile851116 2023-08-16 11:52
    关注
    
    with t as 
    (
    
    select 'A' as ID ,50 as value1 ,75 as value2 ,0 as value3,0 as value4 ,0 as value5, '2015-10-07 12:27:00.000' as DATETIME_col
    union all 
    select 'A' ,50,75,0,0,0,'2015-10-07 12:29:00.000'
    union all
    select 'A' ,51,75,0,0,0,'2015-10-07 12:39:00.000'
    union all
    select 'B',36,42,29,12,0,'2015-10-07 16:58:00.000'
    union all
    select 'B',36,43,30,19,0,'2015-10-07 16:57:00.000'
    union all
    select 'B',36,43,30,12,0,'2015-10-07 16:56:00.000'
    union all
    select 'C',28,33,29,10,89,'2015-10-07 16:54:00.000'
    union all
    select 'C',29,34,29,10,16,'2015-10-07 16:55:00.000'
    union all
    select 'C',29,35,29,15,18,'2015-10-07 16:56:00.000'
    union all
    select 'C',29,35,30,16,18,'2015-10-07 16:57:00.000'
    union all
    select 'D',31,55,34,35,19,'2015-10-07 00:10:00.000'
    union all
    select 'D',31,54,34,38,31,'2015-10-07 00:11:00.000'
    union all
    select 'D',31,54,34,33,15,'2015-10-07 00:12:00.000'
    )
    select 
        id
        ,sum(mark_v1) as vcount1
        ,sum(mark_v2) as vcount2 
        ,sum(mark_v3) as vcount3 
        ,sum(mark_v4) as vcount4 
        ,sum(mark_v5) as vcount5
        ,max('2015-10-07 00:11:00.000'::date)
    from
        (
            select 
                *
                ,case when value1>30 then 1 else 0 end as mark_v1
                ,case when value2>30 then 1 else 0 end as mark_v2
                ,case when value3>30 then 1 else 0 end as mark_v3
                ,case when value4<15 then 1 else 0 end as mark_v4
                ,case when value5<40 then 1 else 0 end as mark_v5
            from 
                t 
        ) a 
    group by id order by id
    

    仅供参考

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler