在查询SSRS中对日期值进行硬编码

问题描述 投票:0回答:1

我有一个查询,我试图对日期值进行硬编码。当前,此数据来自SSRS日历。我认为我的数据与预期相差无几。要调试所有东西,我需要对日期部分进行硬编码。

我该如何照顾这些事情?我是SQL和SSRS的新手。

WITH Calendar (Start_Date, End_Date) AS(
Select TOP 6
DATEADD(mm, DATEDIFF(m,0,DATEADD(MONTH, DATEDIFF(MONTH, 0, @StartDate) -6 + Row_Number() Over (Order By CI_ID), 0)),0)[Start_Date],
DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,DATEADD(MONTH, DATEDIFF(MONTH, 0, @StartDate) -6 + Row_Number() Over (Order By CI_ID), 0))+1,0)) [End_Date] 
From v_UpdateInfo
Group by CI_ID
)
Select Start_Date, End_Date,
(Select count(distinct ucs.ResourceID)
From v_Update_ComplianceStatus ucs
JOIN v_UpdateInfo ui on ui.CI_ID = ucs.CI_ID and ucs.Status = 2 AND ui.IsSuperseded = 0
JOIN v_CICategoryInfo_All cica JOIN v_CategoryInfo ci on cica.CategoryInstanceID = ci.CategoryInstanceID AND cica.CategoryInstanceName IN (@Category) on ucs.CI_ID = cica.CI_ID
Where ucs.ResourceID IN (Select rs.ResourceID from v_r_system  rs   JOIN V_FullCollectionMembership AS FCM on rs.ResourceID = FCM.ResourceID and FCM.CollectionID = @CollectionID  JOIN v_UpdateScanStatus uss on uss.ResourceID = rs.ResourceID where rs.Operating_System_Name_and0 like '%Workstation 6.1%' And DATEDIFF("d",uss.LastScanTime,getdate()) <= @DaysScanned AND rs.Obsolete0 = 0 )
AND ui.DateRevised BETWEEN cal.Start_Date AND cal.End_Date
) [Windows 7],
(Select count(rs.ResourceID) from v_r_system rs JOIN V_FullCollectionMembership AS FCM on rs.ResourceID = FCM.ResourceID and FCM.CollectionID = @CollectionID  JOIN v_UpdateScanStatus uss on uss.ResourceID = rs.ResourceID where rs.Operating_System_Name_and0 like '%Workstation 6.3%' And DATEDIFF("d",uss.LastScanTime,getdate()) <= @DaysScanned AND rs.Obsolete0 = 0) [Windows 8.1 Total],
(Select count(distinct ucs.ResourceID)
From v_Update_ComplianceStatus ucs
JOIN v_UpdateInfo ui on ui.CI_ID = ucs.CI_ID and ucs.Status = 2 AND ui.IsSuperseded = 0
JOIN v_CICategoryInfo_All cica JOIN v_CategoryInfo ci on cica.CategoryInstanceID = ci.CategoryInstanceID AND cica.CategoryInstanceName IN (@Category) on ucs.CI_ID = cica.CI_ID
Where ucs.ResourceID IN (Select rs.ResourceID from v_r_system rs    JOIN V_FullCollectionMembership AS FCM on rs.ResourceID = FCM.ResourceID and FCM.CollectionID = @CollectionID  JOIN v_UpdateScanStatus uss on uss.ResourceID = rs.ResourceID where rs.Operating_System_Name_and0 like '%Workstation 10%' And DATEDIFF("d",uss.LastScanTime,getdate()) <= @DaysScanned AND rs.Obsolete0 = 0 )
AND ui.DateRevised BETWEEN cal.Start_Date AND cal.End_Date
) [Windows 10],
(Select count(rs.ResourceID) from v_r_system rs JOIN V_FullCollectionMembership AS FCM on rs.ResourceID = FCM.ResourceID and FCM.CollectionID = @CollectionID  JOIN v_UpdateScanStatus uss on uss.ResourceID = rs.ResourceID where rs.Operating_System_Name_and0 like '%Workstation 10%' And DATEDIFF("d",uss.LastScanTime,getdate()) <= @DaysScanned AND rs.Obsolete0 = 0) [Windows 10 Total]
from Calendar cal

到目前为止,我尝试了这个。

