| Author |
Topic |
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2011-11-07 : 11:42:55
|
This query prodcues the below:select area = coalesce(area, 'Balto CLMS Take UN'),count(p.fo) as cntfrom pendingbyddsandfo pjoin natdocfile n on n.doc = p.fogroup by n.areaAreas TotalsCLMSUnit 301 1702 16803 6204 3205 3606 96 How do I change my stored procedure to produce this instead with a Case statement?Areas TotalCLMSUnit 3Area 1 17Area 2 168Area 3 62Area 4 32Area 5 36Area 6 96 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-07 : 11:46:29
|
| do you have a field which has these area descriptions?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-07 : 11:48:25
|
if yes it should beselect area = coalesce(areadescription, 'Balto CLMS Take UN'),count(p.fo) as cntfrom pendingbyddsandfo pjoin natdocfile n on n.doc = p.fogroup by n.areadescription if notselect area = coalesce('Area ' + cast(n.area as varchar(3)), 'Balto CLMS Take UN'),count(p.fo) as cntfrom pendingbyddsandfo pjoin natdocfile n on n.doc = p.fogroup by 'Area ' + cast(n.area as varchar(3))------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2011-11-07 : 11:51:05
|
| Thanks I don't have a field with those descriptions, so I used the last version. How can I get rid of the 0 in front of the number?Areas TotalsCLMSUnit 3Area 01 17Area 02 168Area 03 62Area 04 32Area 05 36Area 06 96 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-07 : 12:04:23
|
quote: Originally posted by JJ297 Thanks I don't have a field with those descriptions, so I used the last version. How can I get rid of the 0 in front of the number?Areas TotalsCLMSUnit 3Area 01 17Area 02 168Area 03 62Area 04 32Area 05 36Area 06 96
select area = coalesce('Area ' + cast(n.area*1 as varchar(3)), 'Balto CLMS Take UN'),count(p.fo) as cntfrom pendingbyddsandfo pjoin natdocfile n on n.doc = p.fogroup by 'Area ' + cast(n.area*1 as varchar(3))------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2011-11-07 : 12:04:33
|
Maybe this will help (notice I provided some sample data in a consumable format!)DECLARE @Yak TABLE (Area VARCHAR(20), Total INT)INSERT @Yak (Area, Total)VALUES('CLMSUnit', 3),('01', 17),('02', 168),('03', 62),('04', 32),('05', 36),('06', 96)SELECT *FROM @YakSELECT CASE WHEN Area NOT LIKE '%[^0-9]%' THEN 'Area ' + CAST(CAST(Area AS INT) AS VARCHAR(10)) ELSE Area END AS Areas, Total FROM @Yak |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2011-11-07 : 12:56:22
|
| Thanks visakh16 that worked. Please explain what this is doing especially the *1 |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2011-11-07 : 13:09:28
|
| Thanks Lamprey for your reply as well. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-07 : 23:51:06
|
quote: Originally posted by JJ297 Thanks visakh16 that worked. Please explain what this is doing especially the *1
* 1 was just to make the values 01,02 etc to integer to strip off leading 0------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2011-11-08 : 10:39:52
|
| Thanks again! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-08 : 10:46:29
|
np ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|