Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Tomji
Starting Member
19 Posts |
Posted - 2008-06-01 : 12:44:07
|
| I have a table as below:COMPUTERNAME, COUNTER, REASONWXP-1 3 FailedWXP-11 13 FailedWXP-3 8 FailedWXP-9 10 FailedWXP-11 7 SuccessWhat I would like to do is create a percentage of Success vs. the Failed for the same Computername.Problem is when I use Group by I cannot tell the COUNTER of the Success anymore. This is what I have been using:SELECT COMPUTERNAME, SUM(COUNTER) AS COUNTERTOTALFROM ReturnTableGROUP BY COMPUTERNAMEThank you. The above query actually targets a result table from another query, but that should not matter. |
|
|
Tomji
Starting Member
19 Posts |
Posted - 2008-06-01 : 13:29:38
|
| Found a way around it. I changed the query that generates this query to have a UNION instead:COMPUTERNAME, COUNTERSUCCESS, COUNTERFAILUREWXP-11 7 0RE2-DC 0 11RE-DC 0 1RE-SMS5 0 9W2K-1 0 7WVI-1 0 6WXP-1 0 3WXP-11 0 13With this table it's easy :)If anyone has a better way to do with my original table, please still let me know. |
 |
|
|
Dallr
Yak Posting Veteran
87 Posts |
Posted - 2008-06-01 : 14:07:12
|
Welcome to SQLTEAMDeclare @MyTable as Table([COMPUTERNAME] Varchar(10), COUNTER Integer, REASON varchar(12))insert into @mytable Values ('WXP-1', 3,'Failed')insert into @mytable Values ('WXP-11', 13,'Failed')insert into @mytable Values ('WXP-3', 8,'Failed')insert into @mytable Values ('WXP-9', 10,'Failed')insert into @mytable Values ('WXP-11', 7 ,'Success')SELECT T1.COMPUTERNAME , SUM(t1.COUNTER) AS COUNTERTOTAL , T1.REASON , (SELECT SUM(T2.COUNTER)FROM @MYTABLE T2 WHERE T2.COMPUTERNAME = T1.COMPUTERNAME) As Occurances , CAST(SUM(t1.COUNTER) AS FLOAT)/(SELECT SUM(T2.COUNTER)FROM @MYTABLE T2 WHERE T2.COMPUTERNAME = T1.COMPUTERNAME)as PercentageFROM @mytable T1GROUP BY T1.COMPUTERNAME , T1.REASONORDER BY T1.COMPUTERNAME, T1.REASONDallr |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-01 : 14:52:50
|
quote: Originally posted by Tomji I have a table as below:COMPUTERNAME, COUNTER, REASONWXP-1 3 FailedWXP-11 13 FailedWXP-3 8 FailedWXP-9 10 FailedWXP-11 7 SuccessWhat I would like to do is create a percentage of Success vs. the Failed for the same Computername.Problem is when I use Group by I cannot tell the COUNTER of the Success anymore. This is what I have been using:SELECT COMPUTERNAME, SUM(COUNTER) AS COUNTERTOTALFROM ReturnTableGROUP BY COMPUTERNAMEThank you. The above query actually targets a result table from another query, but that should not matter.
May be this:-SELECT COMPUTERNAME, SUM(CASE WHEN REASON='Falied' THEN COUNTER ELSE 0 END) AS Failure, SUM(CASE WHEN REASON='Success' THEN COUNTER ELSE 0 END) AS Success, ISNULL(SUM(CASE WHEN REASON='Success' THEN COUNTER ELSE 0 END)/NULLIF(SUM(COUNTER),0),0) AS SuccessPercentageFROM TableGROUP BY COMPUTERNAME |
 |
