Solved

MS SQL showing records where sum() is null

Posted on 2014-02-06
5
329 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 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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