Solved

SELECT with IF Else and multiple date ranges

Posted on 2011-09-26
2
270 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
[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 60

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
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…

696 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