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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:52:59+00:00 2026-06-01T01:52:59+00:00

I have a table which contains User IDs, Period IDs and Qty: User Period

  • 0

I have a table which contains User IDs, Period IDs and Qty:

User Period Qty
---- ------ ---
   1  11201   3
   1  11202   2
   1  11203   5
   2  11202   4
   2  11203   1

Note: User 2 started in Period 11202 so he/she does not have a row for Period 11201

I need to get the Users only from the latest Period…

SELECT User FROM table WHERE Period = 11203

…and then use these User IDs on the same table to get Period 11201 and 11202 Qty for both users.

The problem: I need to see the (null) Qty for User 2 in Period 11201. Like so:

User Period    Qty
---- ------  -----
   1  11201      3
   1  11202      2
   2  11201 (null)
   2  11202      4

I can’t figure out the query/joins to achieve it. So far I have this but predictably it gives me only 3 rows (User 1, Periods 11201/11202 and User 2, Period 11202):

SELECT a.User, a.Period, a.Qty
FROM (SELECT User FROM table WHERE Period = 11203) b
LEFT JOIN table a
ON b.User = a.User
WHERE a.Period BETWEEN 11201 AND 11202
  • 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-01T01:53:00+00:00Added an answer on June 1, 2026 at 1:53 am

    You need to get all the required periods and make a Cartesian product of these with the Users first, and then outer join to that:

    SELECT b.User, p.Period, a.Qty
    FROM (SELECT User FROM table WHERE Period = 11203) b
    CROSS JOIN (SELECT DISTINCT Period FROM table 
                WHERE Period BETWEEN 11201 AND 11202) p
    LEFT JOIN table a
    ON a.User = b.User
    AND a.Period = p.Period
    

    Note also the SELECT needs to get the User and Period from the outer table not the inner one, otherwise everything will be null on the rows where the user is missing.

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

Sidebar

Related Questions

I currently have a user's table which contains a one-to-one relationship for Youtube OAuth
I have a database named Tamaris, which contains a table User. I created a
I have one user table. and second language table which contains language that user
I have a table which contains user choices of a certain type... with each
This is probably a newbie question. I have a table USER which contains info
In my web-service i have a user table which contains column called HITS,now as
I have a table in Oracle where one of the column contains UserIds which
I have a table called order which contains columns id , user_id , price
I have a single product table with multiple fields which contain user evaluations of
I have table which contains double values stored in mysql database ...I need to

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.