简体   繁体   中英

creating summation in a sql table in SSIS for Tableau

I want to sum hours for daily dates by month in an ssis package. I wrote the following sql code for this:

The below query calculates the sum of ForecastHoursEnd and groups the date by month

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

and, The below query calculates the sum of Actual Hours and groups the date by month

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

Now I am trying to write the code in SSIS to pass them to a variable/column so that I can use that column in Tableau to create a chart, how would I do that?

I was referencing the following links for this purpose:

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

First, there is no reason at all you can't just use Tableau to perform your aggregations at the year and month grain - without having to pre-aggregate the data.

However, if you insist on preparing the data in SQL first, you have a couple of options:

1. UNION

This is a more normalized structure that will allow you to use t ("type") in as the measure to separate/delineate your data.

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

This is denormalized and closer to the existing table structure. Tableau can handle this design nicely, as well, by simply using the measures (hours) as dual axes.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM