解决方案 »

  1.   

    http://188.93.174.57/#q=t-sql+%22group+by%22+max&newwindow=1&tbs=lr:lang_1zh-CN%7Clang_1zh-TW&lr=lang_zh-CN%7Clang_zh-TW
      

  2.   

    with tb(ID,TM,VALUE)as
    (
    select 1,'2014-01-01',10 union
    select 1,'2014-01-02',12 union
    select 1,'2014-01-03',14 union
    select 1,'2014-01-04',11 union
    select 1,'2014-01-05',19 union
    select 2,'2014-01-01',10 union
    select 2,'2014-01-02',11 union
    select 2,'2014-01-03',11 union
    select 2,'2014-01-04',11 union
    select 2,'2014-01-05',10 union
    select 2,'2014-01-06',10 union
    select 3,'2014-01-01',13 union
    select 3,'2014-01-02',14 union
    select 3,'2014-01-03',17 union
    select 3,'2014-01-04',10 union
    select 3,'2014-01-05',11
    )
    select * from (select ID,TM,VALUE,row_number() over(partition by ID order by VALUE desc) as rownumber from tb where TM between '2014-01-05' and '2014-01-06') as T where T.rownumber = 1;
      

  3.   

    做了个试验,发现用union连接起来的语句速度最快
      

  4.   

    但是不知道程序中的sql语句字符串能不能那么长啊?
      

  5.   

    with tb(ID,TM,VALUE)as
    (
    select 1,'2014-01-01',10 union
    select 1,'2014-01-02',12 union
    select 1,'2014-01-03',14 union
    select 1,'2014-01-04',11 union
    select 1,'2014-01-05',19 union
    select 2,'2014-01-01',10 union
    select 2,'2014-01-02',11 union
    select 2,'2014-01-03',11 union
    select 2,'2014-01-04',11 union
    select 2,'2014-01-05',10 union
    select 2,'2014-01-06',10 union
    select 3,'2014-01-01',13 union
    select 3,'2014-01-02',14 union
    select 3,'2014-01-03',17 union
    select 3,'2014-01-04',10 union
    select 3,'2014-01-05',11
    )
    select * from (select ID,TM,VALUE,row_number() over(partition by ID order by VALUE desc) as rownumber from tb where TM between '2014-01-05' and '2014-01-06') as T where T.rownumber = 1 and ID in (1,2);
    ID TM VALUE rownumber
    1 2014-01-05 19 1
    2 2014-01-05 10 1
      

  6.   

    楼主sql什么版本的?
    CTE要sql 2005以上版本才支持的
      

  7.   


    学习一下 sql 语句教程,了解“分组”的概念。
      

  8.   

    另外,什么“最大、最小、平均、计数.....”之类的,这叫做 sql 语言中的统计函数,也是应该会直接拿来用的基本编程知识。
      

  9.   

    用程序试验了3种方法,表中数据量2千万。实际情况是表中还有个字段datatype,就是同一个ID对应多个datatype。表的主键为ID,datatype,TM。试验取一种datatype,该datatype下的ID有78个。
    (1)
    本来用
    select  ID, TM,VALUE
      FROM A b where b.VALUE=(select MAX(VALUE) FROM A where ID=b.ID and datatype=‘X’)  order by ID
    但是发现最大的VALUE值也有重复的,取最大值最早出现的时间,所以用以下语句
    select  ID,min(TM) TM,VALUE
      FROM A b where b.VALUE=(select MAX(VALUE) FROM A where ID=b.ID and datatype=‘X’) group by ID,VALUE  order by ID
    (2)用一个很长的语句
    select ID,TM,VALUE from (SELECT TOP 1 ID,TM,VALUE  FROM A where ID=1 and datatype=‘X’ order by DI desc) b
     union
    select ID,TM,VALUE  from (SELECT TOP 1 ID,TM,VALUE  FROM A where ID=2 and datatype=‘X’ order by DI desc) b
     union
    ...............
    select ID,TM,VALUE  from (SELECT TOP 1 ID,TM,VALUE  FROM A where ID=77 and datatype=‘X’ order by DI desc) b
     union
    select ID,TM,VALUE  from (SELECT TOP 1 ID,TM,VALUE  FROM A where ID=78 and datatype=‘X’ order by DI desc) b
    (3)
    SELECT TOP 1 ID,TM,VALUE  FROM A where ID=1 and datatype=‘X’ order by DI desc
    SELECT TOP 1 ID,TM,VALUE  FROM A where ID=2 and datatype=‘X’ order by DI desc
    ...............
    SELECT TOP 1 ID,TM,VALUE  FROM A where ID=77 and datatype=‘X’ order by DI desc
    SELECT TOP 1 ID,TM,VALUE  FROM A where ID=78 and datatype=‘X’ order by DI desc
    测试结果(耗时):
    (1)4125ms
    (2)2373ms
    (3)4867ms
      

  10.   

    group by +MAX()并不是最快的,但是它的语句是最精简的