How/where 我使用 PIVOT 吗?

How/where do I use PIVOT?

我有一个 table 的数据,显示单个数据的多行。在下面的示例中,我有一个名为“位置类型”的列,其中显示“BH”和“TPI”。 BH 和 TPI 各有一个特定位置,显示在“位置”列中。我不想要 BH 和 TPI 的新行,而是想要两个新列来显示该行的位置数据。我在下面包含了几行数据。我怀疑我需要在这里使用 PIVOT,但我一直很难弄清楚 PIVOT 需要在哪里使用。有人可以提供一些指导或给我一个解决方案吗?

这是查询中的数据示例。

API14 First Prod Date Location Type Location
43013540070000 2/8/2021 BH Township 3S Range 4W Section 17 DUCHESNE County
43013540070000 2/8/2021 TPI Township 3S Range 4W Section 18 DUCHESNE County

这是我希望看到的格式:

API14 First Prod Date BH Location TPI Location
43013540070000 2/8/2021 Township 3S Range 4W Section 17 DUCHESNE County Township 3S Range 4W Section 18 DUCHESNE County

到目前为止,这是我的代码:

DECLARE @SearchYear AS VARCHAR(4) = '2021'
DECLARE @SearchMonth AS VARCHAR(2) = '6'

SELECT
    dbo.BuildAPI14(Well.WellID, Construct.SideTrack, Construct.Completion) AS 'API14',
    CAST(ConstructDate.EventDate AS DATE) AS 'First Prod Date',
    Loc.LocType AS 'Location Type',
    CONCAT('Township ',LocExt.Township,LocExt.TownshipDir,' ','Range ',LocExt.Range,LocExt.RangeDir,' Section ',LocExt.Sec,' ',RefCounty.CountyName,' County') AS 'Location',
    tblAPDTracker.SpacingRule AS 'Spacing Rule',
    Lease.Number AS 'Entity Number',
    WellHistory.WHComments AS 'Well History Comments'
FROM Well
    LEFT JOIN Construct ON Construct.WellKey = Well.PKey
    LEFT JOIN ConstructReservoir ON ConstructReservoir.ConstructKey = Construct.PKey
    LEFT JOIN Lease ON Lease.Pkey = ConstructReservoir.LeaseKey
    LEFT JOIN WellHistory ON WellHistory.WellKey = Construct.WellKey
    LEFT JOIN tblAPDTracker ON LEFT(tblAPDTracker.APINO,10) = Well.WellID
    LEFT JOIN Loc ON loc.ConstructKey = Construct.PKey AND Loc.LocType IN ('BH','TPI')
    LEFT JOIN LocExt ON LocExt.LocKey = Loc.PKey
    LEFT JOIN ConstructDate ON ConstructDate.ConstructKey = Construct.PKey AND ConstructDate.Event = 'FirstProduction'
    LEFT JOIN RefCounty ON RefCounty.PKey = LocExt.County
WHERE
    WorkType = 'ENTITY' AND
    WellHistory.ModifyUser = 'UTAH\rachelmedina' AND
    YEAR(WellHistory.ModifyDate) = @SearchYear AND
    MONTH(WellHistory.ModifyDate) = @SearchMonth
GROUP BY
    Well.WellID,
    Construct.SideTrack,
    Construct.Completion,
    ConstructDate.EventDate,
    Loc.LocType,
    LocExt.Township,
    LocExt.TownshipDir,
    LocExt.Range,
    LocExt.RangeDir,
    LocExt.Sec,
    RefCounty.CountyName,
    tblAPDTracker.SpacingRule,
    Lease.Number,
    WellHistory.WHComments,
    WellHistory.ModifyDate
ORDER BY
    Well.WellId,
    WellHistory.ModifyDate DESC
Select *
from
(
SELECT
    dbo.BuildAPI14(Well.WellID, Construct.SideTrack, Construct.Completion) AS 'API14',
    CAST(ConstructDate.EventDate AS DATE) AS 'First Prod Date',
    Loc.LocType AS 'Location Type',
    CONCAT('Township ',LocExt.Township,LocExt.TownshipDir,' ','Range ',LocExt.Range,LocExt.RangeDir,' Section ',LocExt.Sec,' ',RefCounty.CountyName,' County') AS 'Location' --,
--    tblAPDTracker.SpacingRule AS 'Spacing Rule',
--    Lease.Number AS 'Entity Number',
--    WellHistory.WHComments AS 'Well History Comments'
FROM Well
    LEFT JOIN Construct ON Construct.WellKey = Well.PKey
    LEFT JOIN ConstructReservoir ON ConstructReservoir.ConstructKey = Construct.PKey
    LEFT JOIN Lease ON Lease.Pkey = ConstructReservoir.LeaseKey
    LEFT JOIN WellHistory ON WellHistory.WellKey = Construct.WellKey
    LEFT JOIN tblAPDTracker ON LEFT(tblAPDTracker.APINO,10) = Well.WellID
    LEFT JOIN Loc ON loc.ConstructKey = Construct.PKey AND Loc.LocType IN ('BH','TPI')
    LEFT JOIN LocExt ON LocExt.LocKey = Loc.PKey
    LEFT JOIN ConstructDate ON ConstructDate.ConstructKey = Construct.PKey AND ConstructDate.Event = 'FirstProduction'
    LEFT JOIN RefCounty ON RefCounty.PKey = LocExt.County
WHERE
    WorkType = 'ENTITY' AND
    WellHistory.ModifyUser = 'UTAH\rachelmedina' AND
    YEAR(WellHistory.ModifyDate) = @SearchYear AND
    MONTH(WellHistory.ModifyDate) = @SearchMonth
GROUP BY
    Well.WellID,
    Construct.SideTrack,
    Construct.Completion,
    ConstructDate.EventDate,
    Loc.LocType,
    LocExt.Township,
    LocExt.TownshipDir,
    LocExt.Range,
    LocExt.RangeDir,
    LocExt.Sec,
    RefCounty.CountyName,
    tblAPDTracker.SpacingRule,
    Lease.Number,
    WellHistory.WHComments,
    WellHistory.ModifyDate
    ) p
pivot (min(p.Location) for [Location Type] in ([TPI], [BH])) pvt

旋转列中的值需要一个聚合运算符(sum、avg、min、max 等),所以选择一个像 min 或 max 这样的运算符,它们不会尝试对字符串做任何事情,但如果您在旋转列中多次出现(位置在这里)。

我还注释掉了未出现在您的示例结果中的所选列,这可能会影响需要出现在 GROUP BY 子句中的内容。

枢轴的另一种替代方法是使用不同的别名重新加入 tables 以区分“BH”与“TPI”实例。我还简化了较长的 table 名称,使用短别名“w”表示 Well,“wh”表示 wellhouse 等

此外,通过查看 A -> B -> C -> D tables 的层次结构有助于确定相关组件的位置。然后我把那些复制下来用于位置。

我删除了位置类型作为列和分组依据,因为查询分别同时连接到 BH 和 TPI。

DECLARE @SearchYear AS VARCHAR(4) = '2021'
DECLARE @SearchMonth AS VARCHAR(2) = '6'

SELECT
        dbo.BuildAPI14(w.WellID, c.SideTrack, c.Completion) API14,
        CAST(cd.EventDate AS DATE) 'First Prod Date',
        CONCAT('Township ', LocExtBH.Township, LocExtBH.TownshipDir,
                ' ','Range ', LocExtBH.Range, LocExtBH.RangeDir,
                ' Section ', LocExtBH.Sec,
                ' ', rcBH.CountyName, ' County') 'BH Location',

        CONCAT('Township ', LocExtTPI.Township, LocExtTPI.TownshipDir,
                ' ','Range ', LocExtTPI.Range, LocExtTPI.RangeDir,
                ' Section ', LocExtTPI.Sec,
                ' ', rcTPI.CountyName, ' County') 'TPI Location',

        ADP.SpacingRule 'Spacing Rule',
        l.Number  'Entity Number',
        wh.WHComments 'Well History Comments'
    FROM 
        Well w
            LEFT JOIN tblAPDTracker ADP
                ON w.WellID = LEFT(trk.APINO,10)
            LEFT JOIN Construct c
                ON w.PKey = c.WellKey
                LEFT JOIN ConstructReservoir cr
                    ON c.PKey = cr.ConstructKey
                    LEFT JOIN Lease l
                        ON cr.LeaseKey = l.Pkey
                LEFT JOIN WellHistory wh
                    ON c.WellKey = wh.WellKey
                LEFT JOIN ConstructDate cd
                    ON c.PKey = cd.ConstructKey 
                    AND cd.Event = 'FirstProduction'

                LEFT JOIN Loc LocBH
                    ON c.PKey = locBH.ConstructKey 
                    AND LocBH.LocType = 'BH'
                    LEFT JOIN LocExt LocExtBH
                        ON LocBH.PKey = LocExtBH.LocKey
                        LEFT JOIN RefCounty rcBH
                            ON LocExtBH.County = rcBH.PKey 

                LEFT JOIN Loc LocTPI
                    ON c.PKey = LocTPI.ConstructKey 
                    AND LocTPI.LocType = 'TPI'
                    LEFT JOIN LocExt LocExtTPI
                        ON LocTPI.PKey = LocExtTPI.LocKey
                        LEFT JOIN RefCounty rcTPI
                            ON LocExtTPI.County = rcTPI.PKey 
    WHERE
            w.WorkType = 'ENTITY' 
        AND wh.ModifyUser = 'UTAH\rachelmedina' 
        AND YEAR(wh.ModifyDate) = @SearchYear 
        AND MONTH(wh.ModifyDate) = @SearchMonth
    GROUP BY
        w.WellID,
        c.SideTrack,
        c.Completion,
        cd.EventDate,
        LocExtBH.Township,
        LocExtBH.TownshipDir,
        LocExtBH.Range,
        LocExtBH.RangeDir,
        LocExtBH.Sec,
        rcBH.CountyName,
        ADP.SpacingRule,
        l.Number,
        wh.WHComments,
        wh.ModifyDate
    ORDER BY
        w.WellId,
        wh.ModifyDate DESC