表 cid g_fen username
11 50 001
12 20 001
11 30 002
12 40 002
11 10 003
12 30 003求一条SQL语,可以统计出所有用户CID 为 11,12 字段g_fen和,结果按字段G_fen排序
11 50 001
12 20 001
11 30 002
12 40 002
11 10 003
12 30 003求一条SQL语,可以统计出所有用户CID 为 11,12 字段g_fen和,结果按字段G_fen排序
USERNAME,
CID,
SUM(G_FEN) AS [TATAL]
FROM [TABLENAME]
GROUP BY USERNAME,CID
ORDER BY 3 -- DESC
from tble
where cid in ('11','12')
group by cid,username
order by sum(g_fen)
FROM TB
GROUP BY USERNAME,CID
ORDER BY G_FEN(DESC)
From Table_list
Where cid = 11 or cid = 12
Group by cid
Order By G_fen
go
create table t(cid int,g_fen int,username varchar(04))
insert into t
select 11,50,'001' union all
select 12,20,'001' union all
select 11,30,'002' union all
select 12,40,'002' union all
select 11,10,'003' union all
select 12,30,'003' union all
select 11,80,'004' union all
select 12,100,'005'select A.username,sum(B.g_fen) as G_fen
from
(select username from t where cid=12
and username in (select username from t where cid=11)) A
left join t B
on A.username=B.username
group by A.username
drop table t/*
username G_fen
-------- -----------
001 70
002 70
003 40
*/