[code="sql"]USE tempdb-- Table ScriptCREATE TABLE TestTable( ID INT, Period CHAR(10), ProjectID INT, Proportion NUMERIC(10,4) )-- Insert Data in TableINSERT INTO TestTableSELECT 3338 ,'Dec 2014',301, 1550.0000UNION ALLSELECT 3338 ,'Dec 2014',2118, 1240.0000UNION ALLSELECT 3338 ,'Dec 2014',2026, 310.0000UNION ALLSELECT 58842 ,'Dec 2014',346, 775.0000UNION ALLSELECT 58842 ,'Dec 2014',534, 2325.0000SELECT * FROM TestTable-- Query for Result I.e Preparing Data in a query that i want to make using a Query. ( Only to be used for ResultSet ).SELECT * FROM (SELECT ID, Period, ProjectID, Proportion, 1240.0000 AS OtherProportionInResultSet1, 310 AS OtherProportionInResultSet2FROM TestTableWHERE ID=3338 AND ProjectID=301UNION ALLSELECT ID, Period, ProjectID, Proportion, 1550.0000 AS OtherProportion1, 310 AS OtherProportion2FROM TestTableWHERE ID=3338 AND ProjectID=2118UNION ALLSELECT ID, Period, ProjectID, Proportion, 1550.0000 AS OtherProportion1, 1240 AS OtherProportion2FROM TestTableWHERE ID=3338 AND ProjectID=2026UNION ALLSELECT ID, Period, ProjectID, Proportion, 2325.0000 AS OtherProportion1, 0.0000 AS OtherProportion2FROM TestTableWHERE ID=58842 AND ProjectID=346UNION ALLSELECT ID, Period, ProjectID, Proportion, 775.0000 AS OtherProportion1, 0.0000 AS OtherProportion2FROM TestTableWHERE ID=58842 AND ProjectID=534)A[/code]Required Result Explanation :[quote] For ID : 3338 ,there are 3 rows. And Result Set Includes rest of the two Valued for that ID. Likewise, for ID=58842, there are 2 rows. ( this can be implemented through Lead and lag function.but, since, original Table has Many ID and Each Id may have N number of Rows window for it, hence need to frame a query that will do it for all ID. [/quote][quote]Formula has to like this:Proportion / ( Proportion + OtherPropotionInResultSet1 + OtherPropotionInResultSet2 + OtherPropotionInResultSet(n) ) calculated for each ID , for each row[/quote]
↧