sql - How to calculate the longest winning streak with daterange -
i have table in sql server consists of data like
userid amount startdate enddate game result ------------------------------------------------------------------------------- 68838 51.00 2014-05-29 15:41:41.167 2014-05-29 15:41:41.167 1 w 68838 51.00 2014-05-29 15:42:30.757 2014-05-29 15:42:30.757 1 w 68838 -0.50 2014-05-31 16:57:31.033 2014-05-31 16:57:31.033 1 l 68838 -0.50 2014-05-31 17:05:31.023 2014-05-31 17:05:31.023 1 l 68838 -0.50 2014-05-31 17:22:03.857 2014-05-31 17:22:03.857 1 l 68838 0.42 2014-05-31 17:26:32.570 2014-05-31 17:26:32.570 1 w 68838 0.42 2014-05-31 17:34:45.330 2014-05-31 17:34:45.330 1 w 68838 0.42 2014-05-31 17:38:44.107 2014-05-31 17:38:44.107 1 w 68838 0.42 2014-05-31 17:42:12.790 2014-05-31 17:42:12.790 1 w 434278 0.42 2014-05-31 16:57:31.033 2014-05-31 16:57:31.033 1 w 434278 0.42 2014-05-31 17:05:31.023 2014-05-31 17:05:31.023 1 w 434278 0.42 2014-05-31 17:22:03.857 2014-05-31 17:22:03.857 1 w 434278 -0.50 2014-05-31 17:26:32.570 2014-05-31 17:26:32.570 1 l 434278 -0.50 2014-05-31 17:34:45.330 2014-05-31 17:34:45.330 1 l 434278 -0.50 2014-05-31 17:38:44.107 2014-05-31 17:38:44.107 1 l 434278 -0.50 2014-05-31 17:42:12.790 2014-05-31 17:42:12.790 1 l 434278 0.42 2014-05-31 17:46:40.723 2014-05-31 17:46:40.723 1 w 434278 -0.50 2014-05-31 17:51:26.190 2014-05-31 17:51:26.190 1 l 434278 0.42 2014-05-31 17:55:32.870 2014-05-31 17:55:32.870 1 w 434278 -4.00 2014-05-31 18:06:54.937 2014-05-31 18:06:54.937 1 l 434278 -2.00 2014-05-31 18:19:29.483 2014-05-31 18:19:29.483 1 l
i want result this, showing longest winning streak each users
userid startdate enddate streak amount -------------------------------------------------------------------- 68838 2014-05-31 17:26:32:570 2014-05-31 17:42:12:570 4 1.68 434278 2014-05-31 16:57:31:033 2014-05-31 17:22:03:857 3 1.26
disclaimer: glenn's answer great answer, , lot of heavy lifting you, doesn't go way doing asked for. going post answer i'd been working on, when came add it, saw glenn's way of doing heavy lifting better way of doing it, reworked answer include way of doing it. urge accept answer, rather mine.
the below should asked for.
select userid, min_startdate startdate, max_enddate enddate, max_group_count streak, sum_amount amount ( select *, -- want earliest maximum streak max(min_startdate) on (partition userid) earliest_startdate ( select *, -- want maximum streak max(max_group_count) on (partition userid) max_streak ( select distinct userid, -- calculate streak count(grouping) on (partition userid, grouping order startdate rows between unbounded preceding , unbounded following ) max_group_count -- calcualte start date of streak ,min(startdate) on (partition userid, grouping order startdate rows between unbounded preceding , unbounded following ) min_startdate -- calcualte end date of streak ,max(enddate) on (partition userid, grouping order startdate rows between unbounded preceding , unbounded following ) max_enddate -- calcualte total amount ,sum(amount) on (partition userid, grouping order startdate rows between unbounded preceding , unbounded following ) sum_amount ( select * -- assign group number streak, can group ,sum(case when result <> prev_result 1 else 0 end) on (partition userid order startdate) grouping ( select * -- want @ previous record determin when -- winning/loosing streak starts , ends ,coalesce(lag(result) on (partition userid order startdate), result) prev_result game ) result = 'w' ) b ) c ) d max_group_count = max_streak ) e min_startdate = earliest_startdate
the output of is:
| userid | startdate | enddate | streak | amount | |--------|----------------------------|----------------------------|--------|--------| | 68838 | may, 31 2014 17:26:32+0000 | may, 31 2014 17:42:12+0000 | 4 | 1.68 | | 434278 | may, 31 2014 16:57:31+0000 | may, 31 2014 17:22:03+0000 | 3 | 1.26 |
i've included sql fiddle can play if fancy: http://sqlfiddle.com/#!6/32777/36/0
Comments
Post a Comment