Author |
Topic |
mikesoper
Starting Member
11 Posts |
Posted - 2013-11-14 : 04:38:21
|
Hi,I have not used the PIVOT function before and need some help:My data is like this:CustID CustType CustValue1 Type1 102 Type2 13 Type2 99 4 Type1 5Could you assist me in using pivot to give meCustType CustCount CustValue(Sum)Type1 2 15Type2 2 100A simple example I hope will assist me in working further with this.Much appreciated |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-11-14 : 04:49:33
|
SELECT CustType , COUNT(CustID ) AS CustCount , SUM(CustValue) AS CustValueFROM TableNameGROUP BY CustType--Chandu |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
|
mikesoper
Starting Member
11 Posts |
Posted - 2013-11-14 : 04:54:35
|
OK thanks,I left out a bit:Each customer is assigned to an area, area 1, area 2, area 3 etc., and it is by area I need to do this aggregation, hence why I thought of PIVOT.So by area, what count of customer types and sum of customer values.Thanks |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-11-14 : 05:01:07
|
Is this? SELECT CustType , COUNT(CustID ) AS CustCount , SUM(CustValue) AS CustValueFROM TableNameGROUP BY CustType, areaIf NOT, post the sample data and expected output--Chandu |
|
|
mikesoper
Starting Member
11 Posts |
Posted - 2013-11-14 : 05:08:28
|
The other issue is that CustValue is nvarchar type, as some records contain "REMOVED"Can we also incorporate a CAST or CONVERT?Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-14 : 05:25:59
|
quote: Originally posted by mikesoper The other issue is that CustValue is nvarchar type, as some records contain "REMOVED"Can we also incorporate a CAST or CONVERT?Thanks
then convert them to NULL first to ignore them from sum calculation.something likeSELECT CustType , COUNT(CustID ) AS CustCount , SUM(NULLIF(CustValue,'REMOVED')) AS CustValueFROM TableNameGROUP BY CustType ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
mikesoper
Starting Member
11 Posts |
Posted - 2013-11-14 : 10:05:13
|
I get "Operand data type nvarchar is invalid for sum...." |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-14 : 13:18:58
|
quote: Originally posted by mikesoper I get "Operand data type nvarchar is invalid for sum...."
CAST it to int then------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-11-14 : 23:11:00
|
SELECT CustType , COUNT(CustID ) AS CustCount , SUM(CAST(NULLIF(CustValue,'REMOVED') AS INT)) AS CustValueFROM TableNameGROUP BY CustType--Chandu |
|
|
mikesoper
Starting Member
11 Posts |
Posted - 2013-11-26 : 08:36:57
|
Hi,Thanks this seems to be working. One of the reasons I mentionned a Pivot was because I also need to calculate the % of totals for each group, for example:CustID CustType CustValue % of total1 Type1 10 8%2 Type2 1 1%3 Type2 99 87%4 Type1 5 4%How could I acheive this?Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-26 : 09:28:50
|
[code]SELECT CustType ,CustCount ,CustValue,CustValue/SUM(CustValue) OVER () AS [% Of Total]FROM(SELECT CustType , COUNT(CustID ) AS CustCount , SUM(CAST(NULLIF(CustValue,'REMOVED') AS INT)) AS CustValueFROM TableNameGROUP BY CustType)t[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
mikesoper
Starting Member
11 Posts |
Posted - 2013-11-26 : 10:09:29
|
Thanks - I have tried this but the results for [% Of Total] are always 0? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-27 : 04:55:24
|
what about now?SELECT CustType ,CustCount ,CustValue,CustValue*100.0/SUM(CustValue) OVER () AS [% Of Total]FROM(SELECT CustType , COUNT(CustID ) AS CustCount , SUM(CAST(NULLIF(CustValue,'REMOVED') AS INT)) AS CustValueFROM TableNameGROUP BY CustType)t ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
mikesoper
Starting Member
11 Posts |
Posted - 2013-11-27 : 05:05:35
|
Brilliant - can we round to 1 decimal point here as well? |
|
|
mikesoper
Starting Member
11 Posts |
Posted - 2013-11-27 : 05:10:04
|
Worked that last one out myself - thank you!!! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
mikesoper
Starting Member
11 Posts |
Posted - 2013-11-27 : 05:16:21
|
Can we round up or down here? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-27 : 05:21:05
|
quote: Originally posted by mikesoper Can we round up or down here?
round to how many decinmal places?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
mikesoper
Starting Member
11 Posts |
Posted - 2013-11-27 : 05:28:33
|
1 decimal place |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
Next Page
|