I have an existing @table
ID Date Val
1 2014-10-01 1
2 2014-10-02 1
3 2014-10-03 1
4 2014-10-04 1
5 2014-10-05 1
6 2014-10-06 1
7 2014-10-07 1
8 2014-10-08 1
9 2014-10-09 1
The Date sequence is of importance. I need to see the first and last date for each Val sequence:
Select Val,MIN([Date]) as A, MAX([Date]) as B
from @T
Group by Val
Val From To
1 2014-10-01 2014-10-09
The Val column now changes for 3 of these entries:
Update @T set Val = 2 where [ID] between 3 and 5
and returns:
ID Date Val
1 2014-10-01 1
2 2014-10-02 1
3 2014-10-03 2
4 2014-10-04 2
5 2014-10-05 2
6 2014-10-06 1
7 2014-10-07 1
8 2014-10-08 1
9 2014-10-09 1
How do I get SQL to return the min/max dates per sequence?
I need to show :
i.e.
1 2014-10-01 2014-10-02
2 2014-10-03 2014-10-05
1 2014-10-06 2014-10-09
If I run the normal Min/Max query, I get
1 2014-10-01 2014-10-09
2 2014-10-03 2014-10-05
Which does not show me that the val was 1 for 1st period,2 for 2nd period and again 1 for 3rd period
declare @T table(ID int,[Date] date,Val int)
Insert Into @T(ID,[Date],Val)
values(1,'2014/10/01', 1),
(2,'2014/10/02', 1),
(3,'2014/10/03', 1),
(4,'2014/10/04', 1),
(5,'2014/10/05', 1),
(6,'2014/10/06', 1),
(7,'2014/10/07', 1),
(8,'2014/10/08', 1),
(9,'2014/10/09', 1)