如何将一个日期列拆分为两列,并从开始日期到最后一个日期获取数据

时间:2015-08-31 13:26:16

标签: sql sql-server tsql

如何将列拆分为两个并在第一个日期和最后一个日期之间显示数据而不重复数据?

我正在研究一个SQL Server数据库我想从我的表中显示三列。我尝试过很多方面,但没有做到需要的。原始表格如下:

Refno       TransactionDate          Code
-----------------------------------------
L820774729  2012-09-01 00:00:00.000  99
L820774729  2012-09-01 00:00:00.000  99
L820774729  2012-10-01 00:00:00.000  99
L820774729  2012-10-01 00:00:00.000  99
L820774729  2012-12-01 00:00:00.000  99
L820774729  2013-01-01 00:00:00.000  99
L820774729  2013-02-01 00:00:00.000  99
L820774729  2013-03-01 00:00:00.000  99
L820774729  2013-03-01 00:00:00.000  99
L820774729  2013-04-01 00:00:00.000  99
L820774729  2013-06-01 00:00:00.000  99
L820774729  2013-07-01 00:00:00.000  99
L820774729  2013-09-01 00:00:00.000  99
L820774729  2013-10-01 00:00:00.000  99
L820774729  2013-10-01 00:00:00.000  99
L820774729  2013-11-01 00:00:00.000  2
L820774729  2013-11-01 00:00:00.000  2
L820774729  2013-11-01 00:00:00.000  99
L820774729  2013-11-01 00:00:00.000  99
L820774729  2013-12-01 00:00:00.000  2
L820774729  2014-01-01 00:00:00.000  2
L820774729  2014-02-01 00:00:00.000  2
L820774729  2014-03-01 00:00:00.000  2
L820774729  2014-04-01 00:00:00.000  2
L820774729  2014-05-01 00:00:00.000  2
L820774729  2014-06-01 00:00:00.000  2
L820774729  2014-07-01 00:00:00.000  2
L820774729  2014-08-01 00:00:00.000  2
L820774729  2014-08-01 00:00:00.000  2
L820774729  2014-08-01 00:00:00.000  2
L820774729  2014-09-01 00:00:00.000  2 
L820774729  2014-10-01 00:00:00.000  2
L820774729  2014-11-01 00:00:00.000  1
L820774729  2014-12-01 00:00:00.000  1
L820774729  2015-01-01 00:00:00.000  1
L820774729  2015-02-01 00:00:00.000  1
L820774729  2015-03-01 00:00:00.000  1
L820774729  2015-04-01 00:00:00.000  1
L820774729  2015-05-01 00:00:00.000  1
L820774729  2015-06-01 00:00:00.000  1

所需的输出是这样的。

From      To        Code
------------------------
Sep 2012  Oct 2013  99
Nov 2013  Nov 2013  2
Nov 2013  Nov 2013  99
Dec 2013  Oct 2014  2
Nov 2014  Jun 2015  1

但我目前的代码是这样的

SELECT 
    [Refno]As Ref,
    [TransactionDate] As [From],
    (SELECT MIN(TransactionDate) 
     FROM LevyTransactions AS LlevyTransactions
     WHERE LlevyTransactions.TransactionDate > LevyTransactions.TransactionDate) AS [To],
    [Code]
FROM 
    dbo.LevyTransactions 
WHERE 
    Refno = 'L82077479'

我当前的输出是这样的。

