一个表有4列,smsTypeOne,smsTypeTwo ,smsTypeThree,smsTypeFour
现在有个需要
查询 
1.只有一个分类的量
2.有二个分类 的量
3.有三个分类 的量
4.有四个分类 的量2,3需求,组合情况太多,不知道如何实现,是否有什么函数可以实现。

解决方案 »

  1.   

    表结构
    smsTypeOne,smsTypeTwo ,smsTypeThree,smsTypeFour
    aaa          aaa         ccc           ddd
    bbb          ccc         aaa           aaa
    ccc          bbb         bbb           bbb
    ===============
    求 有二个分类 的量,有三个分类 的量
      

  2.   

    比如:
    只有一个分类的量
    select count(*) where smsTypeOne=smsTypeTwo 
    and smsTypeTwo=smsTypeThree and smsTypeThree=smsTypeFour
      

  3.   

    with cte1 as
    (
      select row_number() over(order by getdate()) no,* from tb
    )
     cte2 as
    (
      select no,smsTypeOne  from cte1
      union
      select no,smsTypeTwo  from cte1
      union
      select no,smsTypeThree from cte1
      union
      select no,smsTypeFour from cte1
    )select * from cte1 where no in(select no from cte2 group by no having count(*)=1)
    select * from cte1 where no in(select no from cte2 group by no having count(*)=2)
    select * from cte1 where no in(select no from cte2 group by no having count(*)=3)
    select * from cte1 where no in(select no from cte2 group by no having count(*)=4)