Declare start_date = '2019-06-01';
declare end_date = '2019-11-30';
    --WITH Calendar (Start_Date, End_Date) AS(
    Select TOP 6
    DATEADD(mm, DATEDIFF(m,0,DATEADD(MONTH, DATEDIFF(MONTH, 0, @StartDate) -6 + Row_Number() Over (Order By CI_ID), 0)),0)[Start_Date],
    DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,DATEADD(MONTH, DATEDIFF(MONTH, 0, @StartDate) -6 + Row_Number() Over (Order By CI_ID), 0))+1,0)) [End_Date] 
    From v_UpdateInfo
    Group by CI_ID
    )
    Select Start_Date, End_Date,
    (Select count(distinct ucs.ResourceID)
    From v_Update_ComplianceStatus ucs
    JOIN v_UpdateInfo ui on ui.CI_ID = ucs.CI_ID and ucs.Status = 2 AND ui.IsSuperseded = 0
    JOIN v_CICategoryInfo_All cica JOIN v_CategoryInfo ci on cica.CategoryInstanceID = ci.CategoryInstanceID --AND 
    --cica.CategoryInstanceName -- IN (@Category) 
    on ucs.CI_ID = cica.CI_ID
    Where ucs.ResourceID IN (Select rs.ResourceID from v_r_system  rs   JOIN V_FullCollectionMembership AS FCM on rs.ResourceID = FCM.ResourceID
     and FCM.CollectionID = 'ABC0028'  JOIN v_UpdateScanStatus uss on uss.ResourceID = rs.ResourceID 
     where rs.Operating_System_Name_and0 like '%Workstation 6.1%' And DATEDIFF("d",uss.LastScanTime,getdate()) <= @DaysScanned AND rs.Obsolete0 = 0 )
    AND ui.DateRevised BETWEEN cal.Start_Date AND cal.End_Date
    ) [Windows 7],
    (Select count(rs.ResourceID) from v_r_system rs JOIN V_FullCollectionMembership AS FCM on rs.ResourceID = FCM.ResourceID 
    and FCM.CollectionID = 'ABC0028'  JOIN v_UpdateScanStatus uss on uss.ResourceID = rs.ResourceID 
    where rs.Operating_System_Name_and0 like '%Workstation 6.3%' 
    And DATEDIFF("d",uss.LastScanTime,getdate()) <= @DaysScanned AND rs.Obsolete0 = 0) [Windows 8.1 Total],
    (Select count(distinct ucs.ResourceID)
    From v_Update_ComplianceStatus ucs
    JOIN v_UpdateInfo ui on ui.CI_ID = ucs.CI_ID and ucs.Status = 2 AND ui.IsSuperseded = 0
    JOIN v_CICategoryInfo_All cica JOIN v_CategoryInfo ci on cica.CategoryInstanceID = ci.CategoryInstanceID 
    --AND cica.CategoryInstanceName IN (@Category)
     on ucs.CI_ID = cica.CI_ID
    Where ucs.ResourceID IN (Select rs.ResourceID 
    from v_r_system rs  JOIN V_FullCollectionMembership AS FCM on rs.ResourceID = FCM.ResourceID and FCM.CollectionID = 'ABC0028'  
    JOIN v_UpdateScanStatus uss on uss.ResourceID = rs.ResourceID 
    where rs.Operating_System_Name_and0 like '%Workstation 10%' And DATEDIFF("d",uss.LastScanTime,getdate()) <= @DaysScanned AND rs.Obsolete0 = 0 )
    AND ui.DateRevised BETWEEN cal.Start_Date AND cal.End_Date
    ) [Windows 10],
    (Select count(rs.ResourceID) from v_r_system rs JOIN V_FullCollectionMembership AS FCM on rs.ResourceID = FCM.ResourceID and
     FCM.CollectionID = 'ABC0028'  JOIN v_UpdateScanStatus uss on uss.ResourceID = rs.ResourceID where rs.Operating_System_Name_and0 
     like '%Workstation 10%' And DATEDIFF("d",uss.LastScanTime,getdate()) <= @DaysScanned AND rs.Obsolete0 = 0) [Windows 10 Total]
    --from Calendar cal
sql sql-server
1个回答
1
投票

您有2个日期参数控制您的查询。它们是Start_DateEnd_Date。您将需要更新查询中对这些引用的所有引用。在您发布的原始查询中,有2个参考。

为了使对查询的修改最少,我在此解决方案中所做的就是修改CTE以使用您的硬编码日期。

另外,请注意,此查询中的日期之外还有其他变量需要声明和处理。它们是@DaysScanned和@CollectionID。由于您没有在OP中提及它们,因此我认为它们对您来说不是问题。

