Solved

MS SQL showing records where sum() is null

Posted on 2014-02-06
5
322 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
Comment Utility
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
Comment Utility
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
Comment Utility
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:ScottPletcher
Comment Utility
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
Comment Utility
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

11 Experts available now in Live!

Get 1:1 Help Now