﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / T-SQL (SS2K5)  / Calculate Weighted Median / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 18 May 2013 11:40:19 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Calculate Weighted Median</title><link>http://www.sqlservercentral.com/Forums/Topic875503-338-1.aspx</link><description>Ok, here's what I came up with.It is imporant to understand the quirky update concept as well as the requirements that need to be fulfilled. So, I urge you to carefully read the article Chris mentioned (the same article is referenced in my sample code, too...)  [code="sql"]SET NOCOUNT ON--- original dataCREATE TABLE #dr_temp([PK] [INT] IDENTITY(1,1) NOT NULL,[ID] [INT] NOT NULL,Code [NVARCHAR](10) NOT NULL,[VALUE] [INT] NOT NULL,[Volume] [INT] NOT NULL)INSERT INTO #dr_temp(ID,Code,VALUE,Volume) VALUES(1,'120',40,30778)INSERT INTO #dr_temp(ID,Code,VALUE,Volume) VALUES(1,'120',40,24860)INSERT INTO #dr_temp(ID,Code,VALUE,Volume) VALUES(1,'120',40,82043)INSERT INTO #dr_temp(ID,Code,VALUE,Volume) VALUES(1,'120',41,136116)INSERT INTO #dr_temp(ID,Code,VALUE,Volume) VALUES(1,'120',42,106886)INSERT INTO #dr_temp(ID,Code,VALUE,Volume) VALUES(1,'120',45,179646)-- column added to hold RunningTotal values ALTER TABLE #dr_temp ADD RunningTotal INT-- add clustered index to guarantee the specific order for the running totalCREATE CLUSTERED INDEX IX_#dr_temp_ID_Volume ON #dr_temp (ID,PK); -- perform the "quirky update"-- for details, please see Jeff Modens related article: -- Link: http://www.sqlservercentral.com/articles/T-SQL/68467/DECLARE @PrevID       INTDECLARE @RunningTotal INTUPDATE #dr_temp     SET 	@RunningTotal = RunningTotal = 		CASE			WHEN ID = @PrevID THEN @RunningTotal + Volume	ELSE Volume		END,	@PrevID = ID   FROM #dr_temp  WITH (TABLOCKX) OPTION (MAXDOP 1);WITH cte AS -- calculate the median per ID(	SELECT 		pk,		ID, 		MedianDiff = (RunningTotal - (MAX(RunningTotal) OVER (PARTITION BY id))/2.0 )	FROM #dr_temp), cte2 AS -- find the PK values holding values larger than the median and number thos PK's(	SELECT  		pk AS pkSub,		ID, 		ROW_NUMBER() OVER(PARTITION BY id ORDER BY pk ) AS ROW	FROM   cte 	WHERE MedianDiff &amp;gt; 0)-- final output: values of the row that holds the 2nd value larger than the medianSELECT  #dr_temp.*FROM   cte2 	INNER JOIN #dr_temp	ON cte2.id = #dr_temp.id		AND pksub=pkWHERE ROW=2DROP TABLE #dr_temp[/code]</description><pubDate>Wed, 03 Mar 2010 11:20:08 GMT</pubDate><dc:creator>LutzM</dc:creator></item><item><title>RE: Calculate Weighted Median</title><link>http://www.sqlservercentral.com/Forums/Topic875503-338-1.aspx</link><description>Since you're using a temp table - you will want to add a clustered index in order to make the running totals work.You should start by reading Jeff's article on the matter (so you can understand why the clustered index is such a hot topic all of a sudden):  [url=http://www.sqlservercentral.com/articles/T-SQL/68467/]http://www.sqlservercentral.com/articles/T-SQL/68467/[/url]Also since you're using temp tables, you should pre-aggregate the data so that there is ONE record per code+value combination.  Referring back to your example records, having multiple likes of code=160, value= 40 would make like substantially more complicated than necessary. Praggregating would also allow you to know what your target number is on the running total.</description><pubDate>Wed, 03 Mar 2010 10:54:17 GMT</pubDate><dc:creator>Matt Miller (#4)</dc:creator></item><item><title>RE: Calculate Weighted Median</title><link>http://www.sqlservercentral.com/Forums/Topic875503-338-1.aspx</link><description>Hi,There is no index because this is a temp table to find the Median value per Code. Hope this clears.Thanks</description><pubDate>Wed, 03 Mar 2010 10:09:43 GMT</pubDate><dc:creator>ashok_raja</dc:creator></item><item><title>RE: Calculate Weighted Median</title><link>http://www.sqlservercentral.com/Forums/Topic875503-338-1.aspx</link><description>[quote][b]ashok_raja (3/3/2010)[/b][hr]Hi Apoligize for the confusion..Here is what I wantID      CODE    Value   Volume  RunningTotal    Average1       120     40      30778   30778   1       120     40      24860   55638   1       120     40      82043   137681  1       120     41      136116  273797  1       120     42      106886  380683  1       120     45      179646  560329          280164.5Since 280164.5 falls b/w 273797 and 380683, the result set should be  1       120     42      106886  380683 Should be able to find median b/w the running total...Hope this clears...Thanks[/quote]With your reply, none of my questions in my previous post has been answered. :crying:Hard to help...</description><pubDate>Wed, 03 Mar 2010 10:06:53 GMT</pubDate><dc:creator>LutzM</dc:creator></item><item><title>RE: Calculate Weighted Median</title><link>http://www.sqlservercentral.com/Forums/Topic875503-338-1.aspx</link><description>Hi Apoligize for the confusion..Here is what I wantID      CODE    Value   Volume  RunningTotal    Average1       120     40      30778   30778   1       120     40      24860   55638   1       120     40      82043   137681  1       120     41      136116  273797  1       120     42      106886  380683  1       120     45      179646  560329          280164.5Since 280164.5 falls b/w 273797 and 380683, the result set should be  1       120     42      106886  380683 Should be able to find median b/w the running total...Hope this clears...Thanks</description><pubDate>Wed, 03 Mar 2010 09:47:04 GMT</pubDate><dc:creator>ashok_raja</dc:creator></item><item><title>RE: Calculate Weighted Median</title><link>http://www.sqlservercentral.com/Forums/Topic875503-338-1.aspx</link><description>[quote][b]ashok_raja (3/2/2010)[/b][hr]Hi,Actually 46 will not be there as the next row will be for different ID i.eInsert into dr_temp(ID,Code,Value,Volume) Values(2,'120',31,2311)if present then Yes! it matches, I think Let's get the 1st match  B/w 106886 and 380683. [b]Is this Possible.[/b]Thanks[/quote]Yes.Do you have a clustered index on your table and if yes, what columns are included?Is there any correlation between the clustered index and the volume column? (eg. if I sort the table based on the clustered index column, will the values for the volume column per ID be in order as well?)</description><pubDate>Tue, 02 Mar 2010 14:53:15 GMT</pubDate><dc:creator>LutzM</dc:creator></item><item><title>RE: Calculate Weighted Median</title><link>http://www.sqlservercentral.com/Forums/Topic875503-338-1.aspx</link><description>Hi,Actually 46 will not be there as the next row will be for different ID i.eInsert into dr_temp(ID,Code,Value,Volume) Values(2,'120',31,2311)if present then Yes! it matches, I think Let's get the 1st match  B/w 106886 and 380683. Is this Possible.Thanks</description><pubDate>Tue, 02 Mar 2010 14:41:45 GMT</pubDate><dc:creator>ashok_raja</dc:creator></item><item><title>RE: Calculate Weighted Median</title><link>http://www.sqlservercentral.com/Forums/Topic875503-338-1.aspx</link><description>[quote][b]ashok_raja (3/2/2010)[/b][hr]Hi,Yep we can use the last one.Can this done in a SQL Query? Thanks[/quote]It depends.Usually, a SQL query will rely on some logic requirement(s). If both results are possible, which one has to be used as a base to determin "the next row"?Let's assume your next row would look like the following (ignore the wrong math. I didn't bother to calculate the new RunningTotal value).[code="plain"]ID      CODE    Value   Volume  RunningTotal  1       120     46      379646  660329    [/code]      If we would use the logic from your previous statements, the the result would be 45.But if we'd also consider your last statement, the result would be either 45 or 46.That's possible to do with SQL as well. But the question is: What is the [b]correct [/b]answer? And [b]why[/b]?</description><pubDate>Tue, 02 Mar 2010 14:35:33 GMT</pubDate><dc:creator>LutzM</dc:creator></item><item><title>RE: Calculate Weighted Median</title><link>http://www.sqlservercentral.com/Forums/Topic875503-338-1.aspx</link><description>Hi,Yep we can use the last one.Can this done in a SQL Query? Thanks</description><pubDate>Tue, 02 Mar 2010 14:20:07 GMT</pubDate><dc:creator>ashok_raja</dc:creator></item><item><title>RE: Calculate Weighted Median</title><link>http://www.sqlservercentral.com/Forums/Topic875503-338-1.aspx</link><description>It still is not really clear:[quote]280164.5 falls in the range b/w 106886 and 380683.[/quote]But it also falls in the range b/w 179646 and 560329. Why not use that one?</description><pubDate>Tue, 02 Mar 2010 14:16:31 GMT</pubDate><dc:creator>LutzM</dc:creator></item><item><title>RE: Calculate Weighted Median</title><link>http://www.sqlservercentral.com/Forums/Topic875503-338-1.aspx</link><description>Hi,Thanks for the replyHere is the business rule280164.5 falls in the range b/w 106886 and 380683However the ending range i.e 380683 &amp;gt; than 280164.5, due to which we need to select the next column which matched the criteria.However this might not be happening to othe Code'sHope this helps.And oops! :ermm: Column Mismatched.Thanks</description><pubDate>Tue, 02 Mar 2010 14:03:28 GMT</pubDate><dc:creator>ashok_raja</dc:creator></item><item><title>RE: Calculate Weighted Median</title><link>http://www.sqlservercentral.com/Forums/Topic875503-338-1.aspx</link><description>Would you mind elaborate the business case?Especially the part [quote]falls b/w 106886 and 380683 but 380683 is greater than 280164.5 so the value is 45.[/quote]Why do you compare the median value to a single Value (106886) instead of the previous running toal (273797)?Also, whydo you use the value of the next row that is larger than the row that already exceeded your median value? (I would expect the target value to be 42, not 45).Finally, your sample data don't match your result set (value and volume are mixed).Please clarify.</description><pubDate>Tue, 02 Mar 2010 13:54:58 GMT</pubDate><dc:creator>LutzM</dc:creator></item><item><title>Calculate Weighted Median</title><link>http://www.sqlservercentral.com/Forums/Topic875503-338-1.aspx</link><description>Hi, Could anyone please help me in writing a query to find the weighted Median for the following data  CREATE TABLE [dbo].[dr_temp](	[PK] [int] IDENTITY(1,1) NOT NULL,	[ID] [int] NOT NULL,	[Code] [nvarchar](10) NOT NULL,	[Value] [int] NOT NULL,	[Volume] [int] NOT NULL	)insert into dr_temp(ID,Code,Volume,Value) values(1,'120',40,30778)insert into dr_temp(ID,Code,Volume,Value) values(1,'120',40,24860)insert into dr_temp(ID,Code,Volume,Value) values(1,'120',40,82043)insert into dr_temp(ID,Code,Volume,Value) values(1,'120',41,136116)insert into dr_temp(ID,Code,Volume,Value) values(1,'120',42,106886)insert into dr_temp(ID,Code,Volume,Value) values(1,'120',45,179646)[code="vb"]ID	CODE	Value	Volume	RunningTotal	Average1	120	40	30778	30778	1	120	40	24860	55638	1	120	40	82043	137681	1	120	41	136116	273797	1	120	42	106886	380683	1	120	45	179646	560329		[b]280164.5[/b][/code]I'm expectiing the Value to be 45... HowCumulative/2 value is 280164.5 which actually falls b/w 106886 and 380683 but 380683 is greater than 280164.5 so the value is 45.Hope this helps.Kindly help me out .If this is possible in SSIS also please let me know.For any questions do reply me.Thanks</description><pubDate>Tue, 02 Mar 2010 13:31:13 GMT</pubDate><dc:creator>ashok_raja</dc:creator></item></channel></rss>