Author |
Topic |
ArunPhilip
Starting Member
21 Posts |
Posted - 2012-09-18 : 14:58:07
|
Hi Everyone,I am stuck and need help. I have written a query that returns the count of types based on code. In the result there are few records that have the same code but different types, for such records I need to add up the count and display it as another column. Any help in this matter is much appriciated.SELECT Count(a.TypeID)CountOfTypes,b.[TypeName], a.Code FROM [Sets] a LEFT JOIN [Types] b ON a.[TypeID] = b.[TypeID]WHERE AppCode <> 'ABC' AND AssetAppCode <> 'DEF' ANDa.TypeID IN (54,57) AND a.LocationID=64 Group By a.Code,b.[TypeName] I also tried to use a derived table, but didnt help.SELECT SUM(X.CountOfTypes)as SUMPERAPP,X.[TypeName],X.CountOfTypesFROM(SELECT Count(a.TypeID)CountOfTypes,b.[TypeName], a.Code FROM [Sets] a LEFT JOIN [Types] b ON a.[TypeID] = b.[TypeID]WHERE AppCode <> 'ABC' AND AssetAppCode <> 'DEF' ANDa.TypeID IN (54,57) AND a.LocationID=64 Group By a.Code,b.[TypeName]) X Group By X.[TypeName],X.Code Order By X.Code |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-18 : 15:09:00
|
post some data and explain what you want as output. otherwise we cant understand what you're asking for!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
ArunPhilip
Starting Member
21 Posts |
Posted - 2012-09-18 : 15:53:12
|
My Expected output is,Count Type Code Sum2 R ABC 2 S ABC 4 3 T DEF 2 U GHI 3 V JKL 2 W JKL 5 |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-18 : 15:53:40
|
where's the sample data?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
ArunPhilip
Starting Member
21 Posts |
Posted - 2012-09-18 : 16:00:01
|
Expected OutputCount Type Code Sum2 R ABC 2 S ABC 4 3 T DEF 2 U GHI 3 V JKL 2 W JKL 5quote: Sample Data In Table TypeType Id Type Name1 R2 S3 T4 U5 V6 WSample Data In Table CodeId CODE Type ID1 ABC 12 ABC 23 DEF 34 GHI 45 JKL 56 JKL 6
|
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-18 : 16:11:36
|
can you explain how you got those count and sum values?as per data i cant understand how you got 3 etc as counts------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
ArunPhilip
Starting Member
21 Posts |
Posted - 2012-09-18 : 17:11:54
|
I appolagize for the confusion,When I run the querySELECT Count(a.TypeID)CountOfTypes,b.[TypeName], a.Code FROM [Sets] a LEFT JOIN [Types] b ON a.[TypeID] = b.[TypeID]WHERE AppCode <> 'ABC' AND AssetAppCode <> 'DEF' ANDa.TypeID IN (1,2) AND a.LocationID=64 Group By a.Code,b.[TypeName] Here is the data that I get,1 Server EFG2 Server RRR2 Desktop ABC4 Server ABCI would like to get a 4th column which will give me the sum for only those records that have the same Code name.1 Server EFG 2 Server RRR 2 Desktop ABC 4 Server ABC 6Does this help? |
 |
|
ArunPhilip
Starting Member
21 Posts |
Posted - 2012-09-18 : 17:26:32
|
I have written 2 queries,Query 1SELECT Count(a.HardwareTypeID)CountOfHWTypes,b.[HardwareTypeName], a.AssetAppCode FROM Assets a LEFT JOIN HardwareTypes b ON a.[HardwareTypeID] = b.[HardwareTypeID] WHERE AssetAppCode <> 'SH00' AND AssetAppCode <> 'SH01' ANDa.HardwareTypeID IN (54,57) AND a.CurrentLocationID=64 GROUP BY a.AssetAppCode,b.[HardwareTypeName] Order By a.AssetAppCode Query 2SELECT Count(HardwareTypeID)SumOfHWType,AssetAppCode FROM Assets WHERE AssetAppCode <> 'SH00' AND AssetAppCode <> 'SH01' AND HardwareTypeID IN (54,57) AND CurrentLocationID=64 Group by AssetAppCode Is there a way I can achive this just by using one query where I have SumOfHWType as the 4th column. This might sound lame, but I am really stuck. Any help is much appreciated |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-18 : 17:28:27
|
[code]SELECT CountOfTypes,[TypeName], Code,CASE WHEN Seq>1 THEN SUM(CountOfTypes) OVER (PARTITION BY [Code]) END AS Col4FROM(SELECT ROW_NUMBER() OVER (PARTITION BY a.Code ORDER BY b.[TypeName]) AS Seq, Count(a.TypeID)CountOfTypes,b.[TypeName], a.Code FROM [Sets] a LEFT JOIN [Types] b ON a.[TypeID] = b.[TypeID]WHERE AppCode <> 'ABC' AND AssetAppCode <> 'DEF' ANDa.TypeID IN (1,2) AND a.LocationID=64 Group By a.Code,b.[TypeName])t[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
ArunPhilip
Starting Member
21 Posts |
Posted - 2012-09-18 : 17:45:42
|
Thanks visakh I will give it a try and let you know. Thanks once again for the help. |
 |
|
ArunPhilip
Starting Member
21 Posts |
Posted - 2012-09-18 : 22:06:30
|
Appreciate the help and support. This is the exact solution that I was looking for. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-19 : 12:08:16
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|