--按某一字段分组取最大(小)值所在行的数据
(爱新觉罗.毓华 2007-10-23于浙江杭州)
/*
数据如下:
name val memo
a    2   a2(a的第二个值)
a    1   a1--a的第一个值
a    3   a3:a的第三个值
b    1   b1--b的第一个值
b    3   b3:b的第三个值
b    2   b2b2b2b2
b    4   b4b4
b    5   b5b5b5b5b5
*/
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a',    2,   'a2(a的第二个值)')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('b',    1,   'b1--b的第一个值')
insert into tb values('b',    3,   'b3:b的第三个值')
insert into tb values('b',    2,   'b2b2b2b2')
insert into tb values('b',    4,   'b4b4')
insert into tb values('b',    5,   'b5b5b5b5b5')
go--一、按name分组取val最大的值所在行的数据。
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          3           a3:a的第三个值
b          5           b5b5b5b5b5
*/--二、按name分组取val最小的值所在行的数据。
--方法1:
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
--方法3:
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          1           a1--a的第一个值
b          1           b1--b的第一个值
*/--三、按name分组取第一次出现的行所在的数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          2           a2(a的第二个值)
b          1           b1--b的第一个值
*/--四、按name分组随机取一条数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          1           a1--a的第一个值
b          5           b5b5b5b5b5
*/--五、按name分组取最小的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          1           a1--a的第一个值
a          2           a2(a的第二个值)
b          1           b1--b的第一个值
b          2           b2b2b2b2
*/--六、按name分组取最大的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          2           a2(a的第二个值)
a          3           a3:a的第三个值
b          4           b4b4
b          5           b5b5b5b5b5
*/
--七,如果整行数据有重复,所有的列都相同。
/*
数据如下:
name val memo
a    2   a2(a的第二个值)
a    1   a1--a的第一个值
a    1   a1--a的第一个值
a    3   a3:a的第三个值
a    3   a3:a的第三个值
b    1   b1--b的第一个值
b    3   b3:b的第三个值
b    2   b2b2b2b2
b    4   b4b4
b    5   b5b5b5b5b5
*/
--在sql server 2000中只能用一个临时表来解决,生成一个自增列,先对val取最大或最小,然后再通过自增列来取数据。
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a',    2,   'a2(a的第二个值)')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('b',    1,   'b1--b的第一个值')
insert into tb values('b',    3,   'b3:b的第三个值')
insert into tb values('b',    2,   'b2b2b2b2')
insert into tb values('b',    4,   'b4b4')
insert into tb values('b',    5,   'b5b5b5b5b5')
goselect * , px = identity(int,1,1) into tmp from tbselect m.name,m.val,m.memo from
(
  select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
) m where px = (select min(px) from
(
  select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
) n where n.name = m.name)drop table tb,tmp/*
name       val         memo
---------- ----------- --------------------
a          1           a1--a的第一个值
b          1           b1--b的第一个值(2 行受影响)
*/
--在sql server 2005中可以使用row_number函数,不需要使用临时表。
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a',    2,   'a2(a的第二个值)')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('b',    1,   'b1--b的第一个值')
insert into tb values('b',    3,   'b3:b的第三个值')
insert into tb values('b',    2,   'b2b2b2b2')
insert into tb values('b',    4,   'b4b4')
insert into tb values('b',    5,   'b5b5b5b5b5')
goselect m.name,m.val,m.memo from
(
  select * , px = row_number() over(order by name , val) from tb
) m where px = (select min(px) from
(
  select * , px = row_number() over(order by name , val) from tb
) n where n.name = m.name)drop table tb/*
name       val         memo
---------- ----------- --------------------
a          1           a1--a的第一个值
b          1           b1--b的第一个值(2 行受影响)
*/

解决方案 »

  1.   

    insert into t_scadaTMeas values('2007/11/20 01:01:00',    1,   1, 'a1')
    insert into t_scadaTMeas values('2007/11/20 01:01:03',    3,   1, 'a1')
    insert into t_scadaTMeas values('2007/11/20 01:59:03',    3,   1, 'a1')insert into t_scadaTMeas values('2007/11/20 05:01:00',    3,   1, 'a1')
    insert into t_scadaTMeas values('2007/11/20 05:01:03',    2,   1, 'a1')
    insert into t_scadaTMeas values('2007/11/20 05:59:03',    1,   1, 'a1')---------
    为什么是这样分组的?
      

  2.   

    我需要统计,某个时段,例如一天中每个小时,或一个月中每一天的:某个实时采集到的物理量,例如:电压,电流,功率等,
    在该时段的平均值,最大值、最大值出现的时间,最小值、最小值出现的时刻;以前在SQL2000中,用子查询方式做过统计。
    但,据说如果使用SQL 2005则可以使用更简洁的语法,并获得性能的提升。因此有此一问。
    小梁贴的东西是SQL2000,我已经在过去有答案。
      

  3.   

    max() over()
    min() over()
      

  4.   

    create table t_scadaTMeas
    (
        dtScadaTime  datetime              not null,
        nAloge       float                 null    ,
        iEquimentId  integer               not null,
        cMeascode    character(8)          not null,
        constraint PK_T_SCADAHOURMEAS primary key (dtScadaTime, iEquimentId, cMeascode)
    )
    go
    insert into t_scadaTMeas values('2007/11/20 01:01:00',    1,   1, 'a1')
    insert into t_scadaTMeas values('2007/11/20 01:01:03',    3,   1, 'a1')
    insert into t_scadaTMeas values('2007/11/20 01:59:03',    3,   1, 'a1')insert into t_scadaTMeas values('2007/11/20 05:01:00',    3,   1, 'a1')
    insert into t_scadaTMeas values('2007/11/20 05:01:03',    2,   1, 'a1')
    insert into t_scadaTMeas values('2007/11/20 05:59:03',    1,   1, 'a1')insert into t_scadaTMeas values('2007/11/20 03:11:00',    1,   2, 'a2')
    insert into t_scadaTMeas values('2007/11/20 03:21:03',    2,   2, 'a2')
    insert into t_scadaTMeas values('2007/11/20 03:59:03',    1,   2, 'a2')insert into t_scadaTMeas values('2007/11/20 23:01:00',    3,   3, 'a3')
    insert into t_scadaTMeas values('2007/11/20 23:01:03',    3,   3, 'a3')
    insert into t_scadaTMeas values('2007/11/20 23:59:03',    3,   3, 'a3')
    ;
    with cte as
    (
       select cMeascode ,iEquimentId,
              ScadaTime=convert(varchar(13),dtScadaTime,120)+':00:00',
              max_val=max(nAloge) over(partition by cMeascode ,iEquimentId,convert(varchar(13),dtScadaTime,120)),
              time_at_max=max(dtScadaTime) over(partition by cMeascode ,iEquimentId,convert(varchar(13),dtScadaTime,120)),
              min_val=min(nAloge) over(partition by cMeascode ,iEquimentId,convert(varchar(13),dtScadaTime,120)),
              time_at_min=min(dtScadaTime) over(partition by cMeascode ,iEquimentId,convert(varchar(13),dtScadaTime,120))
       from t_scadaTMeas
    )
    select distinct * from ctedrop table t_scadaTMeas/*
    cMeascode iEquimentId ScadaTime           max_val                time_at_max             min_val                time_at_min
    --------- ----------- ------------------- ---------------------- ----------------------- ---------------------- -----------------------
    a1        1           2007-11-20 01:00:00 3                      2007-11-20 01:59:03.000 1                      2007-11-20 01:01:00.000
    a1        1           2007-11-20 05:00:00 3                      2007-11-20 05:59:03.000 1                      2007-11-20 05:01:00.000
    a2        2           2007-11-20 03:00:00 2                      2007-11-20 03:59:03.000 1                      2007-11-20 03:11:00.000
    a3        3           2007-11-20 23:00:00 3                      2007-11-20 23:59:03.000 3                      2007-11-20 23:01:00.000(4 行受影响)
    */
      

  5.   

    === 要求的查询结果
    cMeascode   iEquimentId   ScadaTime               max_val   time_at_max             min_val   time_at_min
    a1          1             '2007/11/20 01:00:00'         3   '2007/11/20 01:01:03'         1    '2007/11/20 01:01:00'
    a1          1             '2007/11/20 05:00:00'         3   '2007/11/20 05:01:00'         1    '2007/11/20 05:59:03'
    a2          2             '2007/11/20 03:00:00'         2   '2007/11/20 03:21:03'         1    '2007/11/20 03:11:00'
    a1          1             '2007/11/20 23:00:00'         3   '2007/11/20 23:01:00'         3    '2007/11/20 23:01:00'=== 实际的查询结果
    a1       1 2007-11-20 01:00:00 3 2007-11-20 01:59:03.000 1 2007-11-20 01:01:00.000
    a1       1 2007-11-20 05:00:00 3 2007-11-20 05:59:03.000 1 2007-11-20 05:01:00.000
    a2       2 2007-11-20 03:00:00 2 2007-11-20 03:59:03.000 1 2007-11-20 03:11:00.000
    a3       3 2007-11-20 23:00:00 3 2007-11-20 23:59:03.000 3 2007-11-20 23:01:00.000问题是:最大值出现的时刻,最小值出现的时刻的实际查询结果不对。
    这里查询出来的是最大时间和最小时间。(非最值出现的时刻对应的时间)