Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Help on how to count guests in hotel every day Expand / Collapse
Author
Message
Posted Tuesday, January 29, 2013 5:26 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 11:12 PM
Points: 3,616, Visits: 5,230
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!
Post #1413397
Posted Wednesday, January 30, 2013 9:41 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC 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

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, August 8, 2013 9:59 AM
Points: 14, Visits: 59
I couldn’t get my assignment done without your help. Thank you.
Post #1413713
Posted Wednesday, January 30, 2013 2:39 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 7:34 AM
Points: 85, Visits: 873
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

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 11:12 PM
Points: 3,616, Visits: 5,230
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!
Post #1413809
Posted Wednesday, January 30, 2013 5:56 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 11:12 PM
Points: 3,616, Visits: 5,230
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!
Post #1413810
Posted Thursday, January 31, 2013 10:11 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC 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 #Guests
SELECT '10/12/2012', '10/16/2012', 'Corky Doe','M' UNION ALL
SELECT '12/12/2012', '12/17/2012', 'Janice Doe','F' UNION ALL
SELECT '11/12/2012', '11/24/2012', 'Howard Stern','M' UNION ALL
SELECT '12/12/2012', '12/13/2012', 'Abagail Johnson','F' UNION ALL
SELECT '12/12/2012', '12/15/2012', 'Teddy Sanft','M' UNION ALL
SELECT '12/12/2012', '12/18/2012', 'John Overton','M' UNION ALL
SELECT '12/12/2012', '12/19/2012', 'Sally Jenson','F' UNION ALL
SELECT '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 Calendar
LEFT JOIN #Guests ON [Day] >= checkin AND [Day] < checkout
GROUP 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

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 11:12 PM
Points: 3,616, Visits: 5,230
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 #Guests
SELECT '10/12/2012', '10/16/2012', 'Corky Doe','M' UNION ALL
SELECT '12/12/2012', '12/17/2012', 'Janice Doe','F' UNION ALL
SELECT '11/12/2012', '11/24/2012', 'Howard Stern','M' UNION ALL
SELECT '12/12/2012', '12/13/2012', 'Abagail Johnson','F' UNION ALL
SELECT '12/12/2012', '12/15/2012', 'Teddy Sanft','M' UNION ALL
SELECT '12/12/2012', '12/18/2012', 'John Overton','M' UNION ALL
SELECT '12/12/2012', '12/19/2012', 'Sally Jenson','F' UNION ALL
SELECT '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 Calendar
LEFT JOIN #Guests ON [Day] >= checkin AND [Day] < checkout
GROUP 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!
Post #1414415
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse