SSIS转换:将SQL子查询转换为SSIS任务

时间:2019-07-16 06:52:58

标签: sql-server ssis etl

我正在尝试使用SSIS深入ETL的“转换”部分。我需要使用SSIS任务通过SQL查询(存储过程)完成任务。

输出将产生前五名三胞胎及其百分比贡献。我已经模拟了我在SQL Stored Proc中所做的事情。

您能指导我怎么做吗?

--Preparing Dataset
CREATE TABLE temp20190716
    (
        WeekNumber INT,
        Triplet VARCHAR(50),
        Product VARCHAR(50) ,
        CustomerType VARCHAR(50),
        Occurence INT
    )

INSERT INTO temp20190716
    (
    WeekNumber ,
    Triplet ,
    Product ,
    CustomerType ,
    Occurence 
    )
VALUES 
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4) ,'Mobile','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Mobile','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Mobile','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4) ,'Mobile','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Mobile','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Mobile','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4) ,'Mobile','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Mobile','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Mobile','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4) ,'Mobile','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Mobile','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Mobile','Consumer',FLOOR(RAND()*(25-10+1))+10),

    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Fixed','Consumer',FLOOR(RAND()*(25-10+1))+10),       
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Fixed','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Fixed','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Fixed','Consumer',FLOOR(RAND()*(25-10+1))+10),       
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Fixed','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Fixed','Consumer',FLOOR(RAND()*(25-10+1))+10) ,
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Fixed','Consumer',FLOOR(RAND()*(25-10+1))+10),       
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Fixed','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Fixed','Consumer',FLOOR(RAND()*(25-10+1))+10) ,
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Fixed','Consumer',FLOOR(RAND()*(25-10+1))+10),       
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Fixed','Consumer',FLOOR(RAND()*(25-10+1))+10),
    (201915,SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4)+':'+SUBSTRING(CONVERT(varchar(255), NEWID()),0,4),'Fixed','Consumer',FLOOR(RAND()*(25-10+1))+10) 

需要使用SSIS达到输出以下

SELECT 
*
FROM
(SELECT TOP 5
    Triplet,
    SUM(Occurence)Sort,
    Triplet+', '+CONVERT(VARCHAR,SUM(Occurence))+', '+ CONVERT(VARCHAR,SUM(Occurence)*100/(SELECT SUM(Occurence) FROM temp20190716 WHERE Product IN ('Mobile') AND CustomerType='Consumer' ))+' %' DisplayName,
    'Consumer Mobile' Tag ,
    CustomerType
FROM temp20190716
WHERE Product IN ('Mobile') AND CustomerType='Consumer'
GROUP BY Triplet ,CustomerType
ORDER BY 2 DESC
UNION 
SELECT TOP 5
    Triplet,
    SUM(Occurence)Sort,
    Triplet+', '+CONVERT(VARCHAR,SUM(Occurence))+', '+ CONVERT(VARCHAR,SUM(Occurence)*100/(SELECT SUM(Occurence) FROM temp20190716 WHERE Product IN ('Fixed') AND CustomerType='Consumer' ))+' %' DisplayName,
    'Consumer Fixed' Tag ,
    CustomerType
FROM temp20190716
WHERE Product IN ('Fixed') AND CustomerType='Consumer'
GROUP BY Triplet ,CustomerType
ORDER BY 2 DESC
)X

SQL输出:

enter image description here

1 个答案:

答案 0 :(得分:2)

如果您的经理绝对不允许您使用将查询作为存储的proc来运行的最佳实践,则可以通过两种方式解决此限制,具体取决于您要如何使用数据。

选项1:将查询用作源连接。您可以在使用查询而不是表/视图的OLE DB Source中创建一个Data Flow Task。看起来像这样:

enter image description here

选项2:在Execute SQL Task中的Control Flow中使用查询。在您的Execute SQL Task中,将ResultSet属性设置为Full Result Set并将其映射到Object类型的变量。然后,您可以像使用任何其他数据集对象一样使用该对象。如果您需要像Foreach Loop中那样迭代处理每一行,则这将是首选解决方案。