将每日数据转换为SQL Server 2008中的每周格式

时间:2014-02-11 21:26:36

标签: sql sql-server-2008 date

Conversiondate         Activity
2013-09-30 00:00:00    Renaissance_Homepage_022013
2013-09-12 00:00:00    RFP - Total
2013-09-23 00:00:00    Reservations Rate List Menu
2013-09-08 00:00:00    SHS 2.0 Home
2013-09-17 00:00:00    MR Promo Registration Confirmation
2013-09-13 00:00:00    Renaissance_ReservationOverlay_022013
2013-09-02 00:00:00    Reservations Rate List Menu
2013-09-13 00:00:00    Renaissance_HotelPage_022013
2013-09-08 00:00:00    Reservations Rate List Menu

我上面的表格包括日期和活动。现在我想编写脚本,我可以在每周找到新的活动。例如:我本周有10个活动,下周还会有另外2个活动。所以,那些2将是新的,并且想要编写脚本以在每周基础上以下面的格式找到新活动;

本周将从星期日开始。

StartDateWEEk     ENDDATEWEEK             Activity          Week
----------------------------------------------------------------    
2013-09-01         2013-09-07         Booking Confo          36
2013-09-01         2013-09-07         Mobile                 36
2013-09-08         2013-09-14         Mobileconfo            37
2013-09-08         2013-09-14         Mobile                 37
2013-09-08         2013-09-14         Reservation            37

我想创建这种格式,您可以看到第37周有一项新活动来电预订。我想设置自动化流程来每周捕获这些新活动。

1 个答案:

答案 0 :(得分:1)

我只想猜你是如何得到你想要的OUPUT中的活动专栏那么这就是我要做的事情....

测试数据

DECLARE @TABLE TABLE( Conversiondate DATETIME,Activity VARCHAR(400))
INSERT INTO @TABLE VALUES
('2013-09-30 00:00:00','Renaissance_Homepage_022013'),
('2013-09-12 00:00:00','RFP - Total'),
('2013-09-23 00:00:00','Reservations Rate List Menu'),
('2013-09-08 00:00:00','SHS 2.0 Home'),
('2013-09-17 00:00:00','MR Promo Registration Confirmation'),
('2013-09-13 00:00:00','Renaissance_ReservationOverlay_022013'),
('2013-09-02 00:00:00','Reservations Rate List Menu'),
('2013-09-13 00:00:00','Renaissance_HotelPage_022013'),
('2013-09-08 00:00:00','Reservations Rate List Menu')

<强>查询

SELECT   CAST(DATEADD(dd, -(DATEPART(dw, Conversiondate)-1)
                         , Conversiondate) AS DATE) [WeekStart]
        ,CAST(DATEADD(dd, 7-(DATEPART(dw, Conversiondate))
                          , Conversiondate) AS DATE) [WeekEnd]
        ,  Activity
        ,DATEPART(WEEK, Conversiondate) [Week_Number]
FROM @TABLE

结果集

╔════════════╦════════════╦═══════════════════════════════════════╦═════════════╗
║ WeekStart  ║  WeekEnd   ║               Activity                ║ Week_Number ║
╠════════════╬════════════╬═══════════════════════════════════════╬═════════════╣
║ 2013-09-29 ║ 2013-10-05 ║ Renaissance_Homepage_022013           ║          40 ║
║ 2013-09-08 ║ 2013-09-14 ║ RFP - Total                           ║          37 ║
║ 2013-09-22 ║ 2013-09-28 ║ Reservations Rate List Menu           ║          39 ║
║ 2013-09-08 ║ 2013-09-14 ║ SHS 2.0 Home                          ║          37 ║
║ 2013-09-15 ║ 2013-09-21 ║ MR Promo Registration Confirmation    ║          38 ║
║ 2013-09-08 ║ 2013-09-14 ║ Renaissance_ReservationOverlay_022013 ║          37 ║
║ 2013-09-01 ║ 2013-09-07 ║ Reservations Rate List Menu           ║          36 ║
║ 2013-09-08 ║ 2013-09-14 ║ Renaissance_HotelPage_022013          ║          37 ║
║ 2013-09-08 ║ 2013-09-14 ║ Reservations Rate List Menu           ║          37 ║
╚════════════╩════════════╩═══════════════════════════════════════╩═════════════╝