Solved

MS SQL showing records where sum() is null

Posted on 2014-02-06
5
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 60

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

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)

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard 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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

724 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