解决方案 »

  1.   


    create table test (id int,数量 int, 时间 datetime,内码1 int ,内码2 int)
    insert test 
    select 1,30,'2011-3-3',1992,1 union all 
    select 2,20,'2011-6-5',1992,1 union all 
    select 3,22,'2012-5-1',1993,2 union all 
    select 4,11,'2013-5-6',1991,1
    select MIN(id),
    SUM(数量),
    MAX(时间),
    内码1,
    内码2
    from test 
    group by 内码1,
    内码2
    order by MIN(id)
      

  2.   

    要是表里没IDcreate table test (数量 int, 时间 datetime,内码1 int ,内码2 int)
    insert test 
    select 30,'2011-3-3',1992,1 union all 
    select 20,'2011-6-5',1992,1 union all 
    select 22,'2012-5-1',1993,2 union all 
    select 11,'2013-5-6',1991,1select SUM(数量),
    MAX(时间),
    内码1,
    内码2
    from test 
    group by 内码1,
    内码2
      

  3.   

    select 
            SUM(数量),
            MAX(时间),
            内码1,
            内码2
    from test 
    group by 内码1,
            内码2
      

  4.   


    Quote: 引用 1 楼 chwnrthd 的回复:
    create table test (id int,数量 int, 时间 datetime,内码1 int ,内码2 int)
    insert test 
    select 1,30,'2011-3-3',1992,1 union all 
    select 2,20,'2011-6-5',1992,1 union all 
    select 3,22,'2012-5-1',1993,2 union all 
    select 4,11,'2013-5-6',1991,1
    select MIN(id),
    SUM(数量),
    MAX(时间),
    内码1,
    内码2
    from test 
    group by 内码1,
    内码2
    order by MIN(id)
    select 1,30,'2011-3-3',1992,1 union all 
    select 2,20,'2011-6-5',1992,1 union all 
    select 3,22,'2012-5-1',1993,2 union all 
    select 4,11,'2013-5-6',1991,1
    这一段代码,要是我有十几万条数据...一条一条的union  all....那不是要做死的节奏啊~! union all  它好像一般适用于两个表检索出的重复数据简单的合并! 
      

  5.   


    哥哥诶~ 我那时创建测试表插入测试数据呢····· select SUM(数量),
            MAX(时间),
            内码1,
            内码2
    from test 
    group by 内码1,
            内码2
      

  6.   

    group by 就行,也可以用row_number的
      

  7.   

    IF EXISTS(SELECT name FROM sys.objects WHERE name = 'test')
    DROP TABLE test
    go
    CREATE TABLE test
    (
    dt nvarchar(10) ,
    no1 NVARCHAR(10) , 
    no2 NVARCHAR(10),
    num int
    )
    GOINSERT INTO test (dt , no1 ,no2, num)
    SELECT '2011-3-3' , '1992' , '1' , 30 UNION ALL
    SELECT '2011-6-5' , '1992'  ,'1' , 20 UNION ALL
    SELECT '2012-5-1' , '1993'   , '2', 22 UNION ALL
    SELECT '2013-5-6' , '1991'   ,'1',  11 
    ---------------------------------執行查詢----------------------
    SELECT SUM(num) AS 數量 , MAX(dt) AS 時間 , no1 , no2 FROM test GROUP BY no1 ,no2 
    ORDER BY SUM(num) desc/*
    數量          時間         no1        no2
    ----------- ---------- ---------- ----------
    50          2011-6-5   1992       1
    22          2012-5-1   1993       2
    11          2013-5-6   1991       1(3 row(s) affected)
    */
      

  8.   


    哥哥诶~ 我那时创建测试表插入测试数据呢····· select SUM(数量),
            MAX(时间),
            内码1,
            内码2
    from test 
    group by 内码1,
            内码2

    大哥的方法已经解决问题!