|
|
Tomji
Starting Member
19 Posts |
Posted - 2008-06-02 : 04:33:33
|
| Thanks for both, they help me improve my SQL a lot.visakh16, your percentage did not work for me, always returned 0. I have no prior expirience with ISNULL and NULLIF, the error must be there somewhere.I use the below now:CAST(CAST(SUM(CASE WHEN Reason='Success' THEN Counter ELSE 0 END) AS Decimal(10,3)) / CAST(SUM(Counter) AS Decimal(10,3)) * 100 AS Decimal(10,3)) AS SuccessPercentage |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-02 : 04:42:02
|
quote: Originally posted by Tomji Thanks for both, they help me improve my SQL a lot.visakh16, your percentage did not work for me, always returned 0. I have no prior expirience with ISNULL and NULLIF, the error must be there somewhere.I use the below now:CAST(CAST(SUM(CASE WHEN Reason='Success' THEN Counter ELSE 0 END) AS Decimal(10,3)) / CAST(SUM(Counter) AS Decimal(10,3)) * 100 AS Decimal(10,3)) AS SuccessPercentage
thats because of implicit conversion i guess. If the COUNTER field is of int typoe you need to do like thisISNULL(SUM(CASE WHEN REASON='Success' THEN COUNTER ELSE 0 END)*(1.0)/NULLIF(SUM(COUNTER),0),0) AS SuccessPercentage |
 |
|
|
Tomji
Starting Member
19 Posts |
Posted - 2008-06-02 : 04:58:59
|
| I guess I now yet again more... division by 0 :),ISNULL(CAST(CAST(SUM(CASE WHEN Reason='Success' THEN Counter ELSE 0 END) AS Decimal(10,3)) / NULLIF(CAST(SUM(CASE WHEN Reason IN ('Success', 'Failed') THEN Counter ELSE 0 END) AS Decimal(10,3)),0) * 100 AS Decimal(10,3)),0) AS SuccessPercentageThe reason why I list Success and Failed seperatly is because I now include two other status reasons that I do not want to calculate into the percentage. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-02 : 05:08:23
|
quote: Originally posted by Tomji I guess I now yet again more... division by 0 :),ISNULL(CAST(CAST(SUM(CASE WHEN Reason='Success' THEN Counter ELSE 0 END) AS Decimal(10,3)) / NULLIF(CAST(SUM(CASE WHEN Reason IN ('Success', 'Failed') THEN Counter ELSE 0 END) AS Decimal(10,3)),0) * 100 AS Decimal(10,3)),0) AS SuccessPercentageThe reason why I list Success and Failed seperatly is because I now include two other status reasons that I do not want to calculate into the percentage.
The reason for using ISNULL and NULLIF was to avoid the division by 0 error. Are you still getting it? |
 |
|
|
Dallr
Yak Posting Veteran
87 Posts |
Posted - 2008-06-02 : 06:37:22
|
You can also use: Declare @MyTable as Table([COMPUTERNAME] Varchar(10), COUNTER Integer, REASON varchar(12))insert into @mytable Values ('WXP-1', 3,'Failed')insert into @mytable Values ('WXP-11', 13,'Failed')insert into @mytable Values ('WXP-3', 8,'Failed')insert into @mytable Values ('WXP-9', 10,'Failed')insert into @mytable Values ('WXP-11', 7 ,'Success')SELECT *FROM (SELECT T1.COMPUTERNAME , T1.REASON , CAST(SUM(t1.COUNTER) AS FLOAT)/(SELECT SUM(T2.COUNTER)FROM @MYTABLE T2 WHERE T2.COMPUTERNAME = T1.COMPUTERNAME)as PercentageFROM @mytable T1GROUP BY T1.COMPUTERNAME , T1.REASON) As ResultsPIVOT ( Max( percentage) FOR [REASON] IN ([Failed],[Success])) As PvtResultDallr |
 |
|
|
Tomji
Starting Member
19 Posts |
Posted - 2008-06-03 : 04:19:01
|
quote: The reason for using ISNULL and NULLIF was to avoid the division by 0 error. Are you still getting it?
Nope, it's all fine now.Thanks again Dallr and visakh16. I have saved both queries for future use. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-03 : 04:19:57
|
quote: Originally posted by Tomji
quote: The reason for using ISNULL and NULLIF was to avoid the division by 0 error. Are you still getting it?
Nope, it's all fine now.Thanks again Dallr and visakh16. I have saved both queries for future use.
you're welcome |
 |
|
|
Dallr
Yak Posting Veteran
87 Posts |
Posted - 2008-06-03 : 06:45:05
|
| No problem, Glad to assist. Dane |
 |
|
|
|
|
|
|
|