Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
T-SQL (SS2K5)
»
calculating ratios
calculating ratios
Rate Topic
Display Mode
Topic Options
Author
Message
michaelmfranz
michaelmfranz
Posted Friday, February 15, 2013 9:06 PM
Forum Newbie
Group: General Forum Members
Last Login: Wednesday, February 20, 2013 5:47 AM
Points: 3,
Visits: 43
Hello, I am trying to calculate a ratio, i am pretty sure the problem is in the line * SUM([SUMTOTALVOLUME])) FROM #t) *
I am trying to do the ratio by TradeHour, but judging by how small the ratio numbers in my result set [RatioVolume] (dont add up to 1.0) it looks like the
SUM([SUMTOTALVOLUME]) is across all trade hours (TradeHour) rather than the trade hour I want it to be over. What changes do i need to make to fix that? TIA
SELECT TradeHour ,AVG([NumCodes]) as [AVGNUMCODES] ,[MCLower] ,[MCUpper] ,SUM([TotalVolume]) AS [SUMTOTALVOLUME] ,(SUM([TotalVolume]) / AVG([NumCodes]) ) as [AVGVOLUME]
INTO #t FROM [dbo].[EODVolumeStats] GROUP BY TradeHour, [MCLower],[MCUpper]
SELECT #t.TradeHour ,#t.[MCLower] ,#t.[MCUpper] ,CONVERT(DECIMAL(18,4),[SUMTOTALVOLUME]/(SELECT CONVERT(DECIMAL(18,4),SUM([SUMTOTALVOLUME])) FROM #t)) as [RatioVolume] FROM #t
GROUP BY #t.TradeHour, #t.SUMTOTALVOLUME, #t.MCLower, #t.MCUpper DROP TABLE #t
Post #1420855
dwain.c
dwain.c
Posted Sunday, February 17, 2013 6:21 PM
SSCrazy
Group: General Forum Members
Last Login: 2 days ago @ 7:24 PM
Points: 2,346,
Visits: 3,192
In looking at this, I don't think you need to use a temp table. However in order to show you how, you'd need to provide:
- DDL for your [EODVolumeStats] table
- Some consumable sample data
- Expected results
Having said that, if the only issue you're facing is that the summary is occuring over all trade hours of the day, and you only want it over specific hours, perhaps something like this can be used to trim the time period down to an hour.
DATEADD(hour, DATEDIFF(hour, 0, TradeHour), 0)
No loops! No CURSORs! No RBAR! Hoo-uh!
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1421018
michaelmfranz
michaelmfranz
Posted Monday, February 18, 2013 4:36 PM
Forum Newbie
Group: General Forum Members
Last Login: Wednesday, February 20, 2013 5:47 AM
Points: 3,
Visits: 43
Thank you for your interest, much apreciated. Yes you are right about temp tables, but i am a mere mortal compared to some here!
TradeHour represents hours of the day
MCLower & MCUpper represent a range of Market capitalisations in millions, the ranges do not overlap
Volume represents trading volume of shares
The ratios I am trying to calculate use EOD data (both historial and intraday hour snapshots) and the number of shares a company has to calculate a ratio so elevated trading volume can be determined by mulitplication
So i need a matrix of Tradehour, MC range and a ratio multiplier
In the morning i want to know if the current volume is unusual. I know by the end of the day(comparing one day to the next), but i want to work that out intraday. Thats why am gathering stats across the market to work out what is high/low trading, because activity isnt uniform throughout the day in each market segment (blue chip, microcpap etc)
Im limited to EOD data , I have already done this with Course of Sales data (with great help from this forum BTW)
I can do this easily in .net code, but not SQL
Post #1421374
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.