在几年内对多个列使用 COUNT()

Use COUNT() for multiple columns over several years

我有一个显示当年井数的查询。我想展示过去 5 年的 count/year。我如何在单个查询中执行此操作?

对于今年,我使用 YEAR(GETDATE())。在过去的五年里,我一直在想我可以使用:YEAR(GETDATE())-1YEAR(GETDATE())-2YEAR(GETDATE())-23 等。我还认为 CASE WHEN 子句可以,但不确定如何在SELECT语句下使用它。

这是我当前的查询:

SELECT
    COALESCE(w.WellType, 'Totals') AS 'WellTypes',
    COUNT(DISTINCT(w.WellID)) AS '2021'
FROM Well w
    LEFT JOIN Construct c ON c.WellKey = w.PKey
    LEFT JOIN ConstructDate cd ON c.PKey = cd.ConstructKey
WHERE
    YEAR(cd.EventDate) = YEAR(GETDATE())
    AND cd.Event = 'LATERALSTATUS'
    AND cd.Comment = 'PA'
GROUP BY ROLLUP(w.WellType)
ORDER BY
    CASE w.WellType
        WHEN 'OW' THEN 1
        WHEN 'GW' THEN 2
        WHEN 'D' THEN 3
        WHEN 'OWI' THEN 4
        WHEN 'WI' THEN 5
    END DESC

这是我目前的结果:

WellTypes 2021
WI 10
OWI 1
D 21
GW 40
OW 72
Totals 144

这是我想要得到的:

WellTypes 2021 2020 2019 2018 2017
WI 10 6 0 5 2
OWI 1 2 3 6 5
D 21 0 0 2 0
GW 40 6 2 0 7
OW 72 1 2 3 4
Totals 144 15 7 14 18

我正在努力在 db-fiddle 中获取它,但我以前从未使用过它。我知道这太长了,但现在,这里是 table 中的纯数据。 “EventDate”、“Comment”和“Event”列来自一个名为“ConstructDate”的table。 “WellType”列来自一个名为“Well”的 table。

