SQLServerCentral.com / SQL Server 2005 / T-SQL (SS2K5) / calculating ratios / Latest PostsInstantForum.NET v99.99.99SQLServerCentral.comhttp://www.sqlservercentral.com/Forums/notifications@sqlservercentral.comSat, 22 Oct 2016 06:28:32 GMT20RE: calculating ratioshttp://www.sqlservercentral.com/Forums/Topic1420855-338-1.aspxThank 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 dayMCLower & MCUpper represent a range of Market capitalisations in millions, the ranges do not overlapVolume represents trading volume of sharesThe 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 mulitplicationSo i need a matrix of Tradehour, MC range and a ratio multiplierIn 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 SQLMon, 18 Feb 2013 16:36:51 GMTmichaelmfranzRE: calculating ratioshttp://www.sqlservercentral.com/Forums/Topic1420855-338-1.aspxIn 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 resultsHaving 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.[code="sql"]DATEADD(hour, DATEDIFF(hour, 0, TradeHour), 0)[/code]Sun, 17 Feb 2013 18:21:31 GMTdwain.ccalculating ratioshttp://www.sqlservercentral.com/Forums/Topic1420855-338-1.aspxHello, 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? TIASELECT 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 #tFri, 15 Feb 2013 21:06:56 GMTmichaelmfranz