数据组由2个不同的列日期和时间跟随另一个

时间:2013-05-31 09:19:43

标签: sql sql-server

以下是输出数据,日期和时间的查询


SELECT a.CurrDate,a.item,
    WeekStartingSunday  = dateadd(dd,(datediff(dd,-53684,a.CurrDate)/7)*7,-53684),
    WeekEndingSaturday  = dateadd(dd,((datediff(dd,-53684,a.CurrDate)/7)*7)+6,-53684)
FROM
    ( 
        SELECT item = 'abc', CurrDate = getdate()
            UNION ALL
        SELECT item = 'def', CurrDate = getdate() +1    
            UNION ALL
        SELECT item = 'abc', CurrDate = getdate() +2    
            UNION ALL
        SELECT item = 'abc', CurrDate = getdate() +3    
            UNION ALL
        SELECT item = '987', CurrDate = getdate() +4    
            UNION ALL
        SELECT item = 'abc', CurrDate = getdate() +5    
            UNION ALL
        SELECT item = 'abc', CurrDate = getdate() +6    
            UNION ALL
        SELECT item = 'abc', CurrDate = getdate() +7
    ) a 
ORDER BY a.CurrDate

结果:

 CurrDate                  item  WeekStartingSunday      WeekEndingSaturday
 ------------------------------------------------------------------------------
 2013-05-31 14:14:26.613    abc   2013-05-26               2013-06-01 
 2013-06-01 14:14:26.613    def   2013-05-26               2013-06-01 
 2013-06-02 14:14:26.613    abc   2013-06-02               2013-06-08 
 2013-06-03 14:14:26.613    abc   2013-06-02               2013-06-08 
 2013-06-04 14:14:26.613    987   2013-06-02               2013-06-08 
 2013-06-05 14:14:26.613    abc   2013-06-02               2013-06-08 
 2013-06-06 14:14:26.613    abc   2013-06-02               2013-06-08 
 2013-06-07 14:14:26.613    abc   2013-06-02               2013-06-08 

下表是所需的结果,我想将结果输出到如下所示的表中:

2013-05-26 to 2013-06-01
---------------------------
2013-05-31      abc
2013-06-01      def 

2013-06-02 to 2013-06-08 
---------------------------
2013-06-02          abc
2013-06-03          abc
2013-06-04          987
2013-06-05          abc
2013-06-06          abc
2013-06-07          abc

我很困惑,怎么能实现呢?这适用于SQL SERVER 2005。

1 个答案:

答案 0 :(得分:0)

试试这个 -

<强>查询:

DECLARE @date DATETIME
SELECT @date = CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME)

DECLARE @SQL NVARCHAR(MAX)

;WITH cte AS 
(
    SELECT 
          a.CurrDate
        , a.item
        , WeekStartingSunday = DATEADD(DAY, 1 - DATEPART(WEEKDAY, CurrDate), CurrDate)
        , WeekEndingSaturday = DATEADD(DAY, 7 - DATEPART(WEEKDAY, CurrDate), CurrDate)
    FROM ( 
        SELECT item = 'abc', CurrDate = @date
            UNION ALL
        SELECT 'def', @date +1    
            UNION ALL
        SELECT 'abc', @date +2    
            UNION ALL
        SELECT 'abc', @date +3    
            UNION ALL
        SELECT '987', @date +4    
            UNION ALL
        SELECT 'abc', @date +5    
            UNION ALL
        SELECT 'abc', @date +6    
            UNION ALL
        SELECT 'abc', @date +7
    ) a 
)
SELECT @SQL = (
    SELECT CHAR(13) +
          STUFF((
              SELECT ' UNION ALL SELECT ''' + CONVERT(VARCHAR(10), CurrDate, 111) + '   ' + item + ''''
              FROM cte c2
              WHERE c2.CurrDate BETWEEN c.WeekStartingSunday AND c.WeekEndingSaturday
              ORDER BY CurrDate
              FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 18, 
                'SELECT [' 
                    + CONVERT(VARCHAR(10), WeekStartingSunday, 111) 
                    + ' to ' 
                    + CONVERT(VARCHAR(10), WeekEndingSaturday, 111) + '] = ')
    FROM (
        SELECT DISTINCT c.WeekStartingSunday, c.WeekEndingSaturday
        FROM cte c
    )c
    ORDER BY WeekStartingSunday
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)')

EXEC sys.sp_executesql @SQL

<强>结果:

2013/06/02 to 2013/06/08
------------------------
2013/06/03   abc
2013/06/04   def
2013/06/05   abc
2013/06/06   abc
2013/06/07   987
2013/06/08   abc

2013/06/09 to 2013/06/15
------------------------
2013/06/09   abc
2013/06/10   abc