Solved

MS SQL showing records where sum() is null

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

810 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