急求一条SQL语句
我有一表如下;id       bm         mc       sl       dj        zj 
1       001        钢笔      6       3.00      18.00
2       001        钢笔      5       3.00      15.00
3       002        铅笔     10       1.00      10.00
4       002        铅笔     15       1.10      16.50
5       002        铅笔      9      1.00        9.00
6       003        圆珠笔    8       2.00      16.00
7       003        圆珠笔    7       2.20      15.40
8       003        圆珠笔    5       2.00      10.00
..      ..         ..        ..      ..        ..
..      ..         ..        ..      ..        ..
得到   编码(bm)相同,名称相同(mc)但单价(dj)不同的表2
id       bm         mc       sl       dj        zj 
1       002        铅笔     19       1.00      19.00
2       002        铅笔     15       1.10      16.50
3       003        圆珠笔   13       2.00      26.00
4       003        圆珠笔    7       2.20      15.40
..      ..         ..        ..      ..        ..
..      ..         ..        ..      ..        ..1,  编码(bm)相同,名称相同(mc)单价(dj)相同的记录不进入表2
2,进入表2的相同记录合并为1条。

解决方案 »

  1.   

    select * from tab a where (select count(1) from tab where bm=a.bm and mc=a.mc and dj=a.dj)>1
      

  2.   

    insert into tb2
    select bm,mc,sum(sl) as sl,max(dj),sum(zj) as zj
    from (
        select *
        from ta a
        where not exists(select 1 from ta where bm = a.bm and mc = a.mc and dj = a.dj)) b
    group by bm ,mc
      

  3.   

    select bm,mc,dj from 表1 group by bm,mc,dj having(count(*))=1
      

  4.   

    create table shangpin1
    (
    id int identity(1,1) not null,
    bm nvarchar(10),
    mc nvarchar(50),
    dj money
    )create table shangpin2
    (
    id int identity(1,1) not null,
    bm nvarchar(10),
    mc nvarchar(50),
    dj money
    )insert shangpin1 values('001','钢笔',3)
    insert shangpin1 values('001','钢笔',3)
    insert shangpin1 values('002','铅笔',1)
    insert shangpin1 values('002','铅笔',1.1)
    insert shangpin1 values('003','圆珠笔',2)
    insert shangpin1 values('003','圆珠笔',2.2)
    select * from shangpin1
    insert shangpin2 select distinct bm,mc,dj from shangpin1select * from shangpin2
      

  5.   

    select bm,mc,dj from TABLE1 GROUP BY BM,MC,DJ having(count(*))=1
      

  6.   

    insert into tb2 
    select bm,mc,sum(sl) as sl,dj,sum(zj) as zj 
    from ta a 
    where exists (
      select 1 from ta where bm=a.bm and mc=a.mc and dj<>a.dj
      )
    group by bm ,mc,dj