Grouping Consecutive Values by Min and Max

MCTampa

Board Regular
Joined
Apr 14, 2016
Messages
97
I have the following table for which I would like to create groups of consecutive weeks by min and max values.

3301
3302
3303
3304
33033
33034
33035
33036
33037
33038
33039
33040
33041
33042
33043
33044
33045
33047
33048
33049
33050

<caption> Automatics </caption><thead>
[TH="bgcolor: #c0c0c0"] AREA Code [/TH]
[TH="bgcolor: #c0c0c0"] Week [/TH]

</thead><tbody>
</tbody><tfoot></tfoot>

The end result I would like is:

[TABLE="width: 215"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]Area Code[/TD]
[TD]Week Start[/TD]
[TD]Week End[/TD]
[/TR]
[TR]
[TD="align: right"]330[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]4[/TD]
[/TR]
[TR]
[TD="align: right"]330[/TD]
[TD="align: right"]33[/TD]
[TD="align: right"]45[/TD]
[/TR]
[TR]
[TD="align: right"]330[/TD]
[TD="align: right"]47[/TD]
[TD="align: right"]50[/TD]
[/TR]
</tbody>[/TABLE]

I'm trying to write this in SQL but many of the solutions I've found online reference using the ROW_NUMBER() function which Access does not recognize.

Can someone please help me out?
Thanks
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Note: This is relative to the area. So the groupings for area 330 will be independent of another area, but the logic will remain the same.
 
Upvote 0
Got the answer at Stack Overflow.
Thanks anyway!

<code>select area, min(week), max(week)
from (select t.*,
(select count(*)
from t as t2
where t2.area = t.area and t2.week <= t.week
) as seqnum
from t
) as t
group by area, (week - seqnum);</code>
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top