November 30, 2011 at 8:39 am
Thank you both. The expanding with Numbers table was my idea as well and I see on the second page of pointed discussion that this was the approach taken by Jeff. I am now looking at Itzik's article.
February 18, 2012 at 11:28 am
nice article Jeff!
thanks!!!!
February 18, 2012 at 11:57 am
rfr.ferrari (2/18/2012)
nice article Jeff!thanks!!!!
You bet. Thank you for the feedback.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 27, 2012 at 7:07 pm
OMG! :w00t:
I must have read this article 5 times at least and I never could quite get a grip on it.
Finally, I've been able to apply it to a real problem! http://www.sqlservercentral.com/Forums/Topic1364849-392-1.aspx?Update=1 Not that I doubted its applicability, just couldn't quite achieve that nirvana of understanding.
Not sure that I have yet, but at least this is a start. 😎
As always, thanks Jeff!
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
September 27, 2012 at 10:26 pm
Jeff Moden (1/16/2011)
Sachin Nandanwar (1/16/2011)
Well I just stumbled upon this article.I tried to do it using quirky update method and seems to be working but haven't tested it on a huge no of rows though.
--=============================================================================
-- Create the test data. This is NOT a part of the solution.
-- This is virually instantaneous.
--=============================================================================
--===== Conditionally drop the test table to make reruns easier.
IF OBJECT_ID('tempdb..#MyHead','U') IS NOT NULL
DROP TABLE #MyHead
;
GO
--===== Create the test table
CREATE TABLE #MyHead
(SomeDate DATETIME, id int DEFAULT(0))
;
--===== Populate the test table with test data
INSERT INTO #MyHead
(SomeDate)
SELECT '2010-01-01' UNION ALL --1st "Group" of dates (StartDate and EndDate)
SELECT '2010-01-01' UNION ALL --Duplicate date
SELECT '2010-01-03' UNION ALL --2nd "Group" of dates (StartDate and EndDate)
SELECT '2010-01-05' UNION ALL --3rd "Group" of dates (StartDate)
SELECT '2010-01-06' UNION ALL --3rd "Group" of dates (EndDate)
SELECT '2010-01-10' UNION ALL --4th "Group" of dates (StartDate)
SELECT '2010-01-10' UNION ALL --Duplicate date
SELECT '2010-01-11' UNION ALL --4th "Group" of dates
SELECT '2010-01-11' UNION ALL --Duplicate date
SELECT '2010-01-11' UNION ALL --Duplicate date
SELECT '2010-01-12' --4th "Group" of dates (EndDate)
;
declare @ordse int=0
declare @somedate datetime=''
update #MyHead set @ordse=ID=case when somedate=@somedate+1 or @somedate=somedate then @ordse+1 else @ordse-1 end,@somedate=somedate
select min(somedate)min,max(somedate)max,DATEDIFF(dd,min(SomeDate)-1,max(SomeDate))Diff from
(
select *,id-ROW_NUMBER()over(order by (select 1))id1 from #MyHead
)t group by id1 order by min(SomeDate)
drop table #MyHead
I realize the intentions are good here and thank you for that but there are a couple of problems with the code there. For one, it breaks several of the rules for doing a Quirky Update. It's tough enough for me to defend the use of the Quirky Update as it is. If you're going to use it and post such solutions, please follow the rules for its use. Thanks.
Second, although the Quirky Update does the job, isn't a panacea and there's simply no need no need for it here. It requires the use of an extra column and would necessarily require the copying of data from a permanent table to a Temp Table if the column couldn't be added to the permanent table.
Last but not least, since you still do a SELECT with aggregates, I believe you'll find that the Quirky Update method is actually a bit slower than conventional methods, in this case.
I can confirm the last statement, namely that QU performs slower than Jeff's method.
Because I was so conceptually challenged to understand what Jeff had done at first, I tried to see if I could apply QU to this case. While I did get it to work (QU I understand, including the rules :-)), it was definitely slower.
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
October 21, 2012 at 6:45 pm
Sorry for the late reply, Dwain. Thanks for the research and the confirmation.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 30, 2012 at 9:14 pm
Very nice, thanks for the article. CTE's can be really confusing.
November 30, 2012 at 11:15 pm
Thank you for the feedback, Nelson, and welcome aboard!
--Jeff Moden
Change is inevitable... Change for the better is not.
October 4, 2013 at 1:18 am
how a simply clever trick just blows your mind.
I wonder how many people only read this Spackle once and really got it.
I just copied it to ssms and walked it through reading it, sat back and sipped my tea ( as if I were Sherlok Holmes handling a one pipe problem ) .
Not the first time Jeff succeeds in astonishing me with a simple solution for a quite commom problem.
Thanks again for sharing it all, Jeff
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution 😀
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
October 4, 2013 at 2:52 am
Nice article!
I immediately applied the code (with minor changes) on another database, and it worked!
Thanks Jeff.
Regards,
IgorMi
Igor Micev,My blog: www.igormicev.com
October 4, 2013 at 7:56 am
Thanks for the feedback, Johan. It's always a pleasure to hear from you.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 4, 2013 at 7:57 am
IgorMi (10/4/2013)
Nice article!I immediately applied the code (with minor changes) on another database, and it worked!
Thanks Jeff.
Regards,
IgorMi
Very cool! Thanks for the feedback. I love success stories.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 6, 2013 at 6:03 pm
I skimmed the article but I couldn't really identify what's new in this re-publish.
I don't suppose you could give us a quick synopsis Jeff?
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
June 28, 2014 at 10:49 am
dwain.c (10/6/2013)
I skimmed the article but I couldn't really identify what's new in this re-publish.I don't suppose you could give us a quick synopsis Jeff?
Apologies for the VERY late reply, Dwain. No doubt that by now you've figured out that SQL Server Central frequently republishes articles that have had no changes at all. To wit, most articles that show up on Fridays were previously published.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 29, 2014 at 9:42 pm
Jeff Moden (6/28/2014)
dwain.c (10/6/2013)
I skimmed the article but I couldn't really identify what's new in this re-publish.I don't suppose you could give us a quick synopsis Jeff?
Apologies for the VERY late reply, Dwain. No doubt that by now you've figured out that SQL Server Central frequently republishes articles that have had no changes at all. To wit, most articles that show up on Fridays were previously published.
Better late than never.
Had that happen to one of mine, so I know the drill.
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 15 posts - 46 through 60 (of 62 total)
You must be logged in to reply to this topic. Login to reply