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 |
cyberpd
Yak Posting Veteran
60 Posts |
Posted - 2007-01-04 : 03:45:04
|
Let me first write down the table structureBook_Details(CatCode,SubCatCode,CatName,SubCatName,BookName,AuthorName,ISBNNo,BookPrice,No_Of_Pages,Book_Size,Description)Suppose, there are 4 tuples. And each tuple has got the word "health" somewhere in the string in the following fields :- BookName, DescriptionEg: BookName field contains "awekfhHEALTHjnkjfbgryhj"Description field contains " this particular book is on HEALTH"let the occurance of the word "HEALTH" is once in the first tuple.let the occurance of the word "HEALTH" is twice in the secnd tuple.let the occurance of the word "HEALTH" is thrice in the third tuple.let the occurance of the word "HEALTH" is 4 times in the 4th tuple.my question is :write down a query that finds out all the tuples that contains the string "HEALTH" in the fields BookName, Description and show the result in a sorted order based on the maximum number of occurance of that string.eg:- 4th tuple will be shown first3rd tuple2nd tuple1st tuplethanx and regards |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-04 : 06:33:37
|
[code]-- prepare sample datadeclare @t table (data varchar(1000))insert @tselect 'awekfhHEALTHjnkjfbgryhj' union allselect 'awekfhHEALTHjnkjHEALTHfbgryhj' union allselect 'aweHEALTHkfhHEALTHHEALTHHEALTHjnkjfHEALTHbgryhj' union allselect 'awekfhHEALTHjnkjfbHEALTHgrHEALTHyhj' union allselect 'awekfhHEALTHHEALTHjnkjfbgryhj' union allselect 'awekfhHEATHjnkjfbgryhj'-- do the workselect data, (len(data) - len(replace(data, 'health', ''))) / len('health') as occurenciesfrom @torder by 2 desc, 1[/code]Peter LarssonHelsingborg, Sweden |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-01-04 : 08:01:13
|
ororder by len(data) - len(replace(data, 'health', '')) descMadhivananFailing to plan is Planning to fail |
 |
|
cyberpd
Yak Posting Veteran
60 Posts |
Posted - 2007-01-04 : 08:15:38
|
this site is great...!!a million thanx to peso.and thnx to madhivanan.. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-04 : 08:55:44
|
Madhi, why would you do another calculation again and waste more CPU cycles?I think it is easier to just do a ORDER BY 2 DESC...Peter LarssonHelsingborg, Sweden |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-01-04 : 09:47:04
|
Well. It is becuase of this querySelect data from @torder by len(data) - len(replace(data, 'health', '')) descMadhivananFailing to plan is Planning to fail |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-04 : 10:04:36
|
Oh, you mean that only show the data and not present the actual number of tuplets?Peter LarssonHelsingborg, Sweden |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-01-04 : 10:28:12
|
Yes from the way questioner asked for MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|