I need a little help with one sql query. Suppose we have one table with data like this:Date ID-----------------4-1-2013 104-2-2013 104-3-2013 104-4-2013 104-5-2013 204-6-2013 204-7-2013 104-8-2013 104-9-2013 104-10-2013 30-----------------

Now, I would like to get min and max dates for each ID, but like this:ID min max------------------------------10 4-1-2013 4-4-201320 4-5-2013 4-6-201310 4-7-2013 4-9-201330 4-10-2013 4-10-2013-------------------------------

;WITH cte AS
(
SELECT *,
ROW_NUMBER() OVER (ORDER BY Date) -
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY date ) AS G
FROM
YourTable
)
SELECT
ID,
MIN(Date) AS [Min],
MAX(Date) AS [Max]
FROM
cte
GROUP BY
ID, G
ORDER BY
[Min],ID