| Author |
Topic |
|
zion99
Posting Yak Master
141 Posts |
Posted - 2008-11-21 : 05:36:18
|
| [code]Declare @Inc_Count table(incID int null,typeID int null,deptDESC VARCHAR (20) null)insert into @Inc_Count (incID,typeID,deptDESC)select 5238, 3, 'DEPT A' union allselect 5245, 3, 'DEPT A' union allselect 5258, 3, 'DEPT B' union allselect 5266, 4, 'DEPT C' union allselect 5281, 1, 'DEPT A' union allselect 5301, 3, 'DEPT B' union allselect 5306, 3, 'DEPT A' union allselect 5306, 2, 'DEPT A' union allselect 5306, 4, 'DEPT A' union allselect 5306, 2, 'DEPT A' union allselect 5309, 1, 'DEPT C'select * from @Inc_Count ORDER BY deptDesc, typeid[/code]i need to generate data on the basis of departments with a particular formula - [code]typeID Mult. factor1 502 313 184 1Final output required:deptDesc Score'DEPT A' (50*1 + 31*2 + 18*3 + 1*1)'DEPT B' (50*0 + 31*0 + 18*2 + 1*0)'DEPT C' (50*1 + 31*0 + 18*0 + 1*1)[/code]i start with getting the count of all typeID for each department into a temporary table & then it sum it upto get the score; creating 3 tables for this purposecan anybody please suggest me a better approach to do this? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-21 : 06:08:55
|
is this what you wantDeclare @Inc_Count table(incID int null,typeID int null,deptDESC VARCHAR (20) null)DEclare @Mul_Factor table(typeID int null,Factor int null)insert into @Inc_Count (incID,typeID,deptDESC)select 5238, 3, 'DEPT A' union allselect 5245, 3, 'DEPT A' union allselect 5258, 3, 'DEPT B' union allselect 5266, 4, 'DEPT C' union allselect 5281, 1, 'DEPT A' union allselect 5301, 3, 'DEPT B' union allselect 5306, 3, 'DEPT A' union allselect 5306, 2, 'DEPT A' union allselect 5306, 4, 'DEPT A' union allselect 5306, 2, 'DEPT A' union allselect 5309, 1, 'DEPT C'insert into @Mul_Factorselect 1,50 union allselect 2,31 union allselect 3,18 union allselect 4,1 ;With CTE (incID,typeID,deptDESC,typeCnt) AS(select i.incID,i.typeID,i.deptDESC,count(*) over (partition by i.deptDESC,mf.typeID) * mf.Factor AS typeCntfrom @Inc_Count iINNER JOIN @Mul_Factor mfON mf.typeID=i.typeID)SELECT *FROM CTE c1INNER JOIN (SELECT deptDESC,SUM(DISTINCT typeCnt) AS deptCount FROM CTE GROUP BY deptDESC)c2ON c1.deptDESC=c2.deptDESCoutput-------------------------------------incID typeID deptDESC typeCnt deptDESC deptCount5281 1 DEPT A 50 DEPT A 1675306 2 DEPT A 62 DEPT A 1675306 2 DEPT A 62 DEPT A 1675306 3 DEPT A 54 DEPT A 1675238 3 DEPT A 54 DEPT A 1675245 3 DEPT A 54 DEPT A 1675306 4 DEPT A 1 DEPT A 1675301 3 DEPT B 36 DEPT B 365258 3 DEPT B 36 DEPT B 365309 1 DEPT C 50 DEPT C 515266 4 DEPT C 1 DEPT C 51 replace * with only columns you want |
 |
|
|
zion99
Posting Yak Master
141 Posts |
Posted - 2008-11-21 : 08:49:38
|
thanks visakh16, it works great had a small doubt. i tried using WITH CTE2 ... in place of INNER JOIN, but it didnt work. any ideas?quote: SELECT *FROM CTE c1INNER JOIN (SELECT deptDESC,SUM(DISTINCT typeCnt) AS deptCount FROM CTE GROUP BY deptDESC)c2ON c1.deptDESC=c2.deptDESC
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-21 : 08:56:00
|
quote: Originally posted by zion99 thanks visakh16, it works great had a small doubt. i tried using WITH CTE2 ... in place of INNER JOIN, but it didnt work. any ideas?quote: SELECT *FROM CTE c1INNER JOIN (SELECT deptDESC,SUM(DISTINCT typeCnt) AS deptCount FROM CTE GROUP BY deptDESC)c2ON c1.deptDESC=c2.deptDESC
you need join with CTE here. With CTE is used for defining the CTE and here you need it to be joined. its like a temporary table which is available for one time use after creation. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-21 : 08:56:34
|
| Refer about CTEs in link below to understand how they workhttp://msdn.microsoft.com/en-us/library/ms190766(SQL.90).aspx |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-21 : 09:13:50
|
Why overcomplicate things?-- Prepare sample dataDECLARE @Master TABLE ( incID INT, typeID TINYINT, deptDesc VARCHAR(20) )INSERT @Master ( incID, typeID, deptDesc )SELECT 5238, 3, 'Dept A' UNION ALLSELECT 5245, 3, 'Dept A' UNION ALLSELECT 5258, 3, 'Dept B' UNION ALLSELECT 5266, 4, 'Dept C' UNION ALLSELECT 5281, 1, 'Dept A' UNION ALLSELECT 5301, 3, 'Dept B' UNION ALLSELECT 5306, 3, 'Dept A' UNION ALLSELECT 5306, 2, 'Dept A' UNION ALLSELECT 5306, 4, 'Dept A' UNION ALLSELECT 5306, 2, 'Dept A' UNION ALLSELECT 5309, 1, 'Dept C'DECLARE @Factor TABLE ( typeID TINYINT, factValue MONEY )INSERT @Factor ( typeID, factValue )SELECT 1, 50 UNION ALLSELECT 2, 31 UNION ALLSELECT 3, 18 UNION ALLSELECT 4, 1-- Here is the final required outputSELECT m.deptDesc, SUM(f.factValue) AS ScoreFROM @Master AS mINNER JOIN @Factor AS f ON f.typeID = m.typeIDGROUP BY m.deptDescORDER BY m.deptDesc E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|
|