|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 10:27 AM
Points: 1,384,
Visits: 4,881
|
|
Nils Gustav Stråbø (12/9/2010) to be able to answer it correctly, without cheating, you will have to manually write down how many rows are inserted in each run of the loop in order to keep track of the number in the meaningless "SomeNumberForDate" column.
I got it right by immediately eliminating the 1 and 365 options, then I couldn't spot any syntax errors, so went for the one that was left.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 7:59 AM
Points: 2,808,
Visits: 1,129
|
|
Nils Gustav Stråbø (12/9/2010)
64% got it right. Yeah, right!! How many cheated and ran the T-SQL code?
I cheated and got it right!
By the way, how many of you read the forum comments and stop after you get to Hugo's?
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Friday, April 26, 2013 11:57 AM
Points: 708,
Visits: 660
|
|
kevin.l.williams (12/9/2010)
Nils Gustav Stråbø (12/9/2010)
64% got it right. Yeah, right!! How many cheated and ran the T-SQL code?
I cheated and got it right! By the way, how many of you read the forum comments and stop after you get to Hugo's? 
Kevin gets a 'like'
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Today @ 8:44 PM
Points: 21,376,
Visits: 9,585
|
|
Nils Gustav Stråbø (12/9/2010) I don't like questions like this, and not because I got it wrong. It teaches me nothing about Sql Server or T-SQL (except debugging peculiar T-SQL), and to be able to answer it correctly, without cheating, you will have to manually write down how many rows are inserted in each run of the loop in order to keep track of the number in the meaningless "SomeNumberForDate" column.
64% got it right. Yeah, right!! How many cheated and ran the T-SQL code?
I'd like the link that tells us we can't run the t-sql before answering. I can't remember ever seeing that post anywhere.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 4:45 PM
Points: 5,297,
Visits: 7,240
|
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Today @ 8:44 PM
Points: 21,376,
Visits: 9,585
|
|
Hugo Kornelis (12/9/2010)
kevin.l.williams (12/9/2010)
By the way, how many of you read the forum comments and stop after you get to Hugo's?   If that number is significantly large, I'll have to consider postponing my comments, so that other valuable comments get read as well.  Thanks for the kind words!
Well I'm one of the many who scroll fast untill I see Hugo, but then again I usually read everything.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: 2 days ago @ 12:30 AM
Points: 1,788,
Visits: 3,330
|
|
I'd like the link that tells us we can't run the t-sql before answering. I can't remember ever seeing that post anywhere. That's cheating in my world
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, May 23, 2012 6:47 AM
Points: 15,
Visits: 134
|
|
I want to thank everyone for the valuable comments. Using those comments and a little googling (Itzik Ben-Gan's number generator at http://www.projectdmx.com/tsql/tblnumbers.aspx#Row and Shannon Severance's answer on a continuous dates question http://efreedom.com/Question/1-1395367/Return-Temp-Table-Continuous-Dates?showall=true#additionalAnswersMarker), I constructed a far better solution.
Anyway, I have learned by posting this little puzzle and I hope some others have too. 
DECLARE @dtFrom DATETIME; DECLARE @dtTo DATETIME; DECLARE @dtStart DATETIME;
SELECT @dtFrom = '2010-01-01T00:00:00.000', @dtTo = '2010-12-31T00:00:00.000';
SET @dtStart = GETDATE();
CREATE TABLE #Dates ( [Date] DATETIME NOT NULL PRIMARY KEY, [SomeValueForDate] INT NOT NULL );
WITH n2 (n) AS (SELECT 1 UNION ALL SELECT 0), -- 2 entries n4 (n) AS (SELECT 1 FROM n2 x CROSS JOIN n2 y), -- 4 entries n16 (n) AS (SELECT 1 FROM n4 x CROSS JOIN n4 y), -- 16 entries n256 (n) AS (SELECT 1 FROM n16 x CROSS JOIN n16 y), -- 256 entries n65536 (n) AS (SELECT 1 FROM n256 x CROSS JOIN n256 y), -- 65536 entries nmax (n) AS (SELECT 1 FROM n65536 x CROSS JOIN n256 y), -- 16777216 entries (would never be reached as max date = 9999-12-31T00:00:00 = 2958463) num (n) AS (SELECT ROW_NUMBER() OVER (ORDER BY n) FROM nmax) INSERT INTO [#Dates] SELECT DATEADD(DAY, n-1, @dtFrom), n FROM num WHERE n <= DATEDIFF(DAY, @dtFrom, @dtTo) + 1;
SELECT DATEDIFF(MS, @dtStart, GETDATE()) AS [Runtime in msec]; SELECT * FROM [#Dates] ORDER BY 1; DROP TABLE #Dates;
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,232,
Visits: 1,046
|
|
Toreador (12/9/2010)
I got it right by immediately eliminating the 1 and 365 options, then I couldn't spot any syntax errors, so went for the one that was left.
I took the same route and then ran the code anyway to be sure. I can see where this would have made an interesting SCRIPTS submission, but does not seem to be a question that teaches anything. It did reinforce something I learned in high school. You HAVE TO RUN CODE to know what it will do, otherwise you are just guessing.
C code C code run run code run
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,232,
Visits: 1,046
|
|
|
|
|