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 |
|
neil_akoga
Yak Posting Veteran
56 Posts |
Posted - 2009-04-28 : 05:39:18
|
| is there any way to do a count of occurrences in a selectso if i had a varchar(column1) like belowrow1'i love dogs, dogs are the best'row2'i hate dogs'row3'dogs, dogs'and i did select * from table where column1 like '%dogs%' i could get the following resultsrow1 = 2row2 = 1row3 = 2is it possible to do an accumulative count of the occurrences of a select value like above? any pointers appreciated |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-28 : 06:56:15
|
| Try thisselect col,(len(col)-len(replace(col,'dogs','')))/4 from your_tableMadhivananFailing to plan is Planning to fail |
 |
|
|
neil_akoga
Yak Posting Veteran
56 Posts |
Posted - 2009-04-28 : 07:18:54
|
| ok, that's perfect, next annoying question- say i have another column, col2(varchar again)col2row1 'dogs and monkeys'row2 'dogs eat food'row3 'dogs don't like dog food'i presume i can do something like this SELECT col, (LEN(col) - LEN(REPLACE(col, 'dogs', ''))) / 4 AS colCount1,(LEN(col2) - LEN(REPLACE(col2, 'dogs', ''))) / 4 AS colCount2FROM tblTestSearchbut how would i add colCount1 and colCount2 together to create a totalCount as a fourth column? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-28 : 07:21:23
|
| SELECT col,colCount1,colCount2,colCount1+colCount2 as total_colCount FROM(SELECT col, (LEN(col) - LEN(REPLACE(col, 'dogs', ''))) / 4 AS colCount1,(LEN(col2) - LEN(REPLACE(col2, 'dogs', ''))) / 4 AS colCount2FROM tblTestSearch) as tMadhivananFailing to plan is Planning to fail |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-04-28 : 07:21:23
|
Wow madhi!Cool and simple.Webfred No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
neil_akoga
Yak Posting Veteran
56 Posts |
Posted - 2009-04-28 : 07:23:24
|
| now you're just showing off :), cheers madhi, you are a true gentleman and a genius |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-28 : 07:23:35
|
quote: Originally posted by webfred Wow madhi!Cool and simple.Webfred No, you're never too old to Yak'n'Roll if you're too young to die.
Thanks MadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-28 : 07:24:25
|
quote: Originally posted by neil_akoga now you're just showing off :), cheers madhi, you are a true gentleman and a genius
Thanks MadhivananFailing to plan is Planning to fail |
 |
|
|
neil_akoga
Yak Posting Veteran
56 Posts |
Posted - 2009-04-28 : 07:48:03
|
| ok, I've found something wrong with it i thinkhere's the test querySELECT col, (LEN(col) - LEN(REPLACE(col, 'dogs', ''))) / 4 AS col3, (LEN(col) - LEN(REPLACE(col, 'dogs', ''))) / 4 AS col4, (LEN(col) - LEN(REPLACE(col, 'dogs', ''))) / 4 + (LEN(col) - LEN(REPLACE(col, 'dogs', ''))) / 4 AS colCountFROM tblTestSearchand here's the data it producesi love dogs, dogs are the best 2 2 4i hate dogs 1 1 2dogs, dogs 2 2 4cats 0 0 0monkey dogs dogs dogs dogs dogs 6 6 12dog 0 0 0dogs 1 1 2any idea why the row with 'monkey dogs dogs dogs dogs dogs' comes back with a count of 6? |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-04-28 : 08:16:17
|
| It will give you a wrong count as long as the number of occurences exceeds 4. |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-04-28 : 08:16:39
|
try this,SELECT col, (LEN(col) - LEN(REPLACE(REPLACE(col,' ','#'), 'dogs', ''))) / 4 AS col3, (LEN(col) - LEN(REPLACE(REPLACE(col,' ','#'), 'dogs', ''))) / 4 AS col4, (LEN(col) - LEN(REPLACE(REPLACE(col,' ','#'), 'dogs', ''))) / 4+(LEN(col) - LEN(REPLACE(REPLACE(col,' ','#'), 'dogs', ''))) / 4FROM tblTestSearch |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-28 : 08:42:30
|
quote: Originally posted by neil_akoga ok, I've found something wrong with it i thinkhere's the test querySELECT col, (LEN(col) - LEN(REPLACE(col, 'dogs', ''))) / 4 AS col3, (LEN(col) - LEN(REPLACE(col, 'dogs', ''))) / 4 AS col4, (LEN(col) - LEN(REPLACE(col, 'dogs', ''))) / 4 + (LEN(col) - LEN(REPLACE(col, 'dogs', ''))) / 4 AS colCountFROM tblTestSearchand here's the data it producesi love dogs, dogs are the best 2 2 4i hate dogs 1 1 2dogs, dogs 2 2 4cats 0 0 0monkey dogs dogs dogs dogs dogs 6 6 12dog 0 0 0dogs 1 1 2any idea why the row with 'monkey dogs dogs dogs dogs dogs' comes back with a count of 6?
Instead of col, use ','+col+','MadhivananFailing to plan is Planning to fail |
 |
|
|
neil_akoga
Yak Posting Veteran
56 Posts |
Posted - 2009-04-28 : 08:47:37
|
| ok, this is really weird now, anyone know why i'm getting some things counted and some not?, here's my results table when i run this querycolumns are 'comments', col0, col1, col2 and relevanceCount (left to right below)Torque control using the Optidrive Plus 2 1 0 3Optidrive Plus 3GV User Guide 2 0 0 2Optidrive E2 User Guide 2 0 0 2here is the query i run to get this resultSELECT comments, (LEN(comments) - LEN(REPLACE(REPLACE(comments, ' ', '#'), 'optidrive', ''))) / 4 AS col0, (LEN(comments) - LEN(REPLACE(REPLACE(comments, ' ', '#'), 'plus', ''))) / 4 AS col1, (LEN(comments) - LEN(REPLACE(REPLACE(comments, ' ', '#'), '3gv', ''))) / 4 AS col2, ((LEN(comments) - LEN(REPLACE(REPLACE(comments, ' ', '#'), 'optidrive', ''))) / 4 + (LEN(comments) - LEN(REPLACE(REPLACE(comments, ' ', '#'), 'plus', ''))) / 4) + (LEN(comments) - LEN(REPLACE(REPLACE(comments, ' ', '#'), '3gv', ''))) / 4 AS relevanceCountFROM tblDLFilesORDER BY relevanceCount DESChere is what i should be gettingTorque control using the Optidrive Plus 1 1 0 2Optidrive Plus 3GV User Guide 1 1 1 3Optidrive E2 User Guide 1 0 0 1 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-28 : 08:55:20
|
| See my previous suggestionMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|