Solved

SQL Sum

Posted on 2013-01-08
2
212 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

813 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

16 Experts available now in Live!

Get 1:1 Help Now