2

我们可以讨论如何使用 ssis 将以下查询结果加载到目标表中。我知道我们可以在 T-sql 中使用它,也可以作为 OLEDB 源查询。但仍然想知道如何实现它,只使用数据流组件

SELECT
     CLIENTID       = CAST(PER.CLIENTID AS INT)
    ,CASEID         = CAST(CS.CASEID AS INT)
    ,CAST(RIGHT(ev.oid, 10) as int) AS EventID
    ,ev.ServiceSubtypeCode
    ,ev.ServiceSubtypeCode  +' - '+ev.ServiceSubTypeDesc as ServiceSubTypeDesc
    ,WU.ProviderID as WorkunitProviderID
    ,WU.ProviderName as WorkUnitProviderName
    ,ev.eventstartdate as AssessmentStartDate
    ,CONVERT(CHAR(5),ASM.getstarttimestamp,8) as AssessmentStartTime
    ,ev.EVENTENDDATE as AssessmentEndDate
    ,ev.EVENTENDTIME as AssessmentEndTime
    ,CAST(asm.getAssmtTemplateName as nvarchar(200)) as AssessmentTypeDesc
    ,j.providerid
    ,j.ProviderName
    ,j.ProviderRole
    ,EV.ISCOMPLETED
    , EV.ISFINALISED 
    ,EV.ISREVOKED
    , EV.REVOKEDDATE AS REVOKEDDATE
    ,ASM.OID AS ASSESSMENTID

FROM DBO.ASSESSMENT ASM
LEFT OUTER JOIN DBO.INDIVIDUALPERSON PER        ON ASM.MYPERSON = PER.OID
LEFT OUTER JOIN DBO.[CASE] CS       ON ASM.MYCASE = CS.OID
LEFT OUTER JOIN (
                        SELECT CAST(ST.CODE AS VARCHAR(8))AS SERVICETYPECODE
                                , CAST(ST.DESCRIPTION AS VARCHAR(100)) AS SERVICETYPEDESC
                                , CAST(SST.CODE AS VARCHAR(8)) AS SERVICESUBTYPECODE
                                , CAST(SST.DESCRIPTION AS VARCHAR(100)) AS SERVICESUBTYPEDESC
                                , DATEADD(DD,0, DATEDIFF(DD,0,EV.GETRPSSTARTTIMESTAMP)) AS EVENTSTARTDATE
                                , CONVERT(CHAR(5),EV.GETRPSSTARTTIMESTAMP,8) AS EVENTSTARTTIME
                                , DATEADD(DD,0, DATEDIFF(DD,0,EV.GETRPSENDTIMESTAMP)) AS EVENTENDDATE
                                , CONVERT(CHAR(5),EV.GETRPSENDTIMESTAMP,8) AS EVENTENDTIME
                                ,CAST(VEN.DESCRIPTION AS VARCHAR(12)) AS EVENTVENUE
                                ,EV.ISCOMPLETED
                                , EV.ISFINALISED 
                                ,EV.ISREVOKED
                                , DATEADD(DD,0, DATEDIFF(DD,0,EV.REVOKEDON)) AS REVOKEDDATE 
                                , EV.OID 
                                        from Event ev
                                        LEFT OUTER JOIN     ServiceType AS st ON ev.myServiceType = st.oid 
                                        LEFT OUTER JOIN  ServiceSubtype AS sst ON ev.myServiceSubtype = sst.oid
                                        LEFT OUTER JOIN  AllCodes AS ven ON ev.myEventVenueCode = ven.oid 
                                     )as  EV
ON ASM.MYEVENT = EV.OID
LEFT OUTER JOIN     (
                                        select wu.oid
                                        ,CAST(wu.providerid AS VARCHAR(100)) AS providerid
                                        ,CAST(nm.getfullname AS VARCHAR(100)) AS ProviderName
                                        ,wu.contactname
                                        ,wu.activatedate as StartDate
                                        ,wu.deactivatedate as EndDate
                                        ,case when wu.deactivatedate is null then 1 else 0 end as IsActiveToday
                                        from workunitprovider wu
                                        LEFT OUTER JOIN  dbo.allprovidernames nm    ON wu.oid = nm.myprovider
                                        where nm.myNameType in (02245.0000000252)
                                    ) as WU
ON ASM.MYWORKUNITPROVIDER = WU.OID
Left join   (                   
                                select f.myEvent
                                ,f.myProvider
                                ,f.myproviderrolecode
                                ,f.Max_ProvOid
                                ,CAST(g.providerid AS VARCHAR(100)) AS providerid
                                ,CAST( i.description AS VARCHAR(150)) AS ProviderRole
                                ,cast (h.getFullName as nvarchar (150)) as ProviderName
                                    from(           select d.myEvent
                                                        ,myProvider
                                                        ,myproviderrolecode
                                                        ,d.Max_ProvOid
                                                         from (                 select  A.myEvent, max(b.oid) as Max_ProvOid
                                                                                    from alleventitems a
                                                                                    left outer join ProviderEventItemRole as b  on a.oid = b.myeventitem
                                                                                    group by A.myEvent
                                                                        ) as d
                                                                        left join 
                                                                        (                           select  A.myEvent,b.myProvider,b.myproviderrolecode,a.oid as a_oid,b.oid as b_oid
                                                                                                    from alleventitems a
                                                                                                    left outer join ProviderEventItemRole as b  on a.oid = b.myeventitem
                                                                        )as e on d.myevent = e.myevent and max_provOid = b_oid
                                ) as f
                                left join dbo.allproviders as g on f.myProvider = g.oid
                                left join (
                                                    select * 
                                                    from dbo.AllProviderNames 
                                                    where mynametype ='02245.0000000252'
                                                )as h on f.myprovider =h.myprovider
                                left join dbo.allcodes as i on f.myproviderrolecode = i.oid 
    )as j on ASM.myevent = j.myevent;
4

1 回答 1

2

在我们开始之前,免责声明:

复杂的 SELECT 查询最好用 T-SQL 表示。SSIS 最适合用于 ETL 任务。

现在……不碍事。让我们看看我们有什么。该查询有 15 个LEFT JOINS嵌套在三个级别:顶部有 5 个,中间有 7 个,底部有 2 个。贯穿始终的是一些CAST()s 和GROUP BYs。所有这些 SQL 命令都可以通过 SSIS 组件完成。

  • JOIN = 合并连接组件。
  • GROUP BY = 聚合组件。
  • CAST = 派生列组件。

由于您有这么大的查询,我建议将其分成更小的块。从最里面的连接开始。

    select 
        d.myEvent
        ,myProvider
        ,myproviderrolecode
        ,d.Max_ProvOid
    from (                 
        select  A.myEvent, max(b.oid) as Max_ProvOid
        from alleventitems a
        left outer join ProviderEventItemRole as b  
        on a.oid = b.myeventitem
        group by A.myEvent
    ) as d
    left join (                           
        select  A.myEvent,b.myProvider,b.myproviderrolecode,a.oid as a_oid,b.oid as b_oid
        from alleventitems a
        left outer join ProviderEventItemRole as b  
        on a.oid = b.myeventitem
    ) as e 

将其转换为 SSIS 看起来像这样。

在此处输入图像描述

上面,我们将四个表合并为一个。您可以在此处了解有关如何配置合并联接的更多信息。对剩余的 JOINS 重复上述模式并将它们连接在一起,您将把整个查询转换为 SSIS!


现在我们可以看到它是如何完成的,请问为什么我们要在 SSIS 中这样做?

于 2016-11-24T02:01:52.033 回答