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
Solved

SQL Server Subquery

Posted on 2013-06-12
3
260 Views
Last Modified: 2013-06-12
I need some help restructuring this query so it does not bring in TotalSales that = 0. I end up with gaps in the data I would like to eliminate.



SELECT			pd.StoreID, st.ShortName, pd.Hour, ROUND(SUM(pd.TaxableSales) + SUM(pd.TaxExemptSales), - 2) AS TotalSales

FROM            POSDataDetailByHour AS pd 
				INNER JOIN FiscalDatesByDay AS fd ON fd.TransactionDate = pd.TransactionDate 
				INNER JOIN Stores AS st ON st.StoreID = pd.StoreID

WHERE			(pd.TransactionDate BETWEEN '6-11-13' AND '6-12-13')

GROUP BY		 pd.StoreID, st.ShortName, pd.Hour

Open in new window

0
Comment
Question by:dk04
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 39242648
SELECT pd.StoreID, st.ShortName, pd.Hour, 
    ROUND(SUM(pd.TaxableSales) + SUM(pd.TaxExemptSales), - 2) AS TotalSales
FROM POSDataDetailByHour AS pd INNER JOIN 
    FiscalDatesByDay AS fd ON fd.TransactionDate = pd.TransactionDate INNER JOIN 
    Stores AS st ON st.StoreID = pd.StoreID
WHERE (pd.TransactionDate BETWEEN '6-11-13' AND '6-12-13')
GROUP BY pd.StoreID, st.ShortName, pd.Hour
HAVING ROUND(SUM(pd.TaxableSales) + SUM(pd.TaxExemptSales), - 2) <> 0

Open in new window

0
 
LVL 8

Expert Comment

by:didnthaveaname
ID: 39242652
try:

SELECT			pd.StoreID, st.ShortName, pd.Hour, ROUND(SUM(pd.TaxableSales) + SUM(pd.TaxExemptSales), - 2) AS TotalSales

FROM            POSDataDetailByHour AS pd 
				INNER JOIN FiscalDatesByDay AS fd ON fd.TransactionDate = pd.TransactionDate 
				INNER JOIN Stores AS st ON st.StoreID = pd.StoreID

WHERE			(pd.TransactionDate BETWEEN '6-11-13' AND '6-12-13')

GROUP BY		 pd.StoreID, st.ShortName, pd.Hour
having
    ROUND(SUM(pd.TaxableSales) + SUM(pd.TaxExemptSales), - 2) > 0

Open in new window

0
 

Author Closing Comment

by:dk04
ID: 39242662
Thank you. It worked great!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

792 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