T-sql有助于在SSRS报告上切换日期

时间:2015-07-24 15:00:37

标签: sql sql-server tsql reporting-services ssrs-2008

我根据业务规则要求更改报表上的预定交货日期。以下是DDL:

1)如果作业有批号,则预定发货日期将是下一个星期一到请求的交货日期。例如,如果作业的请求交货日期为2015-07-29,并且它还有批号,那么预定发货日期将为2015-08-03。

2)如果作业没有批号,则预定发货日期将是请求交货日期之前的星期一。例如,如果作业J012347的请求交货日期为2015-08-04并且没有批号,那么预定发货日期将是2015-08-04之前的星期一。

同样,如果请求的交付日期是2015-08-07且没有批号,则预定交付日期为2015-08-03。

我们可以做到这一点吗?我很感激任何帮助。

希望这有帮助

这是DDL:

DECLARE @Date datetime;
SET @Date = GETDATE();

DECLARE @TEST_DATA TABLE
(
    DT_ID INT       IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED
   ,JobNumber       VARCHAR(10)   NOT NULL
   ,JobStatus       CHAR(1)       NOT NULL
   ,ExpectedDate    VARCHAR(10)   NOT NULL
   ,LastShippedDate VARCHAR(10)       NULL
   ,BatchNumber VARCHAR(10)         NULL
);

INSERT INTO @TEST_DATA (JobStatus, JobNumber,ExpectedDate,LastShippedDate, BatchNumber) 
VALUES
 ('S', 'J012345','2015-07-10','2015-07-10', 'B001')
,('S', 'J012346','2015-07-15','2015-07-17', 'B001')
,('S', 'J012346','2015-07-17','2015-07-15', 'B002')
,('S', 'J012346','2015-07-08','2015-07-10', 'B002')
,('O', 'J012347','2015-08-04',NULL, NULL)
,('O', 'J012347','2015-07-24',NULL, 'B003')
,('O', 'J012347','2015-07-31',NULL, 'B003')
,('O', 'J012347','2015-07-27',NULL, 'B004')
,('O', 'J012348','2015-07-21',NULL, 'B004')
,('O', 'J012349','2015-07-31',NULL,  NULL)
,('O', 'J012350','2015-08-07',NULL,  NULL)
,('O', 'J012351','2015-08-14',NULL,  NULL)
,('O', 'J012362','2015-08-28',NULL, 'B004')
,('O', 'J012363','2015-07-22',NULL, 'B005')
,('O', 'J012364','2015-07-24',NULL, NULL)
,('O', 'J012365','2015-07-31',NULL, NULL)
,('O', 'J012366','2015-08-21',NULL, 'B006')
,('O', 'J012372','2015-07-27',NULL, 'B007')
,('O', 'J012378','2015-07-29',NULL, 'B008')
,('O', 'J012367','2015-08-11',NULL, NULL)
,('O', 'J012367','2015-07-30',NULL, NULL)
,('O', 'J012367','2015-09-18',NULL, 'B006')
;

SELECT 
    J.DT_ID
    ,JobNumber
   ,J.JobStatus /*O-Open, S-Shipped, I-Invoiced*/
   ,J.ExpectedDate 'Requested Delivery Date'
   ,J.LastShippedDate
   , BatchNumber
   ,CASE
        /* SHIPPED - SHOW LastShippedDate */
        WHEN j.JobStatus  ='S' OR j.JobStatus  ='I' OR j.JobStatus  ='V'  THEN Cast(j.LastShippedDate as DATE)
        /* MISSED SHIPMENT SET TO NEXT MONDAY */
        WHEN j.JobStatus <>'S'  AND DATEDIFF(dd,0,ExpectedDate)/7 <= DATEDIFF(dd,0,@Date) / 7 THEN
            DATEADD(DAY,((DATEDIFF(dd,0,@Date)/7) * 7) + 7,0)
        /* FUTURE SHIPMENTS */
        --ELSE CAST(J.ExpectedDate AS DATE)
        ELSE DATEADD(DAY,((DATEDIFF(dd,0,J.ExpectedDate)/7) * 7) + 7,0)
    END AS [Scheduled Ship Date],

    DATENAME(dw, J.ExpectedDate) AS ExpectedDateDayName,
    DATENAME(dw, CASE
            WHEN j.JobStatus  ='S' OR j.JobStatus  ='I' OR j.JobStatus  ='V'  THEN Cast(j.LastShippedDate as DATE)
            WHEN j.JobStatus <>'S' AND DATEDIFF(dd,0,ExpectedDate)/7 <= DATEDIFF(dd,0,@Date) / 7 THEN
                DATEADD(DAY,((DATEDIFF(dd,0,@Date)/7) * 7) + 7,0)
            --ELSE CAST(J.ExpectedDate AS DATE)
            ELSE DATEADD(DAY,((DATEDIFF(dd,0,J.ExpectedDate)/7) * 7) + 7,0)
        END) AS ScheduledShipDateDayName

FROM @TEST_DATA AS J
ORDER BY [Requested Delivery Date]

1 个答案:

答案 0 :(得分:1)

所以你试图找到&#34;下周一&#34;如果没有批次,并且请求日期前一个星期一有一个:

Select Case when BatchNumber is not null then  
                case when datepart(dw,J.ExpectedDate) > 1 then DATEADD(dd,9-DATEPART(dw,J.ExpectedDate),J.ExpectedDate)
                    else DATEADD(dd,1,J.ExpectedDate) end 
        Else dateadd(ww,-2, case when datepart(dw,J.ExpectedDate) > 1 then DATEADD(dd,9-DATEPART(dw,J.ExpectedDate),J.ExpectedDate)
                else DATEADD(dd,1,J.ExpectedDate) end) end AS [Scheduled Ship Date]


FROM @TEST_DATA AS J