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 Sum

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

856 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