Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Performing strange conditional COUNT Rate Topic Display Mode Topic Options
Author
 Message
 Posted Wednesday, March 06, 2013 6:12 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Thursday, January 16, 2014 1:23 PM Points: 105, Visits: 194
Post #1427714
 Posted Wednesday, March 06, 2013 7:39 PM
 Right there with Babe Group: General Forum Members Last Login: Today @ 3:18 PM Points: 742, Visits: 2,245
 HiThe third example you gave doesn't appear to conform with the rules use on the other examples. I would have expect 4 based on the previous examples`Example 1N,1,1,3,1,1,4,11---->2---->3->Example 2N,1,1,3,1,1,1,11---->2------->ExampleN,3,3,1,1,1,6,11>2>3------>4->`If my assumption is correct then you could do a count of differences greater than one and add one`SELECT COUNT(*) + 1 FROM #temp1 WHERE DataDifference > 1`If you want to assign a group then you could do something like the following quirky update. I may a some changes to your original query into #temp1 to accommodate it.`;With tblDifference as(Select Row_Number() OVER (Order by DateRecorded) as RowNumber,Name, DateRecorded from #m_TestTable)Select Cur.Name,Cur.DateRecorded as CurrentDay,Prv.DateRecorded as PreviousDay,Datediff(Day, Prv.DateRecorded, Cur.DateRecorded) as DateDifference,Cur.RowNumber,0 Grpinto #temp1FromtblDifference Cur Left Outer Join tblDifference PrvOn Cur.RowNumber = Prv.RowNumber + 1 Order by Cur.DateRecordedALTER TABLE #temp1 ALTER COLUMN RowNumber int NOT NULLALTER TABLE #temp1 ADD CONSTRAINT tmp_pk PRIMARY KEY (RowNumber)DECLARE @i AS INT = 1UPDATE #temp1SET Grp = @i, @i = @i + isnull(cast(cast(DateDifference - 1 as bit) as integer),0)SELECT * FROM #temp1`Hope this helps
Post #1427724
 Posted Thursday, March 07, 2013 12:41 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Thursday, January 16, 2014 1:23 PM Points: 105, Visits: 194
 Thanks for the prompt response. Yes your assumptions are correct. Can you clarify this statement. I think it contains some special charactersSELECT COUNT(*) + 1 FROM #temp1 WHERE DataDifference > 1What is "> 1" ?How do you mean "assign a group" ?Thanks.
Post #1427812
 Posted Thursday, March 07, 2013 1:38 AM
 SSCertifiable Group: General Forum Members Last Login: Thursday, March 06, 2014 9:46 AM Points: 6,641, Visits: 12,612
 This is called "Islands and Gaps". An island is a set of rows where the dates are consecutive, bounded by rows with non-consecutive dates. Jeff Moden has a great Spackle article covering the subject in some detail. Once you've identified the islands, what do you want to do?Edit: clarity “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps
Post #1427824
 Posted Thursday, March 07, 2013 5:49 AM
 Hall of Fame Group: General Forum Members Last Login: Yesterday @ 8:02 PM Points: 3,512, Visits: 4,934
 ChrisM@Work (3/7/2013)This is called "Islands and Gaps". An island is a set of rows where the dates are consecutive, bounded by rows with non-consecutive dates. Jeff Moden has a great Spackle article covering the subject in some detail. Once you've identified the islands, what do you want to do?Edit: clarityThat's a great article Chris! Oldey but goody.Makes me think of something like this. `WITH SampleData AS (SELECT Name, RecordedDateFROM (Values ('John', dateadd(day,1,GetDate())),('John', dateadd(day,2,GetDate())),('John', dateadd(day,3,GetDate())),('John', dateadd(day,6,GetDate())),('John', dateadd(day,7,GetDate())),('John', dateadd(day,8,GetDate())), ('John', dateadd(day,12,GetDate())),('John', dateadd(day,13,GetDate())))a (Name, RecordedDate))SELECT Name, COUNT(*)FROM ( SELECT Name FROM ( SELECT Name, RecordedDate ,rn=RecordedDate-ROW_NUMBER() OVER (PARTITION BY Name ORDER BY RecordedDate) FROM SampleData) a GROUP BY Name, rn) bGROUP BY Name`Although I couldn't think of a good name for the second column. [Count of islands] perhaps? 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 #1427919
 Posted Thursday, March 07, 2013 6:04 AM
 SSCertifiable Group: General Forum Members Last Login: Thursday, March 06, 2014 9:46 AM Points: 6,641, Visits: 12,612
 Nice one geezer It makes for a surprisingly succinct query. “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps
Post #1427928
 Posted Thursday, March 07, 2013 7:50 AM
 Hall of Fame Group: General Forum Members Last Login: Yesterday @ 8:02 PM Points: 3,512, Visits: 4,934
 ChrisM@Work (3/7/2013)Nice one geezer It makes for a surprisingly succinct query.Good thing I'm not getting paid by the keystroke. 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 #1427989
 Posted Thursday, March 07, 2013 11:05 AM
 Right there with Babe Group: General Forum Members Last Login: Today @ 3:18 PM Points: 742, Visits: 2,245
 sqlislife (3/7/2013)Thanks for the prompt response. Yes your assumptions are correct. Can you clarify this statement. I think it contains some special charactersSELECT COUNT(*) + 1 FROM #temp1 WHERE DataDifference > 1What is "> 1" ?How do you mean "assign a group" ?Thanks.Sorry about that the greater than symbol > appears ok on mine
Post #1428148
 Posted Thursday, March 07, 2013 11:07 AM
 Right there with Babe Group: General Forum Members Last Login: Today @ 3:18 PM Points: 742, Visits: 2,245
 ChrisM@Work (3/7/2013)This is called "Islands and Gaps". An island is a set of rows where the dates are consecutive, bounded by rows with non-consecutive dates. Jeff Moden has a great Spackle article covering the subject in some detail. Once you've identified the islands, what do you want to do?Have to agree ... a great article. Was concentrating on the dateDifference column, so totally over looked this.
Post #1428152
 Posted Thursday, March 07, 2013 11:20 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Thursday, January 16, 2014 1:23 PM Points: 105, Visits: 194
 Wow, interesting. Nice article by Jeff as always. Thanks guys. I will modify this to suit my needs.
Post #1428161

 Permissions