SQL填写日期和数据

时间:2016-12-20 18:17:28

标签: sql-server date datetime

我有一个数据表和一个日历表。我需要填补空白,每天都有一排。我需要填写日期以及使用序列和操作的先前有效值。 NULL是有效的操作类型。我已经接近但是NULLS一直在搞砸我的结果。

我正在使用SQL Server 2014.非常感谢您的帮助。

数据:

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|

日历:

DateKey |                Date|
--------|--------------------|
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 |

期望的结果:

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|

用于获取测试表的TSQL:

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' )

1 个答案:

答案 0 :(得分:0)

这个SQL命令怎么样?

SELECT A.[ID]
      ,A.[SEQ]
      ,ISNULL(A.[Date], B.[Date]) AS 'DATE'
      ,A.[Action]
FROM DATA A FULL JOIN CALENDAR B 
     ON cast(A.[Date] as DATE) = cast(B.[Date] as DATE)
ORDER BY B.[date] ASC

FULL JOIN选项的结果:

ID  | SEQ | DATE                    | Action|
----|-----|-------------------------|-------|
A   | 1   | 2016-04-05 12:09:23.000 |yield  |
NULL| NULL| 2016-04-06 00:00:00.000 |NULL   |
A   | 2   | 2016-04-07 12:25:16.000 |stop   |
NULL| NULL| 2016-04-08 00:00:00.000 |NULL   |
NULL| NULL| 2016-04-09 00:00:00.000 |NULL   |
NULL| NULL| 2016-04-10 00:00:00.000 |NULL   |
NULL| NULL| 2016-04-11 00:00:00.000 |NULL   |
A   | 3   | 2016-04-12 11:25:42.000 |NULL   |
NULL| NULL| 2016-04-13 00:00:00.000 |NULL   |
NULL| NULL| 2016-04-14 00:00:00.000 |NULL   |
NULL| NULL| 2016-04-15 00:00:00.000 |NULL   |
NULL| NULL| 2016-04-16 00:00:00.000 |NULL   |
NULL| NULL| 2016-04-17 00:00:00.000 |NULL   |
A   | 4   | 2016-04-18 11:25:42.000 |go     |