我的查询效果很好但有一个问题,它会显示我不想要的其他数据。
数据是每个用户名的最新数据。
以下是查询:
SELECT
l.USER_KEY AS id,
l.USER_ID AS username,
gl1.CHAR_KEY AS char_id,
gl1.NAME AS charname,
gl1.GATENUM AS server,
CONVERT(VARCHAR(20), l.LOGINTIME, 100) AS user_time,
CONVERT(VARCHAR(20), gl1.OCCUR_TIME, 100) AS char_time
FROM LOG_CONNECT201211 AS gl1
JOIN game.dbo.CHAR_INFOR AS g --character data
ON gl1.CHAR_KEY = g.CHAR0_KEY OR gl1.CHAR_KEY = g.CHAR1_KEY OR gl1.CHAR_KEY = g.CHAR2_KEY
JOIN login.dbo.USER_CHECK_LOGIN AS l --login data
ON g.USER_KEY = l.USER_KEY
JOIN (SELECT char_key, max(OCCUR_TIME) as mostrecent --game logs
FROM LOG_CONNECT201211
WHERE KIND=20 OR KIND=21
GROUP BY char_key) AS gl2
ON gl2.char_key = gl1.char_key and gl2.mostrecent = gl1.OCCUR_TIME
WHERE l.CHECKLOGIN = 1
ORDER BY username DESC
返回:
id username char_id name map user_time char_time
------------------------------------------------------------------------------------
3667 zr5970 11002 warpath 4 Nov 15 2012 8:54AM Nov 7 2012 6:31AM
3667 zr5970 11004 bloodfines 4 Nov 15 2012 8:54AM Nov 7 2012 6:33AM
3667 zr5970 11003 hanzhou 1 Nov 15 2012 8:54AM Nov 15 2012 8:54AM
14999 yvacosta 52086 Creams 1 Nov 15 2012 8:17AM Nov 15 2012 8:17AM
23433 yurich 1911481 abal 5 Nov 15 2012 8:34AM Nov 9 2012 4:05PM
23433 yurich 1911482 yurich 5 Nov 15 2012 8:34AM Nov 15 2012 8:30AM
23433 yurich 1911483 sharmaine 5 Nov 15 2012 8:34AM Nov 15 2012 8:35AM
10967 yubiwamoi 33376 Dwina 1 Nov 15 2012 4:33AM Nov 15 2012 4:33AM
所以数据是正确的,但我只想为每个用户名返回一行。
在这个数据上,用户名返回3,有3个名字,但我想要的唯一名字就是那个带有最新char_time的名字。
示例正确数据:
id username char_id name map user_time char_time
-----------------------------------------------------------------------------------
3667 zr5970 11003 hanzhou 1 Nov 15 2012 8:54AM Nov 15 2012 8:54AM
14999 yvacosta 52086 Creams 1 Nov 15 2012 8:17AM Nov 15 2012 8:17AM
23433 yurich 1911483 sharmaine 5 Nov 15 2012 8:34AM Nov 15 2012 8:35AM
10967 yubiwamoi 33376 Dwina 1 Nov 15 2012 4:33AM Nov 15 2012 4:33AM
请注意,我仅显示zr5970
的最新char_time
请指教,谢谢。
答案 0 :(得分:0)
首先只选择用户名和最长时间。这样,您将拥有正确的行数。如果有,您可以轻松地查询其他列...
答案 1 :(得分:0)
select a.USER_KEY AS id,
l.USER_ID AS username,
gl1.CHAR_KEY AS char_id,
gl1.NAME AS charname,
gl1.GATENUM AS server,
CONVERT(VARCHAR(20), l.LOGINTIME, 100) AS user_time,
CONVERT(VARCHAR(20), gl1.OCCUR_TIME, 100) AS char_time
from
(select user_key, max(occur_time) as mostrecent
from log_connect2011
WHERE KIND=20 OR KIND=21
group by user_key) a
join LOG_CONNECT201211 AS gl1 on a.user_key = gl1.user_key and a.mostrecent = gl1.occur_time
JOIN game.dbo.CHAR_INFOR AS g --character data
ON gl1.CHAR_KEY = g.CHAR0_KEY OR gl1.CHAR_KEY = g.CHAR1_KEY OR gl1.CHAR_KEY = g.CHAR2_KEY
JOIN login.dbo.USER_CHECK_LOGIN AS l --login data
ON g.USER_KEY = l.USER_KEY
WHERE l.CHECKLOGIN = 1
ORDER BY username DESC
答案 2 :(得分:0)
使用ROW_NUMBER()
SELECT *
FROM
(
SELECT
l.USER_KEY AS id,
l.USER_ID AS username,
gl1.CHAR_KEY AS char_id,
gl1.NAME AS charname,
gl1.GATENUM AS server,
CONVERT(VARCHAR(20), l.LOGINTIME, 100) AS user_time,
CONVERT(VARCHAR(20), gl1.OCCUR_TIME, 100) AS char_time,
rn = row_number() over (partition by l.USER_KEY order by gl1.OCCUR_TIME DESC)
FROM LOG_CONNECT201211 AS gl1
JOIN game.dbo.CHAR_INFOR AS g --character data
ON gl1.CHAR_KEY = g.CHAR0_KEY OR gl1.CHAR_KEY = g.CHAR1_KEY OR gl1.CHAR_KEY = g.CHAR2_KEY
JOIN login.dbo.USER_CHECK_LOGIN AS l --login data
ON g.USER_KEY = l.USER_KEY
WHERE l.CHECKLOGIN = 1
) X
WHERE rn=1
ORDER BY username DESC