From                     To                       Code
------------------------------------------------------
2012-09-01 00:00:00.000  2012-10-01 00:00:00.000  99
2012-09-01 00:00:00.000  2012-10-01 00:00:00.000  99 
2012-10-01 00:00:00.000  2012-11-01 00:00:00.000  99
2012-10-01 00:00:00.000  2012-11-01 00:00:00.000  99
2012-12-01 00:00:00.000  2013-01-01 00:00:00.000  99
2013-01-01 00:00:00.000  2013-02-01 00:00:00.000  99
2013-02-01 00:00:00.000  2013-03-01 00:00:00.000  99
2013-03-01 00:00:00.000  2013-04-01 00:00:00.000  99
2013-03-01 00:00:00.000  2013-04-01 00:00:00.000  99
2013-04-01 00:00:00.000  2013-05-01 00:00:00.000  99
2013-06-01 00:00:00.000  2013-07-01 00:00:00.000  99
2013-07-01 00:00:00.000  2013-08-01 00:00:00.000  99
2013-09-01 00:00:00.000  2013-10-01 00:00:00.000  99
2013-10-01 00:00:00.000  2013-11-01 00:00:00.000  99
2013-10-01 00:00:00.000  2013-11-01 00:00:00.000  99
2013-11-01 00:00:00.000  2013-12-01 00:00:00.000  2
2013-11-01 00:00:00.000  2013-12-01 00:00:00.000  2
2013-11-01 00:00:00.000  2013-12-01 00:00:00.000  99
2013-11-01 00:00:00.000  2013-12-01 00:00:00.000  99
2013-12-01 00:00:00.000  2014-01-01 00:00:00.000  2
2014-01-01 00:00:00.000  2014-02-01 00:00:00.000  2
2014-02-01 00:00:00.000  2014-03-01 00:00:00.000  2
2014-03-01 00:00:00.000  2014-04-01 00:00:00.000  2
2014-04-01 00:00:00.000  2014-05-01 00:00:00.000  2
2014-05-01 00:00:00.000  2014-06-01 00:00:00.000  2
2014-06-01 00:00:00.000  2014-07-01 00:00:00.000  2
2014-07-01 00:00:00.000  2014-08-01 00:00:00.000  2
2014-08-01 00:00:00.000  2014-09-01 00:00:00.000  2
2014-08-01 00:00:00.000  2014-09-01 00:00:00.000  2
2014-08-01 00:00:00.000  2014-09-01 00:00:00.000  2
2014-09-01 00:00:00.000  2014-10-01 00:00:00.000  2
2014-10-01 00:00:00.000  2014-11-01 00:00:00.000  2
2014-11-01 00:00:00.000  2014-12-01 00:00:00.000  1
2014-12-01 00:00:00.000  2015-01-01 00:00:00.000  1
2014-12-01 00:00:00.000  2015-01-01 00:00:00.000  1
2015-01-01 00:00:00.000  2015-02-01 00:00:00.000  1
2015-02-01 00:00:00.000  2015-03-01 00:00:00.000  1
2015-03-01 00:00:00.000  2015-04-01 00:00:00.000  1
2015-04-01 00:00:00.000  2015-05-01 00:00:00.000  1
2015-05-01 00:00:00.000  2015-06-01 00:00:00.000  1
2015-06-01 00:00:00.000  NULL                     1

如果您对解决方案有任何了解,请帮助我。

即使你有想在c#.net上做这个也没问题,你可以回复请帮助我。

2 个答案:

答案 0 :(得分:0)

有几种方法可以实现您想要的效果。一种方法是行数的差异。事实证明,当值相同时,以下表达式是常量:

select (row_number() over (partition by ref order by [TransactionDate]) -
        row_number() over (partition by ref, code order by [TransactionDate])
       ) as grp
from LevyTransactions t;

你可以尝试一下,亲眼看看。

其余的只是聚合:

select ref, code, min(TransactionDate), max(TransactionDate)
from (select (row_number() over (partition by ref order by TransactionDate) -
              row_number() over (partition by ref, code order by [TransactionDate])
             ) as grp
      from LevyTransactions t
     ) t
group by ref, code, grp;

答案 1 :(得分:0)

这只是戈登的答案,修复了列名。它应该输出你想要的结果。

WITH cte AS(
    select *, (row_number() over (partition by ref order by [TransactionDate]) -
            row_number() over (partition by ref, code order by [TransactionDate])
           ) as group_no
    from LevyTransactions t
)
SELECT
  Ref, MIN(Code) AS Code, MIN(TransactionDate) AS StartDate, MAX(TransactionDate) AS EndDate
FROM cte
GROUP BY Ref, group_no
ORDER BY 1,3

SQL Fiddle Demo

修改:要获得您提到的显示格式,请使用以下查询

WITH cte AS(
    select *, (row_number() over (partition by ref order by [TransactionDate]) -
            row_number() over (partition by ref, code order by [TransactionDate])
           ) as group_no
    from LevyTransactions t
)
SELECT
  Ref
  ,MIN(Code) AS Code
  ,STUFF(CONVERT(VARCHAR,MIN(TransactionDate),107), 4,4, ' ') AS [From]
  ,STUFF(CONVERT(VARCHAR,MAX(TransactionDate),107), 4,4, ' ') AS [To]
FROM cte
GROUP BY Ref, group_no
ORDER BY Ref,MIN(TransactionDate)