Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Query help filling in dates and data

$
0
0
I have a data table and a calendar table. I need to fill in the gaps to have a row for each day. I need the dates to be filled in as well as to use the previous valid value for sequence and action. NULL is a valid action type. I've gotten close but the NULLS have been screwing with my results.I'm using SQL Server 2014. Your help is much appreciated.Data:[code="other"]+---------------------------------------------+| ID | Sequence| Date| Action| |+---------------------------------------------+| ---|---------|--------------------|-------| || A | 1 | 4/5/2016 12:09:23 | yield| || A | 2 | 4/7/2016 12:25:16 | stop| || A | 3 | 4/12/2016 11:25:42| NULL| || A | 4 | 4/18/2016 11:25:42| go| || B | 1 | 2/17/2016 14:15:10| yield| || B | 2 | 3/1/2016 7:56:37 | stop| || B | 3 | 4/1/2016 9:24:46 | go| || B | 4 | 5/4/2016 12:25:16 | exit| |+---------------------------------------------+[/code]Calendar:[code="other"]+---------+-------------------------+| DateKey | Date |+---------+-------------------------+| 2838 | 2016-04-06 00:00:00.000 || 2839 | 2016-04-07 00:00:00.000 || 2840 | 2016-04-08 00:00:00.000 || 2841 | 2016-04-09 00:00:00.000 || 2842 | 2016-04-10 00:00:00.000 || 2843 | 2016-04-11 00:00:00.000 || 2844 | 2016-04-12 00:00:00.000 || 2845 | 2016-04-13 00:00:00.000 || 2846 | 2016-04-14 00:00:00.000 || 2847 | 2016-04-15 00:00:00.000 || 2848 | 2016-04-16 00:00:00.000 || 2849 | 2016-04-17 00:00:00.000 || 2850 | 2016-04-18 00:00:00.000 |+---------+-------------------------+[/code]Desired results:[code="other"]ID | Sequence| Date| Action|----|---------|--------------------|----------|A | 1| 4/5/2016 12:09:23| yield|A | 1| 4/6/2016 0:00:00| yield|A | 2| 4/7/2016 12:25:16| stop|A | 2| 4/8/2016 0:00:00| stop|A | 2| 4/9/2016 0:00:00| stop|A | 2| 4/10/2016 0:00:00| stop|A | 2| 4/11/2016 0:00:00| stop|A | 3| 4/12/2016 10:35:34| NULL|A | 3| 4/13/2016 0:00:00| NULL|A | 3| 4/14/2016 0:00:00| NULL|A | 3| 4/15/2016 0:00:00| NULL|A | 3| 4/16/2016 0:00:00| NULL|A | 3| 4/17/2016 0:00:00| NULL|A | 4| 4/18/2016 11:25:4| go|[/code]TSQL to get test tables up:[code="sql"]IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Data') DROP TABLE Data;CREATE TABLE Data (ID varchar(20), SEQ INT, Date datetime, Action varchar(20));INSERT INTO data (ID, SEQ, Date, Action) VALUES('A', 1, '4/5/2016 12:09:23','yield' ),('A', 2, '4/7/2016 12:25:16','stop' ),('A', 3, '4/12/2016 11:25:42','NULL' ),('A', 4, '4/18/2016 11:25:42','go' )IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Calendar') DROP TABLE Calendar;CREATE TABLE Calendar (DateKey int, Date datetime);INSERT INTO Calendar (DateKey, Date) VALUES('2838','4/6/2016 0:00:00'),('2839','4/7/2016 0:00:00' ),('2840', '4/8/2016 0:00:00' ),('2841', '4/9/2016 0:00:00' ),('2842', '4/10/2016 0:00:00'),('2843', '4/11/2016 0:00:00' ),('2844', '4/12/2016 0:00:00' ),('2845', '4/13/2016 0:00:00' ),('2846', '4/14/2016 0:00:00' ),('2847', '4/15/2016 0:00:00'),('2848', '4/16/2016 0:00:00' ),('2849', '4/17/2016 0:00:00' ),('2850', '4/18/2016 0:00:00' )[/code]

Viewing all articles
Browse latest Browse all 3145

Trending Articles