I have a table that contains categories, dates and bids. Each category can have different bids for different dates, one category can have only one bid for a certain date.
Id CatId Date Rate ------ ------ ------------ --------- 000001 12 2009-07-07 1 000002 12 2009-07-08 1 000003 12 2009-07-09 1 000004 12 2009-07-10 2 000005 12 2009-07-15 1 000006 12 2009-07-16 1 000007 13 2009-07-08 1 000008 13 2009-07-09 1 000009 14 2009-07-07 2 000010 14 2009-07-08 1 000010 14 2009-07-10 1
Unique index (catid, Date, Rate) I would like each category to group all ranges of continuous dates and keep only the beginning and end of the range. In the previous example, we would have:
CatId Begin End Rate
I found a similar solution in the forum that definitely didnβt give a result
WITH q AS ( SELECT *, ROW_NUMBER() OVER (PARTITION BY CatId, Rate ORDER BY [Date]) AS rnd, ROW_NUMBER() OVER (PARTITION BY CatId ORDER BY [Date]) AS rn FROM my_table ) SELECT CatId AS catidd, MIN([Date]) as beginn, MAX([Date])as endd, Rate FROM q GROUP BY CatId, rnd - rn, Rate
CM. SQL FIDDLE How can I do the same in mysql? Please, help!
Fouzi
source share