如果另一个事件已经存在,则将同一天 start/end 日期计算为 0 天

Calculate same day start/end dates as 0 days if another occurrence already exists

我有一个查询,在某些情况下 "phase" 在同一天开始和结束 - 这按 1 天计算。但是,如果另一个 "phase" 在同一天针对同一裁判开始和结束。不。和期间号,那么我想将其计算为 0 天。

示例:

**Ref.  Period.  Phase  StDt        EndDt**
013     3        KAA    01/01/16    01/01/16  - This is one day
013     3        TAA    02/01/16    03/01/16  - this is 2 days    
013     3        KAT    01/01/16    01/01/16  - **would like this to be counted as 0 day**
013     3        TTA    04/04/16    04/04/16  - this is one day

我希望在按 Ref. 分组的数据中进行这种独特的计算。和期号。这是一个棘手的....

谢谢

这个表达式应该适用于 SSRS 端:

=IIF(Fields!StartDate.Value=Fields!EndDate.Value AND Fields!Phase.Value <> LOOKUPSET(Fields!StartDate.Value &"_" & Fields!EndDate.Value,Fields!StartDate.Value & "_" & Fields!EndDate.Value,Fields!Phase.Value,"DatasetName").GetValue(0),0,DATEDIFF("D",Fields!StartDate.Value,Fields!EndDate.Value)+1)

它将return第一阶段的值 1 return由数据集编辑。如果阶段-日期范围组合在分组中不是唯一的,这将不会像写的那样工作,但您应该能够相应地进行修改。

此外,如果行在 SSRS 和数据集之间的排序不同,则它可能不是出现的第一行获得 1。

试试这个。 我假设您正在使用 TSQl(不确定您是否还标记了 SQL.

;WITH cte_result(ID,Ref, Period,Phase,StDt,EndDt) AS
(
SELECT 1,'013' ,3,'KAA',CAST('01/01/16'AS DATETIME),CAST('01/01/16'AS DATETIME) UNION ALL
SELECT 2,'013' ,3,'TAA','01/02/16','01/03/16' UNION ALL 
SELECT 3,'013' ,3,'KAT','01/01/16','01/01/16' UNION ALL
SELECT 4,'013' ,3,'TTA','04/04/16','04/04/16')
,cte_PreResult AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY CAST(StDt AS DATE), CAST(EndDt AS DATE) ORDER BY ID) AS [Order],
    Ref,
    Period,
    Phase,
    StDt,
    EndDt
FROM cte_result
)
SELECT Ref,
    Period,
    Phase,
    StDt,
    EndDt,
    CASE 
        WHEN [Order] <> 1
            THEN '0 Day(s)'
        ELSE CAST(DATEDIFF(dd, StDt, EndDt) + 1 AS VARCHAR(10)) + ' Day(s)'
        END AS Comment
FROM cte_PreResult

如果没有 ID 列,则 select 要排序的某些列,可能是阶段,所以将 ID 替换为阶段,如此处 ROW_NUMBER() OVER (PARTITION BY StDt, EndDt ORDER BY ID) AS [Order],如果没有要排序的候选列,那么试试这个

;WITH cte_result(ID,Ref, Period,Phase,StDt,EndDt) AS
(
SELECT 1,'013' ,3,'KAA',CAST('01/01/16'AS DATETIME),CAST('01/01/16'AS DATETIME) UNION ALL
SELECT 2,'013' ,3,'TAA','01/02/16','01/03/16' UNION ALL 
SELECT 3,'013' ,3,'KAT','01/01/16','01/01/16' UNION ALL
SELECT 4,'013' ,3,'TTA','04/04/16','04/04/16')
,cte_PreResult AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY CAST(StDt AS DATE), CAST(EndDt AS DATE) ORDER BY (SELECT NULL)) AS [Order],
    Ref,
    Period,
    Phase,
    StDt,
    EndDt
FROM cte_result
)
SELECT Ref,
    Period,
    Phase,
    StDt,
    EndDt,
    CASE 
        WHEN [Order] <> 1
            THEN '0 Day(s)'
        ELSE CAST(DATEDIFF(dd, StDt, EndDt) + 1 AS VARCHAR(10)) + ' Day(s)'
        END AS Comment
FROM cte_PreResult

以下成功了!基本上,我使用 count aggregate 来计算 PER Ref 和 period 阶段在同一天开始和结束的实例数。然后,对于任何超过 1 的地方,我只使用简单的 case 语句将第一个计为 1,将任何后续的计为 0。我将以下创建为连接中的子查询作为左外部连接:

LEFT OUTER JOIN
                      (SELECT     TOP (100) PERCENT Period, Ref, 
CONVERT(date, PhaseStartDate) AS stdt, CONVERT(date, PhaseEndDate) AS enddt, 
COUNT(*) 
                                               AS NoOfSameDayPhases, 
MIN(PhaseSequence) AS FirstPhSeq
                        FROM          Phases AS Phases_1
                        WHERE      (CONVERT(date, PhaseStartDate) = 
CONVERT(date, PhaseEndDate))
                        GROUP BY VoidPeriod, Ref, CONVERT(date, 
PhaseStartDate), CONVERT(date, PhaseEndDate)) AS SameDayPH ON CONVERT(date, 
                  PhaseEndDate) = SameDayPH.enddt AND CONVERT(date, 
PhaseStartDate) = SameDayPH.stdt AND 
                  VoidPeriod = SameDayPH.VoidPeriod AND SameDayPH.Ref = 
VoidPhases.Ref