Solved

SQL Server Subquery

Posted on 2013-06-12
3
263 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
[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
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

707 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