2017-04-06 103 views
0

我想在T-SQL公用表表達式中使用開始和結束日期參數。我對SQL Server開發非常陌生,我不確定在查詢中缺少什麼。在SQL Server CTE中使用日期參數

我可以指定值爲@startdate & @enddate並得到正確的結果。

但是,我想弄清楚如何打開這兩個參數,以便用戶可以指定開始日期和結束日期值。該查詢將用於SSRS報告中。

DECLARE @startdate Datetime, 
     @enddate Datetime; 

SET @startdate = '2017-02-09'; 
SET @enddate = '2017-02-10'; 

WITH ManHours AS 
( 
    SELECT DISTINCT 
     a.plant_name AS Plant, SUM(tc.total_hr) AS TotalHours 
    FROM 
     area AS a 
    INNER JOIN 
     tf_department AS dep ON a.plant_id = dep.plant_id 
    INNER JOIN 
     tf_timecard AS tc ON dep.department_id = tc.department_id 
    WHERE 
     tc.timecard_dt BETWEEN @startdate AND @enddate 
     AND tc.department_id IN (266, 453, ...endlessly long list of IDs......) 
     AND tc.hourtype_id = 1 
    GROUP BY 
     a.plant_name), 
Tonnage AS 
( 
    SELECT DISTINCT 
     a.plant_name AS Plant, SUM(tglt.postqty) AS TotalTonnage 
    FROM 
     area AS a 
    INNER JOIN 
     plantgl AS pgl ON a.plant_id = pgl.plant_id 
    INNER JOIN 
     tgltransaction AS tglt ON pgl.glacckey = tglt.glacctkey 
    WHERE 
     tglt.postdate BETWEEN @startdate AND @enddate 
    GROUP BY 
     a.plant_name 
) 
SELECT DISTINCT 
    ManHours.Plant, 
    SUM(TotalTonnage) as 'Production Tons' , 
    SUM(TotalHours) as 'Man Hours', 
    TotalTonnage/TotalHours AS TonsPerManHour 
FROM 
    ManHours 
LEFT OUTER JOIN 
    Tonnage ON ManHours.Plant = tonnage.Plant 
GROUP BY 
    ManHours.Plant, ManHours.TotalHours, Tonnage.TotalTonnage 
+2

將此查詢包裝在存儲過程中 – TheGameiswar

+0

您可以刪除DECLARE和SET語句。當您將其餘查詢放入SSRS中的數據集時,它將自動創建兩個參數。您只需將參數更改爲DATE類型,並且會在運行查詢之前提示輸入日期。 –

回答

0

下面是您可以使用的存儲過程的示例。另外,我提供了CTE中指定的「無盡長的ID列表」的兩個替代方案。在我看來,最好將這個邏輯拉出查詢並將其放在存儲過程的開始處。這將使您或其他人能夠輕鬆地返回並修改此列表如果/當其更改爲時。更好的是,我提供了一個TABLE VARIABLE(@ListOfDeptIdsFromTable),你可以用它來實際地檢索10這個數據,而不是硬編碼一個字符串。

CREATE PROCEDURE Report 
    @startdate DATETIME, 
    @enddate DATETIME 
AS 
BEGIN 

    SET NOCOUNT ON; 

    DECLARE @ListOfDeptIds VARCHAR(MAX) = '266, 453, ...endlessly long list of IDs......'; 

    DECLARE @ListOfDeptIdsFromTable TABLE 
    (
     department_id INT 
    ) 

    INSERT INTO @ListOfDeptIdsFromTable (department_id) 
    SELECT department_id 
    FROM -- Table here 
    WHERE -- Where credentials to retrieve the long list 

    WITH ManHours AS 
    ( 
     SELECT DISTINCT 
      a.plant_name AS Plant, SUM(tc.total_hr) AS TotalHours 
     FROM 
      area AS a 
     INNER JOIN 
      tf_department AS dep ON a.plant_id = dep.plant_id 
     INNER JOIN 
      tf_timecard AS tc ON dep.department_id = tc.department_id 
     WHERE 
      tc.timecard_dt BETWEEN @startdate AND @enddate 
      AND tc.department_id IN (@ListOfDeptIds) -- or ... IN (SELECT department_id FROM @ListOfDeptIdsFromTable) 
      AND tc.hourtype_id = 1 
     GROUP BY 
      a.plant_name), 
    Tonnage AS 
    ( 
     SELECT DISTINCT 
      a.plant_name AS Plant, SUM(tglt.postqty) AS TotalTonnage 
     FROM 
      area AS a 
     INNER JOIN 
      plantgl AS pgl ON a.plant_id = pgl.plant_id 
     INNER JOIN 
      tgltransaction AS tglt ON pgl.glacckey = tglt.glacctkey 
     WHERE 
      tglt.postdate BETWEEN @startdate AND @enddate 
     GROUP BY 
      a.plant_name 
    ) 
    SELECT DISTINCT 
     ManHours.Plant, 
     SUM(TotalTonnage) as 'Production Tons' , 
     SUM(TotalHours) as 'Man Hours', 
     TotalTonnage/TotalHours AS TonsPerManHour 
    FROM 
     ManHours 
    LEFT OUTER JOIN 
     Tonnage ON ManHours.Plant = tonnage.Plant 
    GROUP BY 
     ManHours.Plant, ManHours.TotalHours, Tonnage.TotalTonnage 

END 
GO