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 |
|
inversions
Starting Member
5 Posts |
Posted - 2007-12-16 : 17:41:39
|
Hi,i need some help with creating a query that would let me select the top 10 between a specific date period and then the top 10 from a previous date range and then displays those that have risen in rank equal to or grater than 2 places.for example:table searchSearchTerm NumSearches Datedog 20 2007-10-01 food 23 2007-10-01music 56 2007-10-02dog 30 2007-10-02ball 85 2007-10-03calender 69 2007-10-05heart 87 2007-10-09baby 45 2007-10-09money 52 2007-10-09fish 74 2007-10-10smile 85 2007-10-10heart 30 2007-10-10dog 50 2007-10-10ball 96 2007-10-11calender 20 2007-11-11food 23 2007-11-12fish 74 2007-11-13smile 85 2007-11-10money 52 2007-11-16heart 87 2007-11-14baby 45 2007-11-13money 46 2007-11-19fish 78 2007-11-18smile 40 2007-11-19dog 50 2007-11-20ball 114 2007-11-21music 186 2007-11-17doctor 205 2007-11-29 select the top 10 by num of searches (numSearches) for two date ranges e.g. 2007-10-01 to 2007-10-31 (month of october) and 2007-11-01 to 2007-11-30(month of november) and identify those that have risen in rank by 2 or more places. i.e. comparing the month of november to that of october.so the output would be as follows:ie for top 10 for month 10 (oct . 2007-10-xx ) Rank SearchTerm NumSearches 1 ball 1812 heart 1173 dog 1004 smile 855 fish 746 calender 697 music 568 money 529 baby 4510 food 23with the top 10 for month 11 (nov. 2007-11-xx) Rank SearchTerm NumSearches1 doctor 2052 Music 1863 fish 152 4 smile 1335 ball 114 6 money 987 heart 878 dog 509 baby 4510 food 23 so the output showing those that have risen in rank by => 2 isRank SearchTerm NumSearches2 music 1863 fish 1526 money 98 any help you could give would be most welcome.thanks,-Inversions |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-12-16 : 18:22:56
|
TOp 10 from a month (in this case October)[Code]Select a.[Rank],a.SearchTerm,a.NumSearchesFrom (Select Year([datecolumn]) as YR,month,[datecolumn]),SearchTerm, NumSearches over (partition by NumSearches order by NumSearches Desc as [Rank] FROM [Table] ) a WHERE [Rank] <=10 and YR = 2007 and MO = 10Something like this for the last part, perhaps.Select a.[Rank],a.SearchTerm,a.NumSearchesFrom (Select Year([datecolumn]) as YR,month,[datecolumn]),SearchTerm, NumSearches over (partition by NumSearches order by NumSearches Desc as [Rank] FROM [Table] WHERE [datecolumn] between '11/1/2007' and '11/30/2007') a INNER JOINSelect b.[Rank] as THisRank, a.Rank as LastRank,b.SearchTerm,b.NumSearches as ThisMonthSearches, a.NumSearches as LastMonthSearchesFrom (Select Year([datecolumn]) as YR,month,[datecolumn]),SearchTerm, NumSearches over (partition by NumSearches order by NumSearches Desc as [Rank] FROM [Table] WHERE [datecolumn] between '10/1/2007' and '10/31/2007') bon a.SearchTerm = b.SearchTermWHERE b.[Rank] - a.[Rank] >=2Order by b.[Rank] desc[/code][Code] Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
matty
Posting Yak Master
161 Posts |
Posted - 2007-12-16 : 23:42:18
|
| SELECT B.Rank,B.SearchTerm,B.NumSearchesFROM (SELECT TOP 10 SearchTerm,SUM(NumSearches) AS NumSearches,DENSE_RANK() OVER(ORDER BY SUM(NumSearches) DESC )AS Rank FROM Search WHERE Date between '2007-10-01' and '2007-10-31' GROUP BY SearchTerm) AJOIN (SELECT TOP 10 SearchTerm,SUM(NumSearches) AS NumSearches,DENSE_RANK() OVER(ORDER BY SUM(NumSearches) DESC )AS Rank FROM Search WHERE Date between '2007-11-01' and '2007-11-30' GROUP BY SearchTerm)BON A.SearchTerm = B.SearchTermWHERE A.Rank-B.Rank >=2ORDER BY 1 |
 |
|
|
|
|
|
|
|