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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:28:51+00:00 2026-05-25T20:28:51+00:00

I was using this SQL statement: SELECT dateId, userId, Salary FROM ( SELECT *,

  • 0

I was using this SQL statement:

SELECT "dateId", "userId", "Salary" 
FROM (
   SELECT *, 
          (row_number() OVER (ORDER BY "userId", "dateId"))%2 AS rn 
   FROM user_table
 ) sa 
 WHERE sa.rn=1 
   AND "userId" = 789 
   AND "Salary" > 0;

But every time the table gets new rows the result of the query is different.
Am I missing something?

  • 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-05-25T20:28:52+00:00Added an answer on May 25, 2026 at 8:28 pm

    Assuming that ("dateId", "userId") is unique and new rows always have a bigger (later) dateId.

    After some comments:

    What I think you need:

    SELECT "dateId", "userId", "Salary"
    FROM (
       SELECT "dateId", "userId", "Salary"
             ,(row_number() OVER (PARTITION BY "userId"   -- either this
                                  ORDER BY "dateId")) % 2 AS rn
       FROM   user_table
       WHERE  "userId" = 789                              -- ... or that
       ) sub
    WHERE  sub.rn = 1
    AND    "Salary" > 0;

    Notice the PARTITION BY. This way you skip every second dateId for each userId, and additional (later) rows don’t change the selection so far.

    Also, as long as you are selecting rows for a single userId (WHERE "userId" = 789), pull the predicate into the subquery, achieving the same effect (stable selection for a single user). You don’t need both.

    The WHERE clause in the subquery only works for a single user, PARTITION BY works for any number of users in one query.

    Is that it? Is it?
    They should give me "detective" badge for this.
    Seriously.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$select = $this->_db->select()->from($this->_name,array(id,fullname,username,email))->where(fullname LIKE '$query%'); I am using this SQL statement currently to power
I have this sql statement SELECT userID from users WHERE (name='name1' AND username='username1') OR
When using this SQL query: Select * from table_name what happens if the name
I have a problem with a SQL statement: Using this select a.id as ID,
Using this statement: select * from qvalues where rowid ,name,compound in ( select rowid,name,compound
I'm planning on creating a view using this SQL SELECT, but the explain for
I want to translate this simple SQL statement to linq-to-sql: SELECT SUM(field1), SUM(field2) FROM
I need the equivalent of this SQL statement in Linq, using method/fluent syntax. SELECT
For example this SQL statement returns 1k rows SELECT * FROM tableName WHERE someCondition
Why is the ( ) mandatory in the SQL statement select * from gifts

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.