日期:2014-05-18  浏览次数:20540 次

sql group by语句
select   a,b,sum(money)as   money,max(str)as   str   from   table1   group   by   a,b


由于str是字符行的,我想得到的是最大长度的str

------解决方案--------------------
select a,b,sum(money)as money,MAX(LEN(str))as str from table1 group by a,b
------解决方案--------------------
select a,b,sum(money)as money,MAX(LEN(str))as str from table1 group by a,b

------解决方案--------------------
select a,b,money,str from table1 a where len(str)=
(
select max(len(str)) from table1
)
------解决方案--------------------
create table table1(a varchar(10),b varchar(10),money int,[str] varchar(100))
insert table1 select 'a ', 'b ',10, '123456 '
union all select 'b ', 'a ',20, '1234567 '
union all select 'c ', 'a ',20, '12345678 '
union all select 'd ', 'a ',20, '23456789 '


select a,b,sum(money)as money,(select top 1 str from table1 t2 where len(str)=(select max(len(str)) from table1 t3 where t3.a=t1.a and t3.b=t1.b ) and t2.a=t1.a and t2.b=t1.b)

from table1 t1 group by a,b
------解决方案--------------------
suifix()的正确