日期:2014-05-16  浏览次数:20405 次

SQL语句的聚合问题
举例:

   数量      时间        内码1   内码2   
1    30     2011-3-3     1992      1
2    20     2011-6-5     1992      1
3    22     2012-5-1     1993      2
4    11     2013-5-6     1991      1


想要得的结果是:

   数量      时间     内码1      内码2
1     50          2011-6-5     1992         1
2     22          2012-5-1     1993         2
3     11          2013-5-6      1991        1  

实现结果: 想把 内码1和内码2 相同的数量合并,并且获取最新的时间 形成一条新数据;
求大神写个SQL语句!  T  T  !纠结了好久了!
------解决方案--------------------
要是表里没ID

create 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,1

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