June 5, 2013 at 4:06 pm
Dird (6/5/2013)
Luis Cazares (6/5/2013)
The "Numbers" or "Tally" Table: What it is and how it replaces a loop.The problem with that is you'd have to create the table separately, when you said do it in tsql I read it as one action :s This is how you do it =P no way you'd do it over the phone though:
begin
WITH generator
AS (SELECT 1 AS id
UNION ALL
SELECT id + 1
FROM generator
WHERE id < 100)
select case
when (id %15)=0 then 'FizzBuzz'
when (id%3)=0 then 'Fizz'
when (id%5)=0 then 'Buzz'
else cast(id as varchar)
end
from generator
end
Actually, something more like this:
WITH
E1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
), --10E+1 or 10 rows
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS (
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
)
select
N,
nullif(case when N % 3 = 0 then 'fizz' else '' end + case when N % 5 = 0 then 'buzz' else '' end, '')
from
cteTally
where
N <= 100
June 5, 2013 at 4:17 pm
Steven Willis (6/5/2013)
Programming can usually be taught but teaching character, now that's not an employer's job even if it was possible.
You'll be surprised, but programming isn't something you can just learn in books, it requires special learning capabilities. You could read this article that talks about an academic paper on the subject http://www.codinghorror.com/blog/2006/07/separating-programming-sheep-from-non-programming-goats.html.
Also, teaching SQL (or any other technology) from scratch isn't part of the employer's job even if delivering training is required, noone will bother on teaching the basics if it's a job requirement.
June 5, 2013 at 4:22 pm
Luis Cazares (6/5/2013)
You'll be surprised, but programming isn't something you can just learn in books, it requires special learning capabilities.
They can if they read TeachFirst Design Patterns (applies to Java/OO only :P)
June 5, 2013 at 4:25 pm
Lynn Pettis (6/5/2013)
Actually, something more like this:
Seems more complex/messy. I think I'll remember my way in case I have a DBA interview (chance to see how little I know about MSSQL ^_^) in the next couple weeks & they decide to recycle some old coding questions `-`
June 5, 2013 at 4:31 pm
Luis Cazares (6/5/2013)
Steven Willis (6/5/2013)
Programming can usually be taught but teaching character, now that's not an employer's job even if it was possible.
You'll be surprised, but programming isn't something you can just learn in books, it requires special learning capabilities. You could read this article that talks about an academic paper on the subject http://www.codinghorror.com/blog/2006/07/separating-programming-sheep-from-non-programming-goats.html.
Also, teaching SQL (or any other technology) from scratch isn't part of the employer's job even if delivering training is required, noone will bother on teaching the basics if it's a job requirement.
No, not surprised. I don't disagree with your statement at all. Programming certainly requires some raw talent to start with not unlike an athlete or musician. Sometimes no amount of teaching will help. I was making some sweeping generalizations earlier and of course if you are trying to hire a SQL progammer you want someone who can do the job. But I think the type of personality you point to may also be the type that is the least comfortable trying to convey those skills over a phone line. It would be like choosing your baseball team's shortstop by phone...you are likely to have very inconsistent results in evaluating the talent that way.
June 5, 2013 at 4:53 pm
Dird (6/5/2013)
Luis Cazares (6/5/2013)
The "Numbers" or "Tally" Table: What it is and how it replaces a loop.The problem with that is you'd have to create the table separately, when you said do it in tsql I read it as one action :s This is how you do it =P no way you'd do it over the phone though:
begin
WITH generator
AS (SELECT 1 AS id
UNION ALL
SELECT id + 1
FROM generator
WHERE id < 100)
select case
when (id %15)=0 then 'FizzBuzz'
when (id%3)=0 then 'Fizz'
when (id%5)=0 then 'Buzz'
else cast(id as varchar)
end
from generator
end
That uses a recursive CTE to count which is worse than using a While Loop. Please see the following article for why rCTEs that count are such a bad thing.
http://www.sqlservercentral.com/articles/T-SQL/74118/
--Jeff Moden
Change is inevitable... Change for the better is not.
June 5, 2013 at 5:19 pm
Dird (6/5/2013)
Luis Cazares (6/5/2013)
The "Numbers" or "Tally" Table: What it is and how it replaces a loop.The problem with that is you'd have to create the table separately, when you said do it in tsql I read it as one action :s This is how you do it =P no way you'd do it over the phone though:
begin
WITH generator
AS (SELECT 1 AS id
UNION ALL
SELECT id + 1
FROM generator
WHERE id < 100)
select case
when (id %15)=0 then 'FizzBuzz'
when (id%3)=0 then 'Fizz'
when (id%5)=0 then 'Buzz'
else cast(id as varchar)
end
from generator
end
Simpler code and no RBAR (hidden or explicit)
WITH CTETally(n) AS(
SELECT TOP 100 ROW_NUMBER() OVER( ORDER BY object_id)
FROM sys.all_columns
)
select case
when (n %15)=0 then 'FizzBuzz'
when (n%3)=0 then 'Fizz'
when (n%5)=0 then 'Buzz'
else cast(n as varchar)
end
from CTETally
EDIT: Correct grammar.
June 5, 2013 at 10:01 pm
Dird (6/5/2013)
Lynn Pettis (6/5/2013)
Actually, something more like this:Seems more complex/messy. I think I'll remember my way in case I have a DBA interview (chance to see how little I know about MSSQL ^_^) in the next couple weeks & they decide to recycle some old coding questions `-`
Well, that is the SQL Server 2005 version building a dynamic tally table, here is the same code using the SQL Server 2008 and newer version:
WITH
E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS (
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
)
select
N,
nullif(case when N % 3 = 0 then 'fizz' else '' end + case when N % 5 = 0 then 'buzz' else '' end, '')
from
cteTally
where
N <= 100;
June 5, 2013 at 11:31 pm
Luis Cazares (6/5/2013)
Simpler code and no RBAR (hidden or explicit)
WITH CTETally(n) AS(
SELECT TOP 100 ROW_NUMBER() OVER( ORDER BY object_id)
FROM sys.all_columns
Yep that's how I was going to do it in the beginning; can't remember why I chose against it =)
June 6, 2013 at 7:47 am
Dird (6/5/2013)
Sean Lange (6/5/2013)
Dird (6/5/2013)
I would say that is a loop. A tally table can do this quite easily though.But then you have to create some pointless table and populate it. If you can't use a loop at all then which chump wants to insert 100 rows by hand -_ I added a cooler solution above this.
If you think a tally table pointless you REALLY need to read this...http://www.sqlservercentral.com/articles/62867/%5B/url%5D
I don't have to create this every time I need to use it. I keep it as a permanent table at all times.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 6, 2013 at 8:16 am
Sean Lange (6/6/2013)
Dird (6/5/2013)
I don't have to create this every time I need to use it. I keep it as a permanent table at all times.
But then the iztec way avoids disk reads
June 6, 2013 at 8:29 am
Dird (6/6/2013)
Sean Lange (6/6/2013)
Dird (6/5/2013)
I don't have to create this every time I need to use it. I keep it as a permanent table at all times.But then the iztec way avoids disk reads
iztec?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 6, 2013 at 8:32 am
Sean Lange (6/6/2013)
iztec?
itzik -_
June 6, 2013 at 8:48 am
Dird (6/6/2013)
Sean Lange (6/6/2013)
iztec?itzik -_
Gotcha. But what do you mean by "But then the iztec way avoids disk reads"? Do you mean by creating the tally table on the fly?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 6, 2013 at 8:49 am
Heh... FizzBuzz... my old friend during interviews. I don't ask about FizzBuzz on phone interviews. I do use it as one of the questions on the pre-interview written (5 question) test. It tells me almost everything I need to know about a candidate. Things like did they take their time to make the handwritten code legible or did they thoughtlessly scribble it out? Did they take the time to avoid hardcoding of the "100" to make the code more usable? If they did use a WHILE loop or {gasp!} rCTE to solve the problem, was it actually properly written?
Out of the last 20 developers I've interviewd, NONE of them has used a setbased method or a Tally Table. I can train people to be smarter but I can't fix stupid (as opposed to being ignorant of techniques), arrogant, or lazy. Getting the best answer for FizzBuzz isn't the reason why I ask the question (although it would be a real pleasure if someone actually did it right, for a change). It's all the other stuff you can learn from it even if someone has memorized the "correct" answer.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 16 through 30 (of 67 total)
You must be logged in to reply to this topic. Login to reply