从datetime列创建两列

时间:2015-12-18 07:22:44

标签: sql-server datetime

所以我有一个关于从datetime列创建两列的问题。

我的例子

  CREATE TABLE #Test (ID int, OK bit, Data datetime)
INSERT INTO #Test VALUES (1,0,'Dec  1 2015 12:00AM')
INSERT INTO #Test VALUES (2,1,'Dec  2 2015 12:00AM')
INSERT INTO #Test VALUES (3,1,'Dec  3 2015 12:00AM')
INSERT INTO #Test VALUES (4,1,'Dec  4 2015 12:00AM')
INSERT INTO #Test VALUES (5,0,'Dec  5 2015 12:00AM')
INSERT INTO #Test VALUES (6,0,'Dec  6 2015 12:00AM')
INSERT INTO #Test VALUES (7,1,'Dec  7 2015 12:00AM')
INSERT INTO #Test VALUES (8,1,'Dec  8 2015 12:00AM')
INSERT INTO #Test VALUES (9,1,'Dec  9 2015 12:00AM')
INSERT INTO #Test VALUES (10,1,'Dec 10 2015 12:00AM')
INSERT INTO #Test VALUES (11,1,'Dec 11 2015 12:00AM')
INSERT INTO #Test VALUES (12,0,'Dec 12 2015 12:00AM')
INSERT INTO #Test VALUES (13,0,'Dec 13 2015 12:00AM')
INSERT INTO #Test VALUES (14,1,'Dec 14 2015 12:00AM')
INSERT INTO #Test VALUES (15,1,'Dec 15 2015 12:00AM')
INSERT INTO #Test VALUES (16,1,'Dec 16 2015 12:00AM')
INSERT INTO #Test VALUES (17,1,'Dec 17 2015 12:00AM')
INSERT INTO #Test VALUES (18,1,'Dec 18 2015 12:00AM')
INSERT INTO #Test VALUES (19,0,'Dec 19 2015 12:00AM')
INSERT INTO #Test VALUES (20,0,'Dec 20 2015 12:00AM')
INSERT INTO #Test VALUES (21,1,'Dec 21 2015 12:00AM')
INSERT INTO #Test VALUES (22,1,'Dec 22 2015 12:00AM')
INSERT INTO #Test VALUES (23,1,'Dec 23 2015 12:00AM')
INSERT INTO #Test VALUES (24,1,'Dec 24 2015 12:00AM')
INSERT INTO #Test VALUES (25,0,'Dec 25 2015 12:00AM')
INSERT INTO #Test VALUES (26,0,'Dec 26 2015 12:00AM')
INSERT INTO #Test VALUES (27,0,'Dec 27 2015 12:00AM')
INSERT INTO #Test VALUES (28,1,'Dec 28 2015 12:00AM')
INSERT INTO #Test VALUES (29,1,'Dec 29 2015 12:00AM')
INSERT INTO #Test VALUES (30,1,'Dec 30 2015 12:00AM')

产生以下结果

ID  OK  Data
--------------------
1   0   2015-12-01 00:00:00.000
2   1   2015-12-02 00:00:00.000
3   1   2015-12-03 00:00:00.000
4   1   2015-12-04 00:00:00.000
5   0   2015-12-05 00:00:00.000
6   0   2015-12-06 00:00:00.000
7   1   2015-12-07 00:00:00.000
8   1   2015-12-08 00:00:00.000
9   1   2015-12-09 00:00:00.000
10  1   2015-12-10 00:00:00.000
11  1   2015-12-11 00:00:00.000
12  0   2015-12-12 00:00:00.000
13  0   2015-12-13 00:00:00.000
14  1   2015-12-14 00:00:00.000
15  1   2015-12-15 00:00:00.000
16  1   2015-12-16 00:00:00.000
17  1   2015-12-17 00:00:00.000
18  1   2015-12-18 00:00:00.000
19  0   2015-12-19 00:00:00.000
20  0   2015-12-20 00:00:00.000
21  1   2015-12-21 00:00:00.000
22  1   2015-12-22 00:00:00.000
23  1   2015-12-23 00:00:00.000
24  1   2015-12-24 00:00:00.000
25  0   2015-12-25 00:00:00.000
26  0   2015-12-26 00:00:00.000
27  0   2015-12-27 00:00:00.000
28  1   2015-12-28 00:00:00.000
29  1   2015-12-29 00:00:00.000
30  1   2015-12-30 00:00:00.000

所以我的问题是如何在不使用光标的情况下创建两列表格。

输出应该是。我知道我可以通过使用游标来实现这一点但它必须是一种更简单的方法。有什么帮助吗?

    StartDate                 EndDate
    -----------------------   -----------------------
    2015-12-02 00:00:00.000 - 2015-12-04 00:00:00.000
    2015-12-07 00:00:00.000 - 2015-12-11 00:00:00.000
    2015-12-14 00:00:00.000 - 2015-12-18 00:00:00.000
    2015-12-21 00:00:00.000 - 2015-12-24 00:00:00.000
    2015-12-28 00:00:00.000 - 2015-12-30 00:00:00.000

1 个答案:

答案 0 :(得分:3)

您可以使用以下查询:

RecRef.GETTABLE("<Customer>");
//connect the payment method to the TemplateHeader and update the record:
//rekening
TemplateHeader.SETFILTER(TableID, FORMAT(18));
 IF TemplateHeader.Code = 'KNT-KLANT' THEN
TemplateHeader.SETFILTER(TemplateHeader.Code,'REK-KLANT');

//kontant
  IF TemplateHeader.Code = 'REK-KLANT' THEN
TemplateHeader.SETFILTER(TemplateHeader.Code,'KNT-KLANT' );

//automatische incasso
  IF TemplateHeader.Code  = 'BIN-INC-KL' THEN
TemplateHeader.SETFILTER(TemplateHeader.Code,'BIN-INC-KL' );
UpdateRecordTemplate.UpdateRecord(TemplateHeader, RecRef);

查询使用计算字段SELECT MIN(Data) AS StartDate, MAX(Data) AS EndDate FROM ( SELECT ID, OK, Data, DATEADD(d, -1 * ROW_NUMBER() OVER (PARTITION BY OK ORDER BY ID), Data) AS grp FROM Test) AS t WHERE OK = 1 GROUP BY grp 来标识具有相同grp切片的连续日期的连续记录的岛。

Demo here