Solved

Missing record in SQL Query

Posted on 2014-12-29
3
259 Views
Last Modified: 2014-12-29
I have the following sql query and the results are missing one day 11/30/14.  I need to have a day show up even if the sales table doesn't include a record for that day, which is the case for this date.  I have verified that the Calendar table does have a 11/30/14 record and I thought I had the joins correct so that the record will show, but to no avail.   Any suggestions?  I have a feeling the problem is associated with the location table, which is how the location name is standardized.  

SELECT     CASE WHEN Locations.[Location Name] IS NULL THEN @Location ELSE Locations.[Location Name] END AS Location, Calendar.Dt, 
	                      CASE WHEN Sales.[Net Sales] IS NULL THEN 0 ELSE Sales.[Net Sales] END AS CurrentSales, LEFT(DATENAME(dw, Calendar.Dt), 3) AS DayName, DATEPART(isowk, 
	                      Calendar.Dt) AS WeekNum
	FROM         Locations LEFT OUTER JOIN
	                      Sales ON Locations.LocationNameOrig = Sales.[Location Name] RIGHT OUTER JOIN
	                      Calendar ON Sales.Date = Calendar.Dt
	WHERE     (Locations.[Location Name] = @Location) AND (Calendar.Dt BETWEEN @Begin AND @End) OR
	                      (Locations.[Location Name] IS NULL) AND (Calendar.Dt BETWEEN @Begin AND @End)
	ORDER BY Calendar.Dt

Open in new window

0
Comment
Question by:rcowen00
  • 2
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40522369
If you wish every date in the Calendar table to appear in the return set, then you'll need the Calendar table on the LEFT side of the JOIN..

FROM Calendar
   LEFT JOIN Sales ON ...
   JOIN Locations ON ...

Open in new window

btw if you want to compare notes on Calendar tables check out my article on Building your own SQL calendar table to perform complex date expressions
0
 

Author Comment

by:rcowen00
ID: 40522385
aaaahhhh so stupid.  Thank you!
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40522476
Anytime.  Far better to ask us stupid questions then ask your co-workers and have them wonder why you're asking stupid questions.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It is helpful to note: This is a cosmetic update and is not required, but should help your reports look better for your boss.  This issue has manifested itself in SSRS version 3.0 is where I have seen this behavior in.  And this behavior is only see…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question