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 7857117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:56:01+00:00 2026-06-02T20:56:01+00:00

I have table: id | uid | action —————– 1 | 1 | 2

  • 0

I have table:

id | uid | action
-----------------
 1 |  1  |   2
 2 |  3  |   1
 3 |  2  |   2
 4 |  3  |   2
 5 |  1  |   1

How can I select uid’s with last action for example 1? In this case only last line.

UPDATE: As you can see for uid=3 at first we have action=1, then action=2 it menas that uid=3 is not “good” line for us. But for uid=1 last action is 1, so that “good” line.

UPDATE2: action=1 is enter and action=2 is exit. Finaly I need to get list of users who in building now, or at least list of last action of every user.

  • 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-02T20:56:02+00:00Added an answer on June 2, 2026 at 8:56 pm

    Edit: I added some explanations at the end of my answer.

    DECLARE @Test TABLE(
        id INT, 
        [uid] INT, 
        [action] INT
    );
    
    INSERT  @Test(id , [uid] , [action])
    SELECT  1 ,  1  ,   2
    UNION ALL
    SELECT  2 ,  3  ,   1
    UNION ALL
    SELECT  3 ,  2  ,   2
    UNION ALL
    SELECT  4 ,  3  ,   2
    UNION ALL
    SELECT  5 ,  1  ,   1;
    
    DECLARE @last_action INT;
    SET     @last_action = 1; --or = 2
    SELECT  x.uid
    FROM
    (
    SELECT  t.uid, 
            t.action,
            ROW_NUMBER() OVER(PARTITION BY t.[uid] ORDER BY t.id DESC) AS RowNum
    FROM    @Test t
    ) x
    WHERE   x.RowNum = 1
    AND     x.action = @last_action 
    

    Results:

    uid         
    ----------- 
    1          
    

    Explanations:
    The query from derived table (...) x will produce folowing results:

    id uid action RowNum
    -- --- ------ ------
    5  1   1      1 <-- last action (1) for uid = 1
    1  1   2      2
    -- --- ------ ------
    3  2   2      1 <-- last action (2) for uid = 2
    -- --- ------ ------
    4  3   2      1 <-- last action (2) for uid = 3
    2  3   1      2
    -- --- ------ ------
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this statement in T-SQL. SELECT Bay From TABLE where uid in (
Can someone help me with this query. I have this table for sample: FILE
I have this table structure: CREATE TABLE users ( uid bigint NOT NULL, first_name
I have a table that looks like this uid gid score 1 a 5
I have user table and payment is many to many relationships table. SELECT user.uID,
I have the following table: UID | Limit |Type 8 | 4 |A 8
I have a table 'articles' : id / uid / last_update / content. I
I have the following table (points): recno uid uname points ============================ 1 a abc
I have table with 3 columns A B C. I want to select *
I have a table for image gallery with four columns like: foid | uid

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.