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 |
|
enaran
Starting Member
8 Posts |
Posted - 2009-10-28 : 07:54:03
|
| Hi all, I have been asked to extract some data from a rather large databse of bookings the we have entered for our 38 different Caravan Parks situated around the country.Specifically I have been requested to provide the top 10 most common postcodes of guests who have stayed at each property.So far I have been able to extract the top 10 most common postcodes, but only for one property at a time.I would love to be able to do it for all properties at the same time (but as individual entities) so that I dont have to re-run it 38 times.My query thus far isSELECT TOP (10) dbo.GUEST.POSTCODE, COUNT(*) AS TotalStays, dbo.Properties.Property_IdFROM dbo.RES INNER JOIN dbo.CATEGORY ON dbo.RES.CAT_ID = dbo.CATEGORY.CAT_ID INNER JOIN dbo.Properties ON dbo.CATEGORY.Property_Id = dbo.Properties.Property_Id INNER JOIN dbo.GSTRES ON dbo.RES.RES_ID = dbo.GSTRES.RES_ID INNER JOIN dbo.GUEST ON dbo.GSTRES.GUEST_ID = dbo.GUEST.GUEST_IDWHERE (dbo.RES.RES_ID <> dbo.RES.GRPRES_ID) AND (dbo.RES.STATUS < 4) AND (dbo.RES.Arrive BETWEEN CONVERT(DATETIME, '2008-10-01 00:00:00', 102) AND CONVERT(DATETIME, '2009-09-30 00:00:00', 102)) AND (dbo.GUEST.POSTCODE <> ' ') AND (dbo.Properties.Property_Id = 1)GROUP BY dbo.GUEST.POSTCODE, dbo.Properties.Property_IdORDER BY TotalStays DESCWhich results inPostcode,TotalStays,Property_Id4740 506 14680 289 14720 273 14670 219 14810 171 14717 161 14870 148 14814 125 14655 115 14817 108 1I would like to achievePostcode,TotalStays,Property_Id4740 506 14680 289 14720 273 14670 219 14810 171 14717 161 14870 148 14814 125 14655 115 14817 108 15000 275 27000 224 27248 74 27250 68 23001 52 24000 51 22000 50 27310 47 21114 44 27001 41 27250 68 37310 59 37320 35 37908 21 37315 19 37316 19 37307 18 37011 18 37015 16 37248 16 3etc...Thank you |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-10-28 : 08:04:26
|
| SELECT t1.POSTCODE,t1.Property_Id,t1.TotalStaysFROM(SELECT dbo.GUEST.POSTCODE , COUNT(*) AS TotalStays , dbo.Properties.Property_Id , [Rank] = RANK() OVER(Partition by dbo.GUEST.POSTCODE, dbo.Properties.Property_Id order by count(*) desc)FROM dbo.RES INNER JOINdbo.CATEGORY ON dbo.RES.CAT_ID = dbo.CATEGORY.CAT_ID INNER JOINdbo.Properties ON dbo.CATEGORY.Property_Id = dbo.Properties.Property_Id INNER JOINdbo.GSTRES ON dbo.RES.RES_ID = dbo.GSTRES.RES_ID INNER JOINdbo.GUEST ON dbo.GSTRES.GUEST_ID = dbo.GUEST.GUEST_IDWHERE (dbo.RES.RES_ID <> dbo.RES.GRPRES_ID) AND (dbo.RES.STATUS < 4) AND (dbo.RES.Arrive BETWEEN CONVERT(DATETIME, '2008-10-01 00:00:00', 102) AND CONVERT(DATETIME, '2009-09-30 00:00:00', 102)) AND (dbo.GUEST.POSTCODE <> ' ') AND (dbo.Properties.Property_Id = 1)GROUP BY dbo.GUEST.POSTCODE, dbo.Properties.Property_Id) t1WHERE Rank <= 10ORDER BY Property_Id asc,TotalStays DESCJimEveryday I learn something that somebody else already knew |
 |
|
|
enaran
Starting Member
8 Posts |
Posted - 2009-10-28 : 08:21:11
|
| Hi JimFThank you for your help.I assume I would have to remove the "AND (dbo.Properties.Property_Id = 1)" from the select clause...However I get a Syntax error from the SQL Mangement studio saying "The OVER SQL construct or statement is not supported."Any ideas? |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2009-10-28 : 08:50:44
|
| OVER not supported?What version of SQL SERVER are you running ? Also, is your database compatibility level set to 90(i think) or better?http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
enaran
Starting Member
8 Posts |
Posted - 2009-10-28 : 09:24:06
|
| My Mistake,Got it to run but it is returning the top * from each property so I modified the statement to include the Rank in the output and every record seems to be ranked no 1?SELECT t1.POSTCODE,t1.Property_Id, t1.TotalStays, t1.RankFROM(SELECT dbo.GUEST.POSTCODE, COUNT(*) AS TotalStays, dbo.Properties.Property_Id, [Rank] = RANK() OVER(Partition by dbo.GUEST.POSTCODE, dbo.Properties.Property_Id order by count(*) desc)FROM dbo.RES INNER JOINdbo.CATEGORY ON dbo.RES.CAT_ID = dbo.CATEGORY.CAT_ID INNER JOINdbo.Properties ON dbo.CATEGORY.Property_Id = dbo.Properties.Property_Id INNER JOINdbo.GSTRES ON dbo.RES.RES_ID = dbo.GSTRES.RES_ID INNER JOINdbo.GUEST ON dbo.GSTRES.GUEST_ID = dbo.GUEST.GUEST_IDWHERE (dbo.RES.RES_ID <> dbo.RES.GRPRES_ID) AND (dbo.RES.STATUS < 4) AND (dbo.RES.Arrive BETWEEN CONVERT(DATETIME, '2008-10-01 00:00:00', 102) AND CONVERT(DATETIME, '2009-09-30 00:00:00', 102)) AND (dbo.GUEST.POSTCODE <> ' ')GROUP BY dbo.GUEST.POSTCODE, dbo.Properties.Property_Id) t1WHERE Rank <= 10ORDER BY Property_Id asc,TotalStays DESCOutputPostcode, property_id, TotalStays, Rank4740 1 506 14680 1 289 14720 1 273 14670 1 219 14810 1 171 14717 1 161 14870 1 148 14814 1 125 14655 1 115 14817 1 108 14350 1 106 14715 1 100 14745 1 99 14702 1 97 14744 1 79 14211 1 72 14718 1 66 14551 1 66 1etc... |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-10-28 : 10:06:01
|
| That would mean that the combination of PropertyID and PostCode is Unique. Remove the Postcode from the Partition by piece of the Rank() function and see if that's what you want.JimEveryday I learn something that somebody else already knew |
 |
|
|
enaran
Starting Member
8 Posts |
Posted - 2009-10-28 : 10:27:28
|
| Thank you - all working. |
 |
|
|
|
|
|
|
|