2016-07-25 41 views
0

我想在ssis包中按月計算每日日期的小時數。我寫了這以下的SQL代碼:在Tableau的SSIS的sql表中創建求和

下面的查詢每月

select year(forecastdate) as y, month(forecastdate) as m,sum(ForecastHrsEnd) as p 
from rpt_histsnapeng 
group by year(forecastdate), month(forecastdate) 

計算ForecastHoursEnd之和羣體的日期和, 下面的查詢計算實際小時數和羣體的日期總和按月

select year(ActualsDate) as y, month(ActualsDate) as m, sum(ActualHrs) as p 
from rpt_histsnapeng 
group by year(ActualsDate), month(ActualsDate) 

現在我試圖寫在SSIS代碼將它們傳遞給一個變量/列,以便我可以使用該列中的Tableau創建圖表,我該怎麼做?

我引用這個目的以下鏈接:

SQL needed: sum over values by monthSQl query to sum-up amounts by month/year

+0

你希望得到的輸出是什麼? – Nicarus

+0

這樣的事情:rpt_HistsnapEng具有預測日期,預測時間,實際日期和實際日期的日期。我想總結實際小時數和整個月的預測小時數 –

+0

您是否試圖將兩個查詢的結果合併爲一個您可以在Tableau中使用的結果? – Nicarus

回答

0

首先,沒有任何道理可言,你不能只使用的Tableau在今年進行的聚合和月份穀物 - 無需預先彙總數據。

但是,如果你堅持要首先準備在SQL中的數據,你有兩個選擇:

1 UNION

這是一個更加標準化的結構,這將允許您使用t(「類型」)作爲衡量來分離/描繪您的數據。

SELECT 
    YEAR(ForecastDate) AS y, 
    MONTH(ForcastDate) AS m, 
    'Forecast' AS t, 
    SUM(ForecastHrsEnd) AS p 
FROM 
    rpt_histsnapeng 
GROUP BY 
    YEAR(ForecastDate), 
    MONTH(ForcastDate) 

UNION ALL 

SELECT 
    YEAR(ActualsDate) AS y, 
    MONTH(ActualsDate) AS m, 
    'Actual' AS t, 
    SUM(ActualHrs) AS p 
FROM 
    rpt_histsnapeng 
GROUP BY 
    YEAR(ActualsDate), 
    MONTH(ActualsDate) 

2. BASIC

這非規範化和接近現有的表結構。 Tableau可以很好地處理這種設計,只需使用度量(小時)作爲雙軸。

SELECT 
    YEAR(ForecastDate) AS Forecast_Year, 
    MONTH(ForcastDate) AS Forecast_Month, 
    YEAR(ActualsDate) AS Actual_Year, 
    MONTH(ActualsDate) AS Actual_Month, 
    SUM(ForecastHrsEnd) AS Forecast_Hours, 
    SUM(ActualHrs) AS Actual_Hours 
FROM 
    rpt_histsnapeng 
GROUP BY 
    YEAR(ForecastDate), 
    MONTH(ForcastDate), 
    YEAR(ActualsDate), 
    MONTH(ActualsDate)