| Author |
Topic |
|
arusu
Yak Posting Veteran
60 Posts |
Posted - 2010-08-23 : 11:51:40
|
| I need a query that will parse out a number from a stringso my results are as follows:11.3.5.65.6.43.5.711.3.411.33.35.3#33#34so i need a query that will shownumber count11 35 23 1other 2so there are 3 results that start with 11, 2 that start with 5 and 1 that starts with 3. The "other" column will be anything that does not follow the regular naming convention. basically it does not have a "." as a delimiter. |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-08-23 : 12:07:47
|
| Since the count of "." are not consistent in the data, I feel the Parse function cannot be used.Alternate way to satify your requirement. Try and let us know.Select OutVal,count(*) from (Select case when Charindex('.',<columnname>) > 1 then left(sval,Charindex('.',<columnname>) -1)else <columnname> end as OutVal from <TableName>where sval like '[0-9]%') as subtabgroup by outvalSample example for your understanding:Declare @Sample table(SVal varchar(50))Insert into @Sampleselect '11.3.5.6' unionselect '5.6.4' unionselect '3.5.7' unionselect '11.3.4' unionselect '11.33.3' unionselect '5.3' unionselect '#33' unionselect '#34'--select * from @SampleSelect OutVal,count(*) from (Select case when Charindex('.',sval) > 1 then left(sval,Charindex('.',sval) -1)else sval end as OutVal from @samplewhere sval like '[0-9]%') as subtabgroup by outvalRegards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-23 : 12:16:57
|
quote: Originally posted by arusu I need a query that will parse out a number from a stringso my results are as follows:11.3.5.65.6.43.5.711.3.411.33.35.3#33#34so i need a query that will shownumber count11 35 23 1other 2so there are 3 results that start with 11, 2 that start with 5 and 1 that starts with 3. The "other" column will be anything that does not follow the regular naming convention. basically it does not have a "." as a delimiter.
SELECT Header,COUNT(Field) AS CountFROM(SELECT CASE WHEN field LIKE '[0-9]%' THEN LEFT(Field,CASE WHEN CHARINDEX('.',Field) > 0 THEN CHARINDEX('.',Field)-1 ELSE LEN(Field) END) ELSE 'Other' END AS Header,FieldFROM YourTable)tGROUP BY Header------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
arusu
Yak Posting Veteran
60 Posts |
Posted - 2010-08-23 : 12:37:23
|
| Ok I thought I could figure out what I need from your answers which i do appreciate but there is more and this is more complicated than what I thought.I have another table which i need to join with results for each categoryso the new table will havenumber color11.1.1 red11.3.5 red11.3.2 blueso the query that I need that hadnumber count11 3will now need to benumber red blue yellow11 2 1 0 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-23 : 12:59:03
|
you can use same logic in your second table also to get categorywise totals.likeSELECT Header,COUNT(CASE WHEN Color='red' THEN 1 ELSE NULL END) AS Red,COUNT(CASE WHEN Color='blue' THEN 1 ELSE NULL END) AS Blue,COUNT(CASE WHEN Color='yellow' THEN 1 ELSE NULL END) AS YellowFROM(SELECT CASE WHEN field LIKE '[0-9]%' THEN LEFT(Field,CASE WHEN CHARINDEX('.',Field) > 0 THEN CHARINDEX('.',Field)-1 ELSE LEN(Field) END) ELSE 'Other' END AS Header,ColorFROM YourTable)tGROUP BY Header------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
arusu
Yak Posting Veteran
60 Posts |
Posted - 2010-08-23 : 13:25:54
|
| but my second table which has the number and color, needs to join from the table that just has the numbers because there are some numbers from the table with color that do not exist in the first table with just the number |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-23 : 13:30:51
|
ok then do likeSELECT q1.Header,q1.[Count],q2.Red,q2.Blue,q2.YellowFROM (First query)q1LEFT JOIN (Second query)q2ON q2.Header = q1.Header put queries in right places------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
arusu
Yak Posting Veteran
60 Posts |
Posted - 2010-08-23 : 15:53:21
|
| How do I get the percentages of the each color's count as another columnso:number red yellow blue %red %yellow %blue11 2 0 2 50.0% 0.0% 50.0% |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-24 : 07:20:39
|
make second query asSELECT Header,COUNT(CASE WHEN Color='red' THEN 1 ELSE NULL END) AS Red,COUNT(CASE WHEN Color='blue' THEN 1 ELSE NULL END) AS Blue,COUNT(CASE WHEN Color='yellow' THEN 1 ELSE NULL END) AS Yellow,COUNT(CASE WHEN Color='red' THEN 1 ELSE NULL END)*100.0/COUNT(*) AS [%red],COUNT(CASE WHEN Color='yellow' THEN 1 ELSE NULL END)*100.0/COUNT(*) AS [%yellow],COUNT(CASE WHEN Color='blue' THEN 1 ELSE NULL END)*100.0/COUNT(*) AS [%blue]FROM(SELECT CASE WHEN field LIKE '[0-9]%' THEN LEFT(Field,CASE WHEN CHARINDEX('.',Field) > 0 THEN CHARINDEX('.',Field)-1 ELSE LEN(Field) END) ELSE 'Other' END AS Header,ColorFROM YourTable)tGROUP BY Header------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
arusu
Yak Posting Veteran
60 Posts |
Posted - 2010-08-24 : 10:01:40
|
| Hi, thank you for all your help so far, i think i just have one more questionwhat if i want to get the percentage of red between red and blue? and how do i get the percentage to show only to one decimal place? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-24 : 10:08:01
|
| for that just include counts of red and blue alone in denominator rather than count(*). a small change in last suggestion will do it. I'm leaving it to you to try it yourself. dont want to spoonfeed you.you can use ROUND() function for that.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
arusu
Yak Posting Veteran
60 Posts |
Posted - 2010-08-24 : 10:20:50
|
| sorry i did not mean for you to feel like you are spoon feeding me. When i ask a question, i have already tried a solution and it was not working for me. For example i already tried COUNT(CASE WHEN color != 'yellow' THEN 1 ELSE NULL END) on the denominatorbut got this errorDivide by zero error encountered.Warning: Null value is eliminated by an aggregate or other SET operation.and i already knew of the round function and used it like this:ROUND(COUNT(CASE WHEN Color='red' THEN 1 ELSE NULL END)*100.0/COUNT(*), 1) AS [%red]but it did not work.You don't have to give me the answer, and i feel bad to ask but maybe you have a hint? |
 |
|
|
arusu
Yak Posting Veteran
60 Posts |
Posted - 2010-08-24 : 10:44:36
|
| ok so the round works with a convert to decimal but i am still having the first problemany suggestions? |
 |
|
|
arusu
Yak Posting Veteran
60 Posts |
Posted - 2010-08-24 : 11:30:24
|
| I figured it out. I am using an ifnull on the denominator and a isnull on the entire thing to turn it into a zerothanks for the help from before |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-25 : 09:40:49
|
quote: Originally posted by arusu I figured it out. I am using an ifnull on the denominator and a isnull on the entire thing to turn it into a zerothanks for the help from before
i hope you meant NULLIFyeah..thats the way to go------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|