WITH Calendar (Start_Date, End_Date)
AS (SELECT CAST('2019-06-01' AS DATETIME2) AS [Start_Date],
           CAST('2019-11-30' AS DATETIME2) AS [End_Date])
SELECT Start_Date,
       End_Date,
       (
           SELECT COUNT(DISTINCT ucs.ResourceID)
           FROM v_Update_ComplianceStatus ucs
               JOIN v_UpdateInfo ui
                   ON ui.CI_ID = ucs.CI_ID
                      AND ucs.Status = 2
                      AND ui.IsSuperseded = 0
               JOIN v_CICategoryInfo_All cica
                   JOIN v_CategoryInfo ci
                       ON cica.CategoryInstanceID = ci.CategoryInstanceID
                          AND cica.CategoryInstanceName IN ( @Category )
                   ON ucs.CI_ID = cica.CI_ID
           WHERE ucs.ResourceID IN
                 (
                     SELECT rs.ResourceID
                     FROM v_r_system rs
                         JOIN V_FullCollectionMembership AS FCM
                             ON rs.ResourceID = FCM.ResourceID
                                AND FCM.CollectionID = @CollectionID
                         JOIN v_UpdateScanStatus uss
                             ON uss.ResourceID = rs.ResourceID
                     WHERE rs.Operating_System_Name_and0 LIKE '%Workstation 6.1%'
                           AND DATEDIFF("d", uss.LastScanTime, GETDATE()) <= @DaysScanned
                           AND rs.Obsolete0 = 0
                 )
                 AND ui.DateRevised
                 BETWEEN cal.Start_Date AND cal.End_Date
       ) [Windows 7],
       (
           SELECT COUNT(rs.ResourceID)
           FROM v_r_system rs
               JOIN V_FullCollectionMembership AS FCM
                   ON rs.ResourceID = FCM.ResourceID
                      AND FCM.CollectionID = @CollectionID
               JOIN v_UpdateScanStatus uss
                   ON uss.ResourceID = rs.ResourceID
           WHERE rs.Operating_System_Name_and0 LIKE '%Workstation 6.3%'
                 AND DATEDIFF("d", uss.LastScanTime, GETDATE()) <= @DaysScanned
                 AND rs.Obsolete0 = 0
       ) [Windows 8.1 Total],
       (
           SELECT COUNT(DISTINCT ucs.ResourceID)
           FROM v_Update_ComplianceStatus ucs
               JOIN v_UpdateInfo ui
                   ON ui.CI_ID = ucs.CI_ID
                      AND ucs.Status = 2
                      AND ui.IsSuperseded = 0
               JOIN v_CICategoryInfo_All cica
                   JOIN v_CategoryInfo ci
                       ON cica.CategoryInstanceID = ci.CategoryInstanceID
                          AND cica.CategoryInstanceName IN ( @Category )
                   ON ucs.CI_ID = cica.CI_ID
           WHERE ucs.ResourceID IN
                 (
                     SELECT rs.ResourceID
                     FROM v_r_system rs
                         JOIN V_FullCollectionMembership AS FCM
                             ON rs.ResourceID = FCM.ResourceID
                                AND FCM.CollectionID = @CollectionID
                         JOIN v_UpdateScanStatus uss
                             ON uss.ResourceID = rs.ResourceID
                     WHERE rs.Operating_System_Name_and0 LIKE '%Workstation 10%'
                           AND DATEDIFF("d", uss.LastScanTime, GETDATE()) <= @DaysScanned
                           AND rs.Obsolete0 = 0
                 )
                 AND ui.DateRevised
                 BETWEEN cal.Start_Date AND cal.End_Date
       ) [Windows 10],
       (
           SELECT COUNT(rs.ResourceID)
           FROM v_r_system rs
               JOIN V_FullCollectionMembership AS FCM
                   ON rs.ResourceID = FCM.ResourceID
                      AND FCM.CollectionID = @CollectionID
               JOIN v_UpdateScanStatus uss
                   ON uss.ResourceID = rs.ResourceID
           WHERE rs.Operating_System_Name_and0 LIKE '%Workstation 10%'
                 AND DATEDIFF("d", uss.LastScanTime, GETDATE()) <= @DaysScanned
                 AND rs.Obsolete0 = 0
       ) [Windows 10 Total]
FROM Calendar cal;
热门问题
推荐问题
最新问题