【Table1】
编号     日期      TYPE     INPUT    OUTPUT   RESULT
1     2010-3-8      A        50        54       15
2     2010-3-8      B        50        37       29
3     2010-3-8      C        80        72       21
4     2010-3-9      A        70        38       47
5     2010-3-9      B        80        61       48
6     2010-3-9      C        60        29       52
7     2010-3-10     A        40        54       33
8     2010-3-10     B        40        67       21
9     2010-3-10     C        10        52       10想得到的查询结果是,INPUT上保留各个TYPE的MAX值,OUTPUT上保留各个TYPE的MIN值,RESULT保留各个TYPE的最早的值(也就是3月8日的):
编号      TYPE     INPUT    OUTPUT   RESULT
1          A        70        38       15
2          B        80        37       29
3          C        80        29       21

解决方案 »

  1.   

    select 
     编号=row_number()over(order by getdate()).*
    from
     (
     select
      *
     from
       (select [type],max(INPUT),min(OUTPUT) from tb group by [type])a
     join
       (select [type],[RESULT] from tb t where 日期=(select min(日期) from tb where [type]=t.[type])b
      on
        a.[type]=b.[type]
      )t
      

  2.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2010-03-26 20:42:30
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([编号] int,[日期] datetime,[TYPE] varchar(1),[INPUT] int,[OUTPUT] int,[RESULT] int)
    insert [tb]
    select 1,'2010-3-8','A',50,54,15 union all
    select 2,'2010-3-8','B',50,37,29 union all
    select 3,'2010-3-8','C',80,72,21 union all
    select 4,'2010-3-9','A',70,38,47 union all
    select 5,'2010-3-9','B',80,61,48 union all
    select 6,'2010-3-9','C',60,29,52 union all
    select 7,'2010-3-10','A',40,54,33 union all
    select 8,'2010-3-10','B',40,67,21 union all
    select 9,'2010-3-10','C',10,52,10
    --------------开始查询--------------------------
    select 
     编号=row_number()over(order by getdate()),*
    from
     (
     select
      a.*,b.[RESULT]
     from
       (select [type],max(INPUT) as INPUT,min([OUTPUT]) as [OUTPUT] from tb group by [type])a
     join
       (select [type],[RESULT] from tb t where 日期=(select min(日期) from tb where [type]=t.[type]))b
      on
        a.[type]=b.[type]
      )t----------------结果----------------------------
    /* 编号                   type INPUT       OUTPUT      RESULT
    -------------------- ---- ----------- ----------- -----------
    1                    A    70          38          15
    2                    B    80          37          29
    3                    C    80          29          21(3 行受影响)
    */
      

  3.   

    select 编号=row_number()over(order by getdate()),
        [type],max(INPUT) INPUT,min(OUTPUT) OUTPUT,
        RESULT=(select RESULT from tb where type=t.type order by 日期)
    from tb t
    group by type
      

  4.   

    --------------------------------------------------------------------------
    --  Author : htl258(Tony)
    --  Date   : 2010-03-26 20:53:33
    --  Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    --          Jul  9 2008 14:43:34 
    --          Copyright (c) 1988-2008 Microsoft Corporation
    --          Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)
    --------------------------------------------------------------------------
    --> 生成测试数据表:tbIF NOT OBJECT_ID('[tb]') IS NULL
    DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([编号] INT,[日期] DATETIME,[TYPE] NVARCHAR(10),[INPUT] INT,[OUTPUT] INT,[RESULT] INT)
    INSERT [tb]
    SELECT 1,N'2010-3-8','A',50,54,15 UNION ALL
    SELECT 2,N'2010-3-8','B',50,37,29 UNION ALL
    SELECT 3,N'2010-3-8','C',80,72,21 UNION ALL
    SELECT 4,N'2010-3-9','A',70,38,47 UNION ALL
    SELECT 5,N'2010-3-9','B',80,61,48 UNION ALL
    SELECT 6,N'2010-3-9','C',60,29,52 UNION ALL
    SELECT 7,N'2010-3-10','A',40,54,33 UNION ALL
    SELECT 8,N'2010-3-10','B',40,67,21 UNION ALL
    SELECT 9,N'2010-3-10','C',10,52,10
    GO
    --SELECT * FROM [tb]-->SQL查询如下:
    select 编号=row_number()over(order by getdate()),
        [type],max(INPUT) INPUT,min(OUTPUT) OUTPUT,
        RESULT=(select TOP 1 RESULT from tb where type=t.type order by 日期)
    from tb t
    group by type
    /*
    编号                   type       INPUT       OUTPUT      RESULT
    -------------------- ---------- ----------- ----------- -----------
    1                    A          70          38          15
    2                    B          80          37          29
    3                    C          80          29          21(3 行受影响)
    */
      

  5.   

    “t.type”是什么用法?我用的是Access,好像没有这种用法,帮我再看看,谢谢了!!!
      

  6.   

    这个句子可以用到Access中吗?好像没有看过这样的“t.type”,再帮我看看,谢谢了!!!
      

  7.   

    select type,max(input)as input,min(output) as output,
    (select min(tb.result) from tb
    where  type =T.TYPE and tb.date = (select min(date) from tb as T1 where T1.type = T.type)  ) as result
    from tb as T group by TYPE;
      

  8.   

    select type,max(input)as input,min(output),(select RESULT from tb where type=T.type order by date limit 1 ) as s from tb as T group by TYPE;