如下:表
单据号 代码 金额
11020301 A01 100
11020301 A02 100
11020302 C02 300
11020302 C04 300要求生成:
11020301 A01 100
11020302 C02 300

11020301 A02 100
11020302 C04 300

解决方案 »

  1.   

    select *
    from tb t
    where not exists(select 1 from tb where 单据号=t.单据号 and 代码<t.代码)
      

  2.   


    delete 表名 from 表名 b
    where not exists
    (select 1 from 表名 where 单据号=b.单据号 and 代码<b.代码)
      

  3.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([单据号] int,[代码] varchar(3),[金额] int)
    insert [tb]
    select 11020301,'A01',100 union all
    select 11020301,'A02',100 union all
    select 11020302,'C02',300 union all
    select 11020302,'C04',300select *
    from tb t
    where not exists(select 1 from tb where 单据号=t.单据号 and 代码<t.代码)
    /**
    单据号         代码   金额
    ----------- ---- -----------
    11020301    A01  100
    11020302    C02  300(2 行受影响)
    **/
    select *
    from tb t
    where not exists(select 1 from tb where 单据号=t.单据号 and 代码>t.代码)
    /**
    单据号         代码   金额
    ----------- ---- -----------
    11020301    A02  100
    11020302    C04  300(2 行受影响)**/
      

  4.   


    SQL code
    select *
    from tb t
    where not exists(select 1 from tb where 单据号=t.单据号 and 代码<t.代码)
      

  5.   

    --------------    随机删除重复记录!  -----------------------------
    --------------    Author: Luoyoumou   -----------------------------drop table test;
    --先求出符合条件的记录:
    create table test(col1 int identity(1,1), col2 varchar(10), col3 varchar(10));insert into test(col2, col3)
    select '我要银子','我没有' UNION ALL
    select '我要银子','你有没?' UNION ALL
    select '我要金子','金子涨价了' UNION ALL
    select '多要银元','我没有' UNION ALL
    select '我要银子','我没有' UNION ALL
    select '我要银子', '你有没?' UNION ALL
    select '我要银子', '明年才有' UNION ALL
    select '我要银子', '你有没?'select * from test;
    select distinct col2, col2 from test;
    select t1.all_sum as '所有记录行',
           t2.dis_sum as '无重复记录行', 
           t1.all_sum-t2.dis_sum as '将要删除的行数'
    from (select count(*) as all_sum from test ) t1,
    (select count(*) as dis_sum from (select distinct col2, col3 from test) t) t2;
    ---如:随机删除 col2='我要银子'  的重复记录
    select sum(row_sum) as all_del from ( 
    select col2, col3, count(col1)-1 as 'row_sum'
     from test
    where col2='我要银子'
    group by col2, col3
    having count(col1)-1<>0) t1--删除条件:将col2、col3都相同的,视为重复记录,将其随机删除指定条件(col2='我要银子')的重复记录,只剩一行
    --选择语句:
    select top (select sum(row_sum) as all_del from ( 
                 select col2, col3, count(col1)-1 as 'row_sum'
                from test
                where col2='我要银子'
                group by col2, col3
                having count(col1)-1<>0) t1) 
        t1.col1, t1.col2, t1.col3 
        from test t1 where t1.col2='我要银子' and t1.col1 not in 
      (select col1 from test t3 where exists ( 
                         select col2, col3 
                         from test where col2='我要银子'
                         and col2=t3.col2 and col3=t3.col3 
                         group by col2, col3 having count(col1)=1 ))
    order by NEWID()
    --删除语句:  将其随机删除(col2='我要银子'的重复记录)只剩一行
    DELETE FROM test
    WHERE col1 in ( select col1 from (
    select top (select sum(row_sum) as all_del from ( 
                 select col2, col3, count(col1)-1 as 'row_sum'
                from test
                where col2='我要银子'
                group by col2, col3
                having count(col1)-1<>0) t1) 
        t1.col1, t1.col2, t1.col3 
        from test t1 where t1.col2='我要银子' and t1.col1 not in 
      (select col1 from test t3 where exists ( 
                         select col2, col3 
                         from test where col2='我要银子'
                         and col2=t3.col2 and col3=t3.col3 
                         group by col2, col3 having count(col1)=1 ))
    order by NEWID()) t
    )
    --删除语句:  将其随机删除所有重复的记录,每组重复记录只剩一行
    DELETE FROM test
    WHERE col1 in ( select col1 from (
    select top (select sum(row_sum) as all_del from ( 
                 select col2, col3, count(col1)-1 as 'row_sum'
                from test
                group by col2, col3
                having count(col1)-1<>0) t1) 
        t1.col1, t1.col2, t1.col3 
        from test t1 where t1.col1 not in 
      (select col1 from test t3 where exists ( 
                         select col2, col3 
                         from test where col2=t3.col2 and col3=t3.col3 
                         group by col2, col3 having count(col1)=1 ))
    order by NEWID()) t
    )
      

  6.   

    select a.单据号,a.代码,a.金额 from ( 
    ( select top (100) percent row_number() OVER (ORDER BY 单据号) AS id,单据号,代码,金额 from u8 ) as a 
     join ( select min(id) as id from (select top (100) percent row_number() OVER 
    (ORDER BY 单据号) AS id,单据号,代码,金额 from u8) as u group by u.单据号 ) as t on t.id = a.id  )