表process如下
   车间   产品ID   工序号
    1      1       10
    1      1       20
    2      1       30
    3      1       40
    1      2       10
    2      2       20(产品ID代表一个产品,工序号代表该产品的生产工序,而车间代表着每个产品的每道生产工序是在哪一个车间完成的)我想得出每个产品在哪 几个车间生产过?
如表temp
   产品ID  车间个数
    1        3
   2         2
谢谢!

解决方案 »

  1.   

    select 产品ID , count(distinct 车间) 车间个数 from process group by 产品ID 
      

  2.   


    select 产品ID,车间个数=count(distinct 车间) from process group by 产品ID
      

  3.   

    select 产品ID,count(distinct 车间) as 车间个数
    from process
    group by 产品ID
      

  4.   

    create table tb(车间 int,  产品ID int,  工序号 int)
    insert into tb values(1,      1,       10 )
    insert into tb values(1,      1,       20 )
    insert into tb values(2,      1,       30 )
    insert into tb values(3,      1,       40 )
    insert into tb values(1,      2,       10 )
    insert into tb values(2,      2,       20 )
    goselect 产品ID , count(distinct 车间) 车间个数 from tb group by 产品ID drop table tb/*
    产品ID        车间个数        
    ----------- ----------- 
    1           3
    2           2(所影响的行数为 2 行)
    */