Solved

SELECT with IF Else and multiple date ranges

Posted on 2011-09-26
2
267 Views
Last Modified: 2012-05-12
Hello,

I have a database with the following info:
Date
Ticker
Price
SharesOutstanding
dSO (change in shares outstanding)

Definitions:
AUM (Assests under management): (Price * SharesOutstanding) AS AUM
Flows (Change in AUM): (Price * dSO) AS Flows

I am trying to pull the following for a list of tickers in a single query.

Assets as of 08/31/2011 (this would be pulling the AUM of 8/31/2011)
Flows from 01/01/2010 – 08/31/2010 (SUM(Flows) over date range)
Flows from 01/01/2011 – 08/31/2011
Flows from 08/01/2011 – 08/31/2011

I believe that I would do this by pulling the data for my earliest date to my last date and then creating columns with calculation in them. However I am not sure how to do this.

Any ideas?
0
Comment
Question by:KOvitt
2 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36602321
I believe that I would do this by pulling the data for my earliest date to my last date and then creating columns with calculation in them...

Exactly. That is called conditional aggregates. You may wish to make this based dynamic (formulaic) dates later, but for now using static values you listed, this may look something like this:

SELECT /*...,*/ SUM(CASE WHEN [Date] >= '20100101' AND [Date] < '20100901' THEN (Price * dSO) ELSE 0 END) AS FlowsPriorYTD
FROM your_table
WHERE [Date] >= '20100101' AND [Date] < '20110901'
;

Hope that helps!
0
 

Author Closing Comment

by:KOvitt
ID: 36710089
Worked like a charm, Thank you soo much!! You are quite the SQL guru :D
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

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

911 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

23 Experts available now in Live!

Get 1:1 Help Now