EventDate WellType Comment Event
1/2/2017 OW PA LATERALSTATUS
1/3/2017 OW PA LATERALSTATUS
1/3/2017 OW PA LATERALSTATUS
1/3/2017 OW PA LATERALSTATUS
1/3/2017 WI PA LATERALSTATUS
1/3/2017 WI PA LATERALSTATUS
1/3/2017 OWI PA LATERALSTATUS
1/3/2017 OWI PA LATERALSTATUS
1/3/2017 OWI PA LATERALSTATUS
1/3/2017 OWI PA LATERALSTATUS
1/3/2017 OWI PA LATERALSTATUS
1/4/2017 GW PA LATERALSTATUS
1/4/2017 GW PA LATERALSTATUS
1/5/2017 GW PA LATERALSTATUS
1/5/2017 GW PA LATERALSTATUS
1/5/2017 GW PA LATERALSTATUS
1/5/2017 GW PA LATERALSTATUS
1/5/2017 GW PA LATERALSTATUS
1/1/2018 WI PA LATERALSTATUS
1/2/2018 WI PA LATERALSTATUS
1/2/2018 WI PA LATERALSTATUS
1/2/2018 WI PA LATERALSTATUS
1/2/2018 WI PA LATERALSTATUS
1/2/2018 OWI PA LATERALSTATUS
1/2/2018 OWI PA LATERALSTATUS
1/2/2018 OWI PA LATERALSTATUS
1/2/2018 OWI PA LATERALSTATUS
1/2/2018 OWI PA LATERALSTATUS
1/2/2018 OWI PA LATERALSTATUS
1/2/2018 D PA LATERALSTATUS
1/2/2018 D PA LATERALSTATUS
1/2/2018 OW PA LATERALSTATUS
1/2/2018 OW PA LATERALSTATUS
1/2/2018 OW PA LATERALSTATUS
1/2/2019 OWI PA LATERALSTATUS
1/2/2019 OWI PA LATERALSTATUS
1/2/2019 OWI PA LATERALSTATUS
1/2/2019 GW PA LATERALSTATUS
1/2/2019 GW PA LATERALSTATUS
1/2/2019 OW PA LATERALSTATUS
1/2/2019 OW PA LATERALSTATUS
1/1/2020 WI PA LATERALSTATUS
1/2/2020 WI PA LATERALSTATUS
1/2/2020 WI PA LATERALSTATUS
1/2/2020 WI PA LATERALSTATUS
1/2/2020 WI PA LATERALSTATUS
1/2/2020 WI PA LATERALSTATUS
1/2/2020 OWI PA LATERALSTATUS
1/2/2020 OWI PA LATERALSTATUS
1/2/2020 GW PA LATERALSTATUS
1/2/2020 GW PA LATERALSTATUS
1/2/2020 GW PA LATERALSTATUS
1/2/2020 GW PA LATERALSTATUS
1/2/2020 GW PA LATERALSTATUS
1/2/2020 GW PA LATERALSTATUS
1/2/2020 OW PA LATERALSTATUS
8/16/2021 WI PA LATERALSTATUS
8/16/2021 WI PA LATERALSTATUS
8/16/2021 WI PA LATERALSTATUS
8/16/2021 WI PA LATERALSTATUS
8/16/2021 WI PA LATERALSTATUS
8/16/2021 WI PA LATERALSTATUS
8/16/2021 WI PA LATERALSTATUS
8/16/2021 WI PA LATERALSTATUS
8/16/2021 WI PA LATERALSTATUS
8/16/2021 WI PA LATERALSTATUS
8/16/2021 OWI PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 D PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/16/2021 GW PA LATERALSTATUS
8/17/2021 GW PA LATERALSTATUS
8/17/2021 GW PA LATERALSTATUS
8/17/2021 GW PA LATERALSTATUS
8/17/2021 GW PA LATERALSTATUS
8/17/2021 GW PA LATERALSTATUS
8/17/2021 GW PA LATERALSTATUS
8/17/2021 GW PA LATERALSTATUS
8/17/2021 GW PA LATERALSTATUS
8/17/2021 GW PA LATERALSTATUS
8/18/2021 GW PA LATERALSTATUS
8/17/2021 GW PA LATERALSTATUS
8/17/2021 GW PA LATERALSTATUS
8/17/2021 OW PA LATERALSTATUS
8/17/2021 OW PA LATERALSTATUS
8/18/2021 OW PA LATERALSTATUS
8/18/2021 OW PA LATERALSTATUS
8/18/2021 OW PA LATERALSTATUS
8/19/2021 OW PA LATERALSTATUS
8/19/2021 OW PA LATERALSTATUS
8/19/2021 OW PA LATERALSTATUS
8/19/2021 OW PA LATERALSTATUS
8/19/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/20/2021 OW PA LATERALSTATUS
8/21/2021 OW PA LATERALSTATUS
8/21/2021 OW PA LATERALSTATUS
8/21/2021 OW PA LATERALSTATUS
8/21/2021 OW PA LATERALSTATUS
8/21/2021 OW PA LATERALSTATUS
8/21/2021 OW PA LATERALSTATUS
8/21/2021 OW PA LATERALSTATUS
8/22/2021 OW PA LATERALSTATUS
8/22/2021 OW PA LATERALSTATUS
8/23/2021 OW PA LATERALSTATUS
8/23/2021 OW PA LATERALSTATUS
8/23/2021 OW PA LATERALSTATUS
8/23/2021 OW PA LATERALSTATUS
8/23/2021 OW PA LATERALSTATUS
8/23/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS
8/24/2021 OW PA LATERALSTATUS

当您需要不同的约束来聚合您想要的内容时,这很棘手。我不会使用计数,而是使用 CASE 语句对实例求和。这是一个示例,您可以 运行 在 SQL 服务器上的会话中:

IF OBJECT_ID('TEMPDB..#TEMP') IS NOT NULL
    DROP TABLE #TEMP

CREATE TABLE #TEMP(
    WellType            NVARCHAR(10)
    ,EventDate          DATE
)
INSERT INTO #TEMP (WellType, EventDate)
VALUES ('OW','2021-11-03')
    ,('GW','2020-11-03')
    ,('D','2019-11-03')
    ,('OWI','2018-11-03')
    ,('WI','2017-11-03')
    ,('WI','2021-11-03')
    ,('D','2020-11-03')
    ,('D','2019-11-03')
    ,('GW','2018-11-03')
    ,('OW','2017-11-03')
    ,('OW','2021-11-03')
    ,('GW','2020-11-03')
    ,('D','2019-11-03')
    ,('OWI','2018-11-03')
    ,('WI','2017-11-03')
    ,('WI','2021-11-03')
    ,('D','2020-11-03')
    ,('D','2019-11-03')
    ,('GW','2018-11-03')
    ,('OW','2017-11-03')

SELECT 
    WellType
    ,SUM(CASE WHEN YEAR(EventDate) = YEAR(GETDATE()) THEN 1 ELSE 0 END) [THIS YEAR]
    ,SUM(CASE WHEN YEAR(EventDate) = YEAR(DATEADD(YEAR,-1,GETDATE())) THEN 1 ELSE 0 END) [LAST YEAR]
    ,SUM(CASE WHEN YEAR(EventDate) = YEAR(DATEADD(YEAR,-2,GETDATE())) THEN 1 ELSE 0 END) [2 YEARS AGO]
    ,SUM(CASE WHEN YEAR(EventDate) = YEAR(DATEADD(YEAR,-3,GETDATE())) THEN 1 ELSE 0 END) [3 YEARS AGO]
FROM #TEMP
GROUP BY WellType

我还使用 GETDATE() 函数来确定求和的年份。这应该可以满足您的要求。

SELECT 语句前后的输出:

如果没有涉及的表和相关数据,我无法重现您自己的查询,但是有多种方法可以将您需要的数据旋转到列中。

其中一种方法是使用 conditional case expression,另一种方法是使用 window analytic functions:

select distinct welltype, 
    Count(case when Year(Eventdate)=Year(GetDate())-0 then 1 end) over(partition by welltype ) [2021],
    Count(case when Year(Eventdate)=Year(GetDate())-1 then 1 end) over(partition by welltype ) [2020],
    Count(case when Year(Eventdate)=Year(GetDate())-2 then 1 end) over(partition by welltype ) [2019],
    Count(case when Year(Eventdate)=Year(GetDate())-3 then 1 end) over(partition by welltype ) [2018],
    Count(case when Year(Eventdate)=Year(GetDate())-4 then 1 end) over(partition by welltype ) [2017]
from t 

Demo Fiddle

很遗憾,没有数据我无法验证请求。我会尝试使用“Grouping Set”运算符结合“Case When”结构进行子查询,然后使用“SUM”聚合函数。

With Cte As (Select YEAR(GetDate()) As [YEAR], YEAR(GetDate())-4 As [first]             
             Union All 
             Select [YEAR]-1 As [YEAR], [first]
             From cte
             Where [YEAR] > [first])
Select WellTypes, SUM([2021]), SUM([2020]), SUM([2019]), SUM([2018]), SUM([2017])
From
(Select
    COALESCE(w.WellType, 'Totals') As WellTypes,
    Case When cte.[YEAR] = YEAR(GetDate()) Then COUNT(DISTINCT(w.WellID)) End As [2021],
    Case When cte.[YEAR] = Year(GetDate())-1 Then COUNT(DISTINCT(w.WellID)) End As [2020],
    Case When cte.[YEAR] = Year(GetDate())-2 Then COUNT(DISTINCT(w.WellID)) End As [2019],
    Case When cte.[YEAR] = Year(GetDate())-3 Then COUNT(DISTINCT(w.WellID)) End As [2018],
    Case When cte.[YEAR] = Year(GetDate())-4 Then COUNT(DISTINCT(w.WellID)) End As [2017]
From Well w
    Inner Join Construct c ON c.WellKey = w.PKey
    Inner Join ConstructDate cd ON c.PKey = cd.ConstructKey
    Inner Join cte On (YEAR(cd.EventDate)=cte.[YEAR])
Where
    And cd.Event = 'LATERALSTATUS'
    And cd.Comment = 'PA'
Group by Grouping Sets ((cte.[YEAR], w.WellType), (cte.[YEAR]))) As T
Group by WellTypes
Order by
    Case WellTypes
        When 'OW' Then 1
        When 'GW' Then 2
        When 'D' Then 3
        When 'OWI' Then 4
        When 'WI' Then 5
    End Desc