Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Help on how to count guests in hotel every day Rate Topic Display Mode Topic Options
Author
 Message
 Posted Tuesday, January 29, 2013 5:26 PM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 You can probably make a change something like this:`SELECT [Day], MaleGuests=ISNULL(SUM(CASE Gender WHEN 'M' THEN 1 END), 0) , FemaleGuests=ISNULL(SUM(CASE Gender WHEN 'F' THEN 1 END), 0)` My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1413397
 Posted Wednesday, January 30, 2013 9:41 AM
 SSC Veteran Group: General Forum Members Last Login: Tuesday, August 13, 2013 11:43 AM Points: 242, Visits: 433
 I would ordinarily add an "Else 0" prior to the End in those case statements rather than the isnull function. Is there a performance difference, or just personal preference? Puto me cogitare, ergo puto me esse.I think that I think, therefore I think that I am.
Post #1413691
 Posted Wednesday, January 30, 2013 10:45 AM
 Grasshopper Group: General Forum Members Last Login: Tuesday, February 9, 2016 2:28 PM Points: 14, Visits: 78
 I couldn’t get my assignment done without your help. Thank you.
Post #1413713
 Posted Wednesday, January 30, 2013 2:39 PM
 SSC Journeyman Group: General Forum Members Last Login: 2 days ago @ 2:09 PM Points: 85, Visits: 1,178
 This is very nearly the same type of problem discussed in Phil Factor's SQL Speed Phreak challenge http://ask.sqlservercentral.com/questions/1227/the-subscription-list-sql-problem.html, which is also discussed nicely here http://www.simple-talk.com/sql/performance/writing-efficient-sql-set-based-speed-phreakery/. Besides being a good read, I think that you could use the winning code easily enough, substituting days for months and guests for subscribers.
Post #1413773
 Posted Wednesday, January 30, 2013 5:54 PM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 srienstr (1/30/2013)I would ordinarily add an "Else 0" prior to the End in those case statements rather than the isnull function. Is there a performance difference, or just personal preference?Actually, you need to use the ISNULL because there may be days when there are no guests. My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1413809
 Posted Wednesday, January 30, 2013 5:56 PM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 murgatroid (1/30/2013)This is very nearly the same type of problem discussed in Phil Factor's SQL Speed Phreak challenge http://ask.sqlservercentral.com/questions/1227/the-subscription-list-sql-problem.html, which is also discussed nicely here http://www.simple-talk.com/sql/performance/writing-efficient-sql-set-based-speed-phreakery/. Besides being a good read, I think that you could use the winning code easily enough, substituting days for months and guests for subscribers.Heavens to murgatroid!I'd seen that article and forgotten about it. Thanks for reminding me. My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1413810
 Posted Thursday, January 31, 2013 10:11 AM
 SSC Veteran Group: General Forum Members Last Login: Tuesday, August 13, 2013 11:43 AM Points: 242, Visits: 433
 I get the same results both ways, even for days with no guests of one gender or no guests at all.`CREATE TABLE #Guests (checkin date, checkout date, FullName varchar(50), Gender char(1))INSERT INTO #GuestsSELECT '10/12/2012', '10/16/2012', 'Corky Doe','M' UNION ALLSELECT '12/12/2012', '12/17/2012', 'Janice Doe','F' UNION ALLSELECT '11/12/2012', '11/24/2012', 'Howard Stern','M' UNION ALLSELECT '12/12/2012', '12/13/2012', 'Abagail Johnson','F' UNION ALLSELECT '12/12/2012', '12/15/2012', 'Teddy Sanft','M' UNION ALLSELECT '12/12/2012', '12/18/2012', 'John Overton','M' UNION ALLSELECT '12/12/2012', '12/19/2012', 'Sally Jenson','F' UNION ALLSELECT '10/20/2012', '12/12/2012', 'Tiffany Blue','F'DECLARE @StartOfMonth DATE = '2012-12-01';WITH Calendar AS ( SELECT TOP (DAY(DATEADD(mm,DATEDIFF(mm,-1,@StartOfMonth),-1))) [Day]=DATEADD(day, number - 1, @StartOfMonth) FROM [master].dbo.spt_values Tally WHERE [Type] = 'P' AND Number BETWEEN 1 AND 31)SELECT [Day], Guests=ISNULL(COUNT(FullName), 0), MaleGuests=ISNULL(SUM(CASE Gender WHEN 'M' THEN 1 END), 0), FemaleGuests=ISNULL(SUM(CASE Gender WHEN 'F' THEN 1 END), 0), Guests2=COUNT(FullName), MaleGuests2=SUM(CASE Gender WHEN 'M' THEN 1 else 0 END), FemaleGuests2=SUM(CASE Gender WHEN 'F' THEN 1 else 0 END)FROM CalendarLEFT JOIN #Guests ON [Day] >= checkin AND [Day] < checkoutGROUP BY [Day]DROP TABLE #Guests` Puto me cogitare, ergo puto me esse.I think that I think, therefore I think that I am.
Post #1414246
 Posted Thursday, January 31, 2013 6:14 PM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 srienstr (1/31/2013)I get the same results both ways, even for days with no guests of one gender or no guests at all.`CREATE TABLE #Guests (checkin date, checkout date, FullName varchar(50), Gender char(1))INSERT INTO #GuestsSELECT '10/12/2012', '10/16/2012', 'Corky Doe','M' UNION ALLSELECT '12/12/2012', '12/17/2012', 'Janice Doe','F' UNION ALLSELECT '11/12/2012', '11/24/2012', 'Howard Stern','M' UNION ALLSELECT '12/12/2012', '12/13/2012', 'Abagail Johnson','F' UNION ALLSELECT '12/12/2012', '12/15/2012', 'Teddy Sanft','M' UNION ALLSELECT '12/12/2012', '12/18/2012', 'John Overton','M' UNION ALLSELECT '12/12/2012', '12/19/2012', 'Sally Jenson','F' UNION ALLSELECT '10/20/2012', '12/12/2012', 'Tiffany Blue','F'DECLARE @StartOfMonth DATE = '2012-12-01';WITH Calendar AS ( SELECT TOP (DAY(DATEADD(mm,DATEDIFF(mm,-1,@StartOfMonth),-1))) [Day]=DATEADD(day, number - 1, @StartOfMonth) FROM [master].dbo.spt_values Tally WHERE [Type] = 'P' AND Number BETWEEN 1 AND 31)SELECT [Day], Guests=ISNULL(COUNT(FullName), 0), MaleGuests=ISNULL(SUM(CASE Gender WHEN 'M' THEN 1 END), 0), FemaleGuests=ISNULL(SUM(CASE Gender WHEN 'F' THEN 1 END), 0), Guests2=COUNT(FullName), MaleGuests2=SUM(CASE Gender WHEN 'M' THEN 1 else 0 END), FemaleGuests2=SUM(CASE Gender WHEN 'F' THEN 1 else 0 END)FROM CalendarLEFT JOIN #Guests ON [Day] >= checkin AND [Day] < checkoutGROUP BY [Day]DROP TABLE #Guests`Sorry. I think I gave you a bad answer without thinking to much about it.I believe the ISNULL was just a carryover from my original solution where it was needed. My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1414415

 Permissions