分组记录一列

时间:2015-07-23 18:57:35

标签: sql tsql sql-server-2012

我正在使用SQL Server 2012.我有这样的表:

Val1 Val2      Val3 Val4
-------------------------------------------
1   25000.00    1   1900-01-01 00:00:00.000
2   25000.00    1   2012-04-02 00:00:00.000
1   25125.00    1   2013-01-01 00:00:00.000
1   25502.00    1   2014-01-01 00:00:00.000
2   25502.00    1   2014-04-01 00:00:00.000
3   25502.00    1   2015-01-01 00:00:00.000
4   25502.00    1   2015-04-01 00:00:00.000
1   62500.00    2   1900-01-01 00:00:00.000
2   62500.00    2   2012-06-29 00:00:00.000
1   63750.00    2   2013-01-01 00:00:00.000
1   65025.00    2   2014-01-01 00:00:00.000
1   69250.00    2   2015-01-01 00:00:00.000
1   4300.00 3   1900-01-01 00:00:00.000
2   4300.00 3   2012-05-01 00:00:00.000
1   4343.00 3   2013-01-01 00:00:00.000
2   4343.00 3   2013-06-01 00:00:00.000
3   4343.00 3   2013-09-01 00:00:00.000
4   4343.00 3   2014-04-01 00:00:00.000
5   4343.00 3   2014-09-01 00:00:00.000
1   3257.25 3   2014-09-15 00:00:00.000
2   3257.25 3   2015-03-01 00:00:00.000
1   4543.00 3   2015-04-01 00:00:00.000

我想得到这样的东西:

 Val1 Val2      Val3 Val4
 ----------------------------------------------
    2   25000.00    1   2012-04-02 00:00:00.000
    1   25125.00    1   2013-01-01 00:00:00.000
    4   25502.00    1   2015-04-01 00:00:00.000
    2   62500.00    2   2012-06-29 00:00:00.000
    1   63750.00    2   2013-01-01 00:00:00.000
    1   65025.00    2   2014-01-01 00:00:00.000
    1   69250.00    2   2015-01-01 00:00:00.000
    2   4300.00 3   2012-05-01 00:00:00.000
    5   4343.00 3   2014-09-01 00:00:00.000
    2   3257.25 3   2015-03-01 00:00:00.000
    1   4543.00 3   2015-04-01 00:00:00.000

我尝试了这个SQL查询:

select 
    t1.* 
from 
    table1 t1 
inner join 
    (select 
         Val3,  max(Val1) as MaxRank
     from 
         table1 
     group by 
         Val3) t13 on t1.Val3 = t13.Val3 and t1.Val1 = t13.MaxRank 
order by 
    t1.Val3

但我只有Val3的一条记录。

3 个答案:

答案 0 :(得分:0)

这样的事情应该这样做(这是在mysql中,但在任何sql数据库中都适用相同的原则):

use example;

drop table if exists table1;

create table table1(
    Val1 varchar(100),
    Val2 varchar(100),
    Val3 varchar(100)
);

insert into table1 values('1', '100.00', '1');
insert into table1 values('2', '100.00', '2');
insert into table1 values('2', '200.00', '2');
insert into table1 values('3', '100.00', '2');
insert into table1 values('3', '200.00', '2');
insert into table1 values('3', '300.00', '2');
insert into table1 values('4', '100.00', '4');
insert into table1 values('4', '200.00', '4');
insert into table1 values('4', '300.00', '4');
insert into table1 values('4', '300.00', '4');


select
    val1,
    max(val2)
from
    table1
group by val1
order by val1
;


+ --------- + -------------- +
| val1      | max(val2)      |
+ --------- + -------------- +
| 1         | 100.00         |
| 2         | 200.00         |
| 3         | 300.00         |
| 4         | 300.00         |
+ --------- + -------------- +
4 rows

答案 1 :(得分:0)

也许这有助于你:

select t1.* 
from table1 t1
    join (
           select max(Val1) as Val1, Val2, Val3 
           from table1 
           group by Val2, Val3
         ) t2 on t1.Val1=t2.Val1 and t1.Val2 = t2.Val2 and t1.Val3=t2.Val3

答案 2 :(得分:0)

这应该有效

SELECT
    val1,
    val2,
    val3,
    val4
FROM
    (SELECT *,
            ROW_NUMBER () OVER (PARTITION BY val2 ORDER BY val1 DESC) rn 
     FROM  table1 
    ) a
WHERE
    rn = 1