这段代码做什么是无法理解的

时间:2013-08-09 10:56:42

标签: sql join

我正在试图找出一个查询正在做什么,我只是不明白为什么它会在多个场景中加入自己的自我突出显示位是我正在讨论的部分?

以[SupplyStatusUpdated] = COALESCE(供气和供气历史

)开头的部分
                        USE [CRM];

        SELECT  gs.GasSupplyID, 
                [Account ID] = acc.AccountID,
                [GMSReference] = cast(gms.SiteRefNum as varchar),
                [AccountNumber] = cast(gms.AccountNum as varchar), 
                [Organisation Name] = prf.[Name] ,
                con.DeclaredDate, 
                [Contract Date] = CAST(con.ContractDate AS DATE),
                [Contract Version] = cv.Name,
                [Contract Status] = cs.Name,
                loa.ContractEndDate [LOA CED],
                gs.CurrentSupplierEndDate [PrevSupplierEndDate],
                loa.ContractEndDate,
                con.StartDate,
                [Supply Status] = gss.Name,
                [SupplyStatusUpdated] = COALESCE(
                                                    (   
                                                        SELECT TOP 1 MAX(gsh2.CreatedDate)
                                                        FROM GasSupply gs2 
                                                            INNER JOIN GasSupplyHistory gsh2 
                                                                ON gsh2.GasSupplyFK = gs2.GasSupplyID
                                                        WHERE gsh2.EventFK = 2 
                                                        AND gsh2.Notes LIKE '%Gas Supply Status (%'
                                                        AND gsh2.GasSupplyFK = gs.GasSupplyID
                                                        GROUP BY gsh2.GasSupplyFK
                                                    ),
                                                    (
                                                        SELECT TOP 1 MAX(gsh3.CreatedDate)
                                                        FROM GasSupplyHistory gsh3 
                                                            INNER JOIN  (
                                                                            SELECT gsh4.GasSupplyFK, MAX(gsh4.EventFK) AS [MaxEvent]
                                                                            FROM GasSupplyHistory gsh4
                                                                            WHERE gsh4.GasSupplyFK = gs.GasSupplyID
                                                                            GROUP BY gsh4.GasSupplyFK
                                                                            HAVING MAX(gsh4.EventFK) = 1
                                                                        ) dt 
                                                                ON dt.GasSupplyFK = gsh3.GasSupplyFK
                                                    )
                                                ),
                [EAC] = gs.EstimatedAnnualConsumption,
                [PreviousSupplier] = r.name,
                [Branch] = b.name,
                [LeadSource] = ls.name,
                gs.UnitPrice,
                gs.StandingCharge,
                gs.WholesalePrice,
                COALESCE(deal.weeknumber,DATEPART(ISOWK, con.[ContractDate])) AS [Week]
        FROM    acc.Account acc
            INNER JOIN [Profile] prf    
                ON acc.ProfileFK = prf.ProfileID
            INNER JOIN [Contract] con       
                ON acc.AccountID = con.AccountFK
            INNER JOIN [loacontract] lo
                ON lo.ContractFK = con.ContractID
            LEFT join [LeadSource] ls
                ON ls.LeadSourceID = con.LeadSourceFK
            INNER JOIN [ContractStatus] cs
                ON cs.ContractStatusID = con.ContractStatusFK 
            INNER JOIN [ContractVersion] cv
                ON cv.ContractVersionID = con.ContractVersionFK 
            INNER JOIN GasSupply gs
                ON gs.ContractFK = con.ContractID
            INNER JOIN GasSupplyStatus gss
                ON gss.GasSupplyStatusID = gs.GasSupplyStatusFK 
            LEFT JOIN Deal deal
                ON deal.ContractFK = con.ContractID
            OUTER APPLY GetGMSReferenceNumbers(con.ContractID) gms
            LEFT JOIN Branch b
                ON b.BranchID = deal.BranchFK
            LEFT JOIN DealBroker bro
                ON  bro.DealFK = deal.DealID
                AND bro.BrokerTypeFK = 36
            LEFT JOIN Person p
                ON p.PersonID = bro.EmployeeFK
            LEFT JOIN Reseller r
                ON r.ResellerID = gs.ResellerFK
            LEFT JOIN   (
                            SELECT  l.contractfk,
                                    MIN(l.contractenddate)[ContractEndDate]
                            FROM    CRM.[contract].LOAContract l
                            GROUP BY l.ContractFK       
                        ) loa
                ON loa.ContractFK = con.ContractID

        WHERE acc.AccountID not in  (
                                        select AccountFK 
                                        from AccountOption 
                                        where OptionFK=9
                                    )
        AND  cast(gms.SiteRefNum as varchar) IS NULL

3 个答案:

答案 0 :(得分:1)

COALESCE(Something, SomethingElse)表示如果第一个参数为NULL,则返回第二个参数(请注意,您可以拥有2个以上的参数,并且它将继续沿着列表继续)。

因此它正在运行第一个子查询,如果结果为NULL,则返回第二个查询的结果。为什么你的业务逻辑究竟是我们无法回答的: - )

MSDN link on Coalesce

答案 1 :(得分:0)

COALESCE返回它找到的第一个非null值,因此如果GasSupply查询返回结果,它将使用:如果返回null,它将查看GasSupplyHistory查询返回结果,如果是,则使用它。如果两个查询都返回null,则SupplyStatusUpdated将为null。

答案 2 :(得分:0)

它自己加入以获取与获取当前记录的源相同的历史记录。

历史数据的表示是使用BigData / NoSQL数据库而不是SQL / Relational数据库的一个常见原因。