«

sql group by、case when用法举例

发布于 阅读:71


select
        txtmemo,
        count(*) as [总计],
        sum(case 
            when   w1.status = 6 then 1 
            else 0 
        end) as [已完成],
        sum(case 
            when w1.userTxt1 = 'I类型' and w1.status = 6 then 1 
            else 0 
        end) as [未完成1类型],
        sum(case 
            when w1.userTxt1 = 'II类型' and w1.status = 6 then 1 
            else 0 
        end) as [未完成2类型],
        sum(case 
            when   w1.status = 5 then 1 
            else 0 
        end) as [未完成],
        sum(case 
            when w1.userTxt1 = 'I类型' and w1.status = 5 then 1 
            else 0 
        end) as [未完成1类型],
        sum(case 
            when w1.userTxt1 = 'II类型' and w1.status = 5 then 1 
            else 0 
        end) as [未完成2类型] 
    from
        PDM_WOMFGOrder w1 
    where
        w1.status in(5,6) 
        and  PrDtDate between '2019-07-08 00:00:00'  and  '2019-08-08 00:00:00' 
    group by
        txtmemo 

SQL