--ID,大赛名称,开始时间,结束时间
SELECT ID,MatchName,StartTime,EndTime FROM ds_Match--大赛表
--ID,大赛ID,参赛用户ID,积分
SELECT ID,Match_ID,UserID,Integral FROM ds_Match_Online--参赛记录表
--ID,用户名
SELECT ID,UserName FROM ds_UserInfo--用户表
请问上面三个表如何能查出参加过两次以上比赛的用户,并且从中筛选两次比赛最高的积分进行汇总统计.
------解决思路----------------------
SELECT UserName,SUM(T3.Integral)SUMIntegral FROM ds_UserInfo T1
CROSS APPLY(SELECT TOP 2 Integral FROM ds_Match_Online T2 WHERE T2.UserID=T1.ID ORDER BY Integral DESC)T3
GROUP BY UserName
HAVING COUNT(T3.Integral)=2
------解决思路----------------------
SELECT B.UserName,max(A.Integral)
FROM ds_Match_Online A,ds_UserInfo B
where A.UserID=B.ID
group by A.UserID
having count(1)>1