datetime - MySQL - convert timed event rows to ranges of states -
datetime - MySQL - convert timed event rows to ranges of states -
let's house gets painted every often, , track changes (the 'paint' event) in simple table:
_______________________ / paintdate | newcolor \ |-----------|-----------| | 1/2/2012 | reddish | | 3/5/2013 | bluish | | 9/9/2013 | greenish | \___________|___________/
is there select statement can give me table of the date ranges house @ specific color?
desired output:
_______________________________ / | | color \ |----------|----------|---------| | 1/2/2012 | 3/5/2013 | reddish | | 3/5/2013 | 9/9/2013 | bluish | | 9/9/2013 | null | greenish | -- not repainted yet, date in 'to' column should null \__________|__________|_________/
well can utilize query
select p.paintdate `from`, l.paintdate `to`, p.newcolor `color` paint p left bring together (select * paint limit 1, 18446744073709551615) l on l.paintdate = (select paintdate paint paintdate > p.paintdate order paintdate asc limit 1)
output
from | | color ------------------------------- 2012-02-01 | 2012-05-03 | reddish 2012-05-03 | 2013-09-09 | bluish 2013-09-09 | (null) | greenish
demo
the above query can improved this
select p.paintdate `from`, l.paintdate `to`, p.newcolor `color` paint p left bring together paint l on l.paintdate = (select paintdate paint paintdate > p.paintdate order paintdate asc limit 1)
demo
mysql datetime select
Comments
Post a Comment