Solved

MS SQL showing records where sum() is null

Posted on 2014-02-06
5
323 Views
Last Modified: 2014-02-25
I have two tables, Location and Sales.  There are multiple sales for each location for each day.  I need to get the location to display even if there were no sales for that day, For example, with the winter weather some locations were closed do to the weather and they had no sales but I still want them to show up in the list.  Here is the script I am using and the data below the script is the results I am looking for.


select l.LocationId
      ,isnull(t.SaleDate,'2014-02-05')
      ,SUM(t.soldprice)
from Location l
      Left Outer Join Sales s on s.LocationId = l.LocationId
where s.TransDate = '2014-02-05'
group by l.StoreId, s.TransDate
order by l.StoreId

Location    Sales
1                125.60
2                346.78
3                0              (they were closed)
4                247.93
5                367.76

Thanks for the help.

John
0
Comment
Question by:j_heck
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 39839991
select l.LocationId
      ,isnull(t.SaleDate,'2014-02-05')
      ,SUM(t.soldprice)
from Location l
      Left Outer Join Sales s on s.LocationId = l.LocationId
where s.TransDate = '2014-02-05'
group by l.StoreId, s.TransDate
HAVING SUM(t.soldPrice) > 0
order by l.StoreId
0
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 39840024
select l.LocationId
      ,isnull(t.SaleDate,'2014-02-05')
      ,SUM(isnull(t.soldprice,0))
from Location l
      Left Outer Join Sales s on s.LocationId = l.LocationId
where s.TransDate = '2014-02-05'
group by l.StoreId, s.TransDate
order by l.StoreId

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 39840079
Since s.TransDate is part of the Sales table, having it in the WHERE condition will simulate an INNER JOIN.  Therefore, you need to do something like this.
select l.LocationId
      ,isnull(s.SaleDate,'2014-02-05')
      ,SUM(isnull(s.soldprice,0))
from Location l
left outer join Sales s 
  on s.LocationId = l.LocationId
 and s.TransDate = '2014-02-05'
group by l.StoreId, s.TransDate
order by l.StoreId

Open in new window


If you need multiple dates, you can CROSS JOIN a table of dates to your locations, then use both in the criteria of the OUTER JOIN/APPLY similar to my example above with the literal date.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39840377
select l.LocationId
      ,isnull(t.SaleDate,'2014-02-05')
      ,SUM(t.soldprice)
from Location l
      Left Outer Join Sales s on s.LocationId = l.LocationId
where (s.TransDate = '2014-02-05' or s.TransDate IS NULL)
group by l.StoreId, s.TransDate
order by l.StoreId
0
 
LVL 32

Accepted Solution

by:
bhess1 earned 500 total points
ID: 39840525
The most common method I know of to do this is to move your date check into the WHERE clause of the Left Join table.  So, your query would become:

select l.LocationId
      ,isnull(t.SaleDate,'2014-02-05')
      ,SUM(t.soldprice)
from Location l
      Left Outer Join Sales s 
          on s.LocationId = l.LocationId
          AND s.TransDate = '2014-02-05'
group by l.StoreId, s.TransDate
order by l.StoreId

Open in new window

0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now