有table表下id,company,deptment 3个列。
如何在知道id的情况下,根据id查询出各个company下的deptment的个数?
deptment有重复。
谢谢 ,在线等~

解决方案 »

  1.   

    我写的查询语句
    select t.company,t.deptment,count(t.deptment) from table t  group by 
    grouping sets((t.company,t.deptment)) 为什么总是多出t.company的行数,怎么能把这行去掉?
    谢谢~
      

  2.   

    --不去重复
    select id , company , count(deptment) from table group by id , company
    --去重复
    select id , company , count(distinct deptment) from table group by id , company
      

  3.   

    select company ,count(distinct deptment) from table  
    where company=(select company from table where id=已知id)
    group by company;先根据已知ID找出company再根据company统计不同的deptment数
      

  4.   

    select distinct company,count(deptment) over(partition by company) from table1 where id=xxx
      

  5.   

    select count(1) from table1 where id=xxx group by company