?
Solved

SELECT with IF Else and multiple date ranges

Posted on 2011-09-26
2
Medium Priority
?
272 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 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

752 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