Solved

SQL Sum

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

Suggested Solutions

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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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…

732 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