Solved

SQL Sum

Posted on 2013-01-08
2
209 Views
Last Modified: 2013-01-08
select I.InventoryID,
SUM(CASE WHEN
((getdate()+7) between D.BeginDate and D.EndDate)
THEN ROUND (I.Qty,0,0) ELSE 0 END) as 'Column1'
SUM(CASE WHEN
((getdate()+14) between D.BeginDate and D.EndDate)
THEN ROUND (I.Qty,0,0) ELSE 0 END) as 'Column2'
From Inventory I
INNER JOIN Date D
WHERE D.DateID=I.DateID
GROUP BY I.InventoryID

Open in new window


I need to create a view using the select above.  Can I aggregate the results of Column1 and Column2 into a Column3 without using a temp table or variable?  I also don't want to display Col1 or Col2.

The results of the above query look like this:

InventoryID     Column1     Column2
1                       6                 7

I need the query to result in:

InventoryID     Column3
1                       13
0
Comment
Question by:darrennelson
2 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 38756774
Why not just do this?

select I.InventoryID,
    SUM(CASE WHEN ((getdate()+7) between D.BeginDate and D.EndDate) OR
        ((getdate()+14) between D.BeginDate and D.EndDate)
        THEN ROUND (I.Qty,0,0) 
        ELSE 0 END) as 'Column3'
From Inventory I
INNER JOIN Date D
WHERE D.DateID=I.DateID
GROUP BY I.InventoryID

Open in new window

0
 

Author Closing Comment

by:darrennelson
ID: 38756802
thanks from a sql newb ;P
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

930 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now