Author:zfive5(zhaozidong)
Email :zfive5@yahoo.com.cn
这几天被一个支持问题烦死了,虽说结果正确,但用户需要顺序与原表也一样
问题大概是这样的:
table1
Auto F_1 F_2 F_3
1 "1" "1" 1
2 "3" "3" 2
3 "2" "2" 3
4 "2" "2" 4
5 "5" "5" 5
select f_1, f_2,sum(f_3) from table1 group by f_1,f_2,f_3
输出:
"1" ,"1" ,1
"2" ,"2" ,7
"3" ,"3" ,2
"5" ,"5" ,5
这样以来就改变了在表里顺序,客户在需要这样的输出:
"1" ,"1" ,1
"3" ,"3" ,2
"2" ,"2" ,7
"5" ,"5" ,5
一句sql语句就可以搞定了(这可以是一上午的想到的,昨天想用Distinct 和 关联查询都没有实现):
select f_1,f_2,f_3 from (select Min(Auto),f_1,f_2,f_3 from table2 group by f_1,f_2,f_3)
"1" ,"1" ,1
"3" ,"3" ,2
"2" ,"2" ,7
"5" ,"5" ,5
豁然开朗!