The question is how long have these customers been jerks on any given date.
I’m working against Sybase
For this simplified table structure of table history_data
table: history_of_jerkiness processing_date name is_jerk --------------- ----- ------- 20090101 Matt true 20090101 Bob false 20090101 Alex true 20090101 Carol true 20090102 Matt true 20090102 Bob true 20090102 Alex false 20090102 Carol true 20090103 Matt true 20090103 Bob true 20090103 Alex true 20090103 Carol false
The report for the 3rd should show that Matt has always been a jerk, Alex has just become a jerk, and Bob has been a jerk for 2 days.
name days jerky ----- ---------- Matt 3 Bob 2 Alex 1
I’d like to find these spans of time dynamically, so if I run the report for the 2nd, I should get different results:
name days_jerky ----- ---------- Matt 2 Bob 1 Carol 2
The key here is trying to find only continuous spans older than a certain date. I’ve found a few leads, but it seems like a problem where there would be very smart tricky solutions.
My solution from SQL Server – same as Dems but I put in a min baseline myself. It assumes there are no gaps – that is there is an entry for each day for each person. If that isn’t true then I’d have to loop.
I created the test table with the following: