Solved

SELECT with IF Else and multiple date ranges

Posted on 2011-09-26
2
266 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

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

This article describes some very basic things about SQL Server filegroups.
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…

707 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

18 Experts available now in Live!

Get 1:1 Help Now