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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:16:18+00:00 2026-05-26T21:16:18+00:00

I am trying to join two tables in SQL, one containing a list of

  • 0

I am trying to join two tables in SQL, one containing a list of items, and the other with the dates they were sold. I got the join part down, but I need to get the information from the REPORT table, but all items in the VENDORS table. I need the items that were not sold to show up as NULL, or preferably 0. This is the code I’m using so far, and it’s only showing the items that were sold on the given day.

SELECT t2.[DATE]
      ,t1.[VENDOR]
      ,t1.[UPC]
      ,t2.[QTY]
      ,t2.[AMOUNT]
FROM [STORESQL].[dbo].[VENDORS] t1
LEFT OUTER JOIN [STORESQL].[dbo].[REPORT] t2 ON t1.UPC=t2.UPC
WHERE VENDOR='119828' AND DATE='2011-11-8'

and example of the tables are

VENDORS:

VENDOR   UPC
119828   1
119828   2
119828   3

REPORT:

DATE       UPC    QTY    AMOUNT
2011-11-8  1      1      9.99
2011-11-8  3      2      18.98

The current code is resulting in

DATE       VENDOR    UPC    QTY    AMOUNT
2011-11-8  119828    1      1      9.99
2011-11-8  119828    3      2      18.98

I need it to show

DATE       VENDOR    UPC    QTY    AMOUNT
2011-11-8  119828    1      1      9.99
2011-11-8  119828    2      0      0.00
2011-11-8  119828    3      2      18.98

I know i’m doing something wrong, but I don’t know what that is.
Thanks in advance.

  • 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-26T21:16:18+00:00Added an answer on May 26, 2026 at 9:16 pm

    Try this instead:

    SELECT t2.[DATE]
          ,t1.[VENDOR]
          ,t1.[UPC]
          ,t2.[QTY]
          ,t2.[AMOUNT]
    FROM [STORESQL].[dbo].[VENDORS] t1
    LEFT OUTER JOIN [STORESQL].[dbo].[REPORT] t2 ON t1.UPC=t2.UPC AND DATE='2011-11-8'
    WHERE VENDOR='119828'
    

    The where clause against DATE makes your outer join to function as an inner join because the comparison will be against a null value in DATE. Moving the comparison to the ON clause should deal with that.

    To get a 0 instead of null as a result you can use COALESCE:

    ,COALESCE(t2.[QTY], 0)
    ,COALESCE(t2.[AMOUNT], 0)
    

    Your date column will also be null and that can be fixed with COALESCE as well:

    COALESCE(t2.[DATE], '2011-11-8')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to LEFT JOIN two tables, to get a list of all rows
I'm trying to join two SQL tables, parent (which I have complete design control
I am trying to join two tables; the purpose being able to search and
I am trying to join together several audio files into one mp4/m4a file containing
I'm trying to join multiple tables, but one of the tables has multiple records
I have two tables that I am trying to join in a query, but
In SQL Server, I am trying to get the rows from the two tables
Another NHibernate JOIN problem. I'm trying to join two different properties from one table
I have two MySQL tables: One for items and another to log purchases. I'm
I am trying to select from the join of the two related tables in

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.