January 30, 2013 at 5:56 pm
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 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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
January 31, 2013 at 10:11 am
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
January 31, 2013 at 6:14 pm
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 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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 3 posts - 16 through 17 (of 17 total)
You must be logged in to reply to this topic. Login to reply