Solved

SQL Sum

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

708 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