## Performing strange conditional COUNT

 Author Message sqlislife SSC-Addicted Group: General Forum Members Points: 465 Visits: 198 Hey guys, Can someone show me how to go about solving this problem? I thought it would be pretty straightforward but turns out it's not. I made some strides but have been stuck on the last portion. Here is what I have so far. I have a table with the following data inside. Create table #m_TestTable (Name varchar(20),DateRecorded datetime)----Insert sample dataInsert into #m_TestTable Values ('John', dateadd(day,1,GetDate()))Insert into #m_TestTable Values ('John', dateadd(day,2,GetDate()))Insert into #m_TestTable Values ('John', dateadd(day,3,GetDate()))Insert into #m_TestTable Values ('John', dateadd(day,6,GetDate()))Insert into #m_TestTable Values ('John', dateadd(day,7,GetDate()))Insert into #m_TestTable Values ('John', dateadd(day,8,GetDate()))Insert into #m_TestTable Values ('John', dateadd(day,12,GetDate()))Insert into #m_TestTable Values ('John', dateadd(day,13,GetDate()))The idea is once the difference between the previous day and the current day is greater than or equal to 1, it should be counted as 1. I use the following query to get the difference in date between the current row and the previous row;With tblDifference as(Select Row_Number() OVER (Order by DateRecorded) as RowNumber,Name, DateRecorded,PointValue from #m_TestTable)Select Cur.Name,Cur.DateRecorded as CurrentDay,Prv.DateRecorded as PreviousDay,Datediff(Day, Prv.DateRecorded, Cur.DateRecorded) as DateDifferenceInto #temp1 FromtblDifference Cur Left Outer Join tblDifference PrvOn Cur.RowNumber = Prv.RowNumber + 1Order by Cur.DateRecorded--select * from #temp1Here is where I get stuck. Based on the datedifference column, I need to perform a count. The datedifference column data appears in following order. NULL, 1, 1, 3, 1, 1, 4, 1So the count for John should be 3 broken down like this. NULL, 1, 1 should count as ONE; 3, 1, 1 should count as ONE and 4, 1 should count as ONE. --But if the data was like this Create table #m_TestTable1 (Name varchar(20),DateRecorded datetime)----Insert sample dataInsert into #m_TestTable1 Values ('John', dateadd(day,1,GetDate()))Insert into #m_TestTable1 Values ('John', dateadd(day,2,GetDate()))Insert into #m_TestTable1 Values ('John', dateadd(day,3,GetDate()))Insert into #m_TestTable1 Values ('John', dateadd(day,6,GetDate()))Insert into #m_TestTable1 Values ('John', dateadd(day,7,GetDate()))Insert into #m_TestTable1 Values ('John', dateadd(day,8,GetDate()))Insert into #m_TestTable1 Values ('John', dateadd(day,9,GetDate()))Insert into #m_TestTable1 Values ('John', dateadd(day,10,GetDate()));With tblDifference as(Select Row_Number() OVER (Order by DateRecorded) as RowNumber,Name, DateRecorded from #m_TestTable1)Select Cur.Name,Cur.DateRecorded as CurrentDay,Prv.DateRecorded as PreviousDay,Datediff(Day, Prv.DateRecorded, Cur.DateRecorded) as DateDifferenceInto #temp2 FromtblDifference Cur Left Outer Join tblDifference PrvOn Cur.RowNumber = Prv.RowNumber + 1Order by Cur.DateRecorded--select * from #temp2The datedifference column data appears in following order. NULL, 1, 1, 3, 1, 1, 1, 1Then in this case the count for John would be only 2. NULL, 1, 1 would count as ONE, 3, 1, 1, 1, 1 would count as ONE. In summary, if there are '1's after a number, they count as ONE. If the number is anything other than 1 and is followed by '1's, it will count as ONE. If the number is anything other than one and is followed by non 1 numbers, they count as one each. To illustrate this last point, if the data was like thisCreate table #m_TestTable2(Name varchar(20),DateRecorded datetime)----Insert sample dataInsert into #m_TestTable2 Values ('John', dateadd(day,1,GetDate()))Insert into #m_TestTable2 Values ('John', dateadd(day,4,GetDate()))Insert into #m_TestTable2 Values ('John', dateadd(day,7,GetDate()))Insert into #m_TestTable2 Values ('John', dateadd(day,8,GetDate()))Insert into #m_TestTable2 Values ('John', dateadd(day,9,GetDate()))Insert into #m_TestTable2 Values ('John', dateadd(day,10,GetDate()))Insert into #m_TestTable2 Values ('John', dateadd(day,16,GetDate()))Insert into #m_TestTable2 Values ('John', dateadd(day,17,GetDate()));With tblDifference as(Select Row_Number() OVER (Order by DateRecorded) as RowNumber,Name, DateRecorded from #m_TestTable2)Select Cur.Name,Cur.DateRecorded as CurrentDay,Prv.DateRecorded as PreviousDay,Datediff(Day, Prv.DateRecorded, Cur.DateRecorded) as DateDifferenceInto #temp3 FromtblDifference Cur Left Outer Join tblDifference PrvOn Cur.RowNumber = Prv.RowNumber + 1Order by Cur.DateRecorded--select * from #temp3The datedifference column data appears in following order. NULL, 3, 3, 1, 1, 1, 6, 1Then the count for John would be 3. NULL, 3, will count as ONE, 3, 1, 1, 1 will count as ONE and 6, 1 will count as 1. I hope this makes sense. Thanks for your time. I know this is a strange one. mickyT SSCertifiable Group: General Forum Members Points: 5814 Visits: 3320 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 sqlislife SSC-Addicted Group: General Forum Members Points: 465 Visits: 198 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. ChrisM@Work SSC Guru Group: General Forum Members Points: 97540 Visits: 20698 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 dwain.c SSC-Forever Group: General Forum Members Points: 44525 Visits: 6431 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. :-P`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!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables ChrisM@Work SSC Guru Group: General Forum Members Points: 97540 Visits: 20698 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 dwain.c SSC-Forever Group: General Forum Members Points: 44525 Visits: 6431 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!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables mickyT SSCertifiable Group: General Forum Members Points: 5814 Visits: 3320 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 mickyT SSCertifiable Group: General Forum Members Points: 5814 Visits: 3320 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. sqlislife SSC-Addicted Group: General Forum Members Points: 465 Visits: 198 Wow, interesting. Nice article by Jeff as always. Thanks guys. I will modify this to suit my needs.