Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8835779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:20:17+00:00 2026-06-14T09:20:17+00:00

I have a query that works well but has one problem, it displays other

  • 0

I have a query that works well but has one problem, it displays other data I don’t want.

The data is the latest of each username.

Here is the query:

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

That returns:

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

So the data is correct, but I only want to return one row per username.

On this data the username returns 3, with 3 names, but the only name I want is that one with the latest char_time.

Example correct data:

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

Notice that I only displayed the data for zr5970 with the latest char_time

Please advice, Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:20:18+00:00Added an answer on June 14, 2026 at 9:20 am

    Use 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this linq query that works well (although it may be written better,
I have the following query that works well. SELECT DISTINCT city,region1,region2 from static_geo_world where
I have written a SQL query that works just fine, but am having a
I am using in C# MYsql .I have query that works if I run
So I have this query that works perfectly: SELECT users.*, GROUP_CONCAT(categories.category_name) AS categories FROM
I have a SQL query that I'm trying to debug. It works fine for
I have a an query that I want to work on so I formatted
I have a problem with an sql query that will not work if I
I have a query that currently returns data with the following attributes: A number
i have some problems with a Query seem IN dosen't work with Group_concat, that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.