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

  • Home
  • SEARCH
  • 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 6617481
In Process

The Archive Base Latest Questions

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

I have a SQL query which pulls unit sales by item, by week: SELECT

  • 0

I have a SQL query which pulls unit sales by item, by week:

SELECT sls_vendor, 
   sls_item, 
   sls_units, 
   DATEPART(week, sls_week) AS sls_date 
FROM   mytable 

Assume I’m looking at a 8 week period, but not every item/vendor combination has a full 8 weeks of sales. However I need my query to show a null value in that instance. So the query would return 8 rows for each item/vendor combination regardless of existence.

I tried creating a temp table which has the numbers 28 to 35 and performing a left join on the query above, but that doesn’t show null values. The results are no different than running the original query alone.

I can think of how this would be done using a crosstab/pivot query, but isn’t this something the join should be doing?

Edit: Updated to show my join query. Datetable just has 8 rows with 1 incremental number for each calendar week.

SELECT * FROM @datetable
    LEFT JOIN
    (SELECT 
        sls_vendor,
        sls_item,
        sls_units,
        datepart(week,sls_week) AS sls_date
    FROM mytable) AS QRY
    ON temp_week = qry.sls_date
  • 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:42:36+00:00Added an answer on May 25, 2026 at 8:42 pm

    Your method should work just fine:

    ;with mytable as (
        select 1 as sls_vendor, 'Test' as sls_item, 30 as sls_units, '8/7/2011' as sls_week union
        select 1 as sls_vendor, 'Test' as sls_item, 30 as sls_units, '8/14/2011' as sls_week union
        select 1 as sls_vendor, 'Test' as sls_item, 30 as sls_units, '8/21/2011' as sls_week
    )
    ,datetable as (
        select 28 as temp_week union
        select 29 union
        select 30 union
        select 31 union
        select 32 union
        select 33 union
        select 34 union
        select 35
    )
    SELECT * FROM datetable
    
        LEFT JOIN
    
        (SELECT 
            sls_vendor,
            sls_item,
            sls_units,
            datepart(week,sls_week) AS sls_date
        FROM mytable) AS QRY
    
        ON temp_week=qry.sls_date
    

    Output:

    temp_week   sls_vendor  sls_item    sls_units   sls_date
    28          NULL        NULL        NULL        NULL
    29          NULL        NULL        NULL        NULL
    30          NULL        NULL        NULL        NULL
    31          NULL        NULL        NULL        NULL
    32          NULL        NULL        NULL        NULL
    33          1           Test        30          33
    34          1           Test        30          34
    35          1           Test        30          35
    

    Edit: If you want to include all week values for every sales vendor, include a cross join with the distinct selection of vendors:

    ;with mytable as (
        select 1 as sls_vendor, 'Test' as sls_item, 30 as sls_units, '8/7/2011' as sls_week union
        select 2 as sls_vendor, 'Test' as sls_item, 30 as sls_units, '8/14/2011' as sls_week union
        select 3 as sls_vendor, 'Test' as sls_item, 30 as sls_units, '8/21/2011' as sls_week
    )
    ,datetable as (
        select 28 as temp_week union
        select 29 union
        select 30 union
        select 31 union
        select 32 union
        select 33 union
        select 34 union
        select 35
    )
    SELECT * FROM datetable
    cross join (select distinct sls_vendor from mytable) v
    
        LEFT JOIN
    
        (SELECT 
            sls_vendor,
            sls_item,
            sls_units,
            datepart(week,sls_week) AS sls_date
        FROM mytable) AS QRY
    
        ON temp_week=qry.sls_date and v.sls_vendor=qry.sls_vendor
    

    Output:

    temp_week   sls_vendor  sls_vendor  sls_item    sls_units   sls_date
    28          1           NULL        NULL        NULL        NULL
    29          1           NULL        NULL        NULL        NULL
    30          1           NULL        NULL        NULL        NULL
    31          1           NULL        NULL        NULL        NULL
    32          1           NULL        NULL        NULL        NULL
    33          1           1           Test        30          33
    34          1           NULL        NULL        NULL        NULL
    35          1           NULL        NULL        NULL        NULL
    28          2           NULL        NULL        NULL        NULL
    29          2           NULL        NULL        NULL        NULL
    30          2           NULL        NULL        NULL        NULL
    31          2           NULL        NULL        NULL        NULL
    32          2           NULL        NULL        NULL        NULL
    33          2           NULL        NULL        NULL        NULL
    34          2           2           Test        30          34
    35          2           NULL        NULL        NULL        NULL
    28          3           NULL        NULL        NULL        NULL
    29          3           NULL        NULL        NULL        NULL
    30          3           NULL        NULL        NULL        NULL
    31          3           NULL        NULL        NULL        NULL
    32          3           NULL        NULL        NULL        NULL
    33          3           NULL        NULL        NULL        NULL
    34          3           NULL        NULL        NULL        NULL
    35          3           3           Test        30          35
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL SELECT query which has a LIKE clause containing an underscore,
I have the following which works in SQL Query Analyzer . select oh.* from
Can we have a SQL query which will basically help in viewing table and
Right now I have this SQL query which is valid but always times out:
I have an Informix SQL query which returns a set of rows. It was
I have SQL query, which is working nice on Oracle and MSSQL. Now I'm
i've got a SQL query which returns multiple rows, and i have : $data
I have been asked in an interview to write a SQL query which fetches
Strange performance outcome, I have a LINQ to SQL query which uses several let
I have the following Linq to SQL query, in which I'm trying to do

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.