有一个表TABL,字段数据的值为
DEP      MRP_NO    BZ
一车间    M1      0.23
一车间    M2      0.33
一车间    M3      0.45
二车间    M1      0.45
二车间    M2      0.33
二车间    M3      0.45
二车间    M4      0.67
三车间    M1      0.45
三车间    M2      0.33
三车间    M3      0.45
三车间    M4      0.78我想把MRP_NO值相同只显示一个,但DEP值只要有一个不同,且BZ只要有一个不同的值显示出来。
怎么写这个语句,时数出来的结果显示为MRP_NO     一车间    二车间   三车间   
M1          0.23       0.45    0.45
M3          0.45       0.45    0.45
M4          NULL       0.67    0.78

解决方案 »

  1.   

    declare @t table 
    (DEP varchar(6),MRP_NO varchar(2),BZ numeric(3,2))
    insert into @t
    select '一车间','M1',0.23 union all
    select '一车间','M2',0.33 union all
    select '一车间','M3',0.45 union all
    select '二车间','M1',0.45 union all
    select '二车间','M2',0.33 union all
    select '二车间','M3',0.45 union all
    select '二车间','M4',0.67 union all
    select '三车间','M1',0.45 union all
    select '三车间','M2',0.33 union all
    select '三车间','M3',0.45 union all
    select '三车间','M4',0.78select MRP_NO,
    '一车间'=max(case DEP when '一车间' then BZ else 0 end),
    '二车间'=max(case DEP when '二车间' then BZ else 0 end),
    '三车间'=max(case DEP when '三车间' then BZ else 0 end)
    from @t group by MRP_NO
    /*
    MRP_NO 一车间                                     二车间                                     三车间
    ------ --------------------------------------- --------------------------------------- ---------------------------------------
    M1     0.23                                    0.45                                    0.45
    M2     0.33                                    0.33                                    0.33
    M3     0.45                                    0.45                                    0.45
    M4     0.00                                    0.67                                    0.78
    */
      

  2.   

    declare @s varchar(1000)
    select @s = ISNULL(@s+',','')+'max(case when dep = '''+dep+''' then bz else null end) as ['+dep+']'
    from 
    (select 
         dep
     from ta
     group by DEP) a
    exec('select [MRP_NO],'+@s+ ' from ta a where exists(select 1 from ta where a.[MRP_NO] = [MRP_NO] and [BZ]!=a.[BZ]) group by [MRP_NO] ')--having count(1) = (select count(1) from ta where )'
      

  3.   


    create table TABL(
        DEP varchar(10),
    MRP_NO varchar(10),
        BZ float
    )
    goinsert into TABL(DEP,MRP_NO,BZ)
    select '一车间','M1',0.23
    union all select '一车间','M2',0.33
    union all select '一车间','M3',0.45
    union all select '二车间','M1',0.45
    union all select '二车间','M2',0.33
    union all select '二车间','M3',0.45
    union all select '二车间','M4',0.67
    union all select '三车间','M1',0.45
    union all select '三车间','M2',0.33
    union all select '三车间','M3',0.45
    union all select '三车间','M4',0.78
    go/*
    select * from TABL
    */--查询方式
    select MRP_NO,
    一车间=max(case DEP when '一车间' then BZ else null end),
    二车间=max(case DEP when '二车间' then BZ else null end),
    三车间=max(case DEP when '三车间' then BZ else null end)
    from TABL group by MRP_NO
    godrop table TABL
    go/*
    M1 0.23 0.45 0.45
    M2 0.33 0.33 0.33
    M3 0.45 0.45 0.45
    M4 NULL 0.67 0.78--插一句,哥们你取表名和字段名的方式真个性!在下佩服!