Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Adding Groups in a select statement (help)
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

35 Posts

Posted - 07/08/2014 :  16:58:32  Show Profile  Reply with Quote
Thanks for helping!

The select statement below works fine but just to a limited extend.

There is just one table (prices) with 3 fields (ticker, close, date).
I use this select statement to get monthly returns for 1 ticker.

My question is: can I get this same statement for multiple tickers? I guess I have to group by ticker but I wouldn´t know how to do it.
Can anyone help?
Many thanks,

--Get Monthly Returns As Percentage
WITH PriceMonthly_CTE
row_number() OVER (ORDER BY DATE_) AS Period,
datepart(yyyy,date_) AS YEAR,
datepart(mm,date_) AS MONTH
from Prices where date_ IN (
--get last pricedate of each period
select MAX(Date_) as a
FROM Prices
group by datepart(yyyy,date_),datepart(mm,date_)
a.Year, a.Month,
CONVERT(NUMERIC(18,4),100*(a.Close_- b.Close_)/b.Close_) as [Monthly Return Percent],
a.Close_ AS [Closing Price]
from PriceMonthly_CTE a
JOIN PriceMonthly_CTE b
ON a.Period = b.Period+1
WHERE a.Year >=2010

Almighty SQL Goddess

38200 Posts

Posted - 07/08/2014 :  17:02:48  Show Profile  Visit tkizer's Homepage  Reply with Quote

Tara Kizer
SQL Server MVP since 2007
Go to Top of Page

Starting Member

35 Posts

Posted - 07/08/2014 :  17:37:05  Show Profile  Reply with Quote
ok, thanks
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000