如何计算带有外部查询的子查询 CASE WHEN 类别?

How do I count subquery CASE WHEN categories with an outer query?

我在这个 db<>fiddle 中有一组示例数据。数据表示根据某些标准属于不同井类型类别的一批井。我正在尝试按井所属的类别对井进行分组,然后根据另一组标准计算每个类别中有多少口井。

我当前的查询部分有效,但只能正确计算 CASE WHEN 子句层次结构中较高的井。这是因为第一个 CASE WHEN 有机会将井类别分配给数据集中的所有井。但是,当它遍历每个 CASE WHEN 子句时,查询“看到的”更少的井,因为它用完了可以为其分配类别的井。当它到达终点时,几乎所有的井都已经分配了一个类别,从而根本不会发生某些类别计数。

这是我当前的查询,也在上面的db<>fiddle link中:

SELECT
    dt.WellCategory,
    ISNULL(SUM(CASE WHEN dt.LeaseType IN ('F','I','S','P') OR dt.LeaseType NOT IN ('F', 'I', 'S', 'P', 'U') THEN 1 END), 0) AS [Number of Wells], -- Federal + Indian + State + Fee + Multi-Lease
    ISNULL(SUM(CASE WHEN dt.LeaseType = 'F' THEN 1 END), 0) AS [Federal], -- Sums up how many wells from the subquery have a leasetype of "Federal"
    ISNULL(SUM(CASE WHEN dt.LeaseType = 'I' THEN 1 END), 0) AS [Indian],
    ISNULL(SUM(CASE WHEN dt.LeaseType = 'S' THEN 1 END), 0) AS [State],
    ISNULL(SUM(CASE WHEN dt.LeaseType = 'P' THEN 1 END), 0) AS [Fee (Private)],
    ISNULL(SUM(CASE WHEN dt.LeaseType NOT IN ('F', 'I', 'S', 'P', 'U') THEN 1 END), 0) AS [Multiple Lease Types]
FROM
(
    SELECT -- Subquery labels wells according to their wellstatus, welltype, etc.
        c.LeaseType,
        CASE 
            WHEN w.WellStatus = 'p' AND w.WellType = 'gw' OR ((w.WellStatus = 'pai' OR w.WellStatus = 'pii') AND (w.WellType = 'gwi' OR w.WellType = 'ggi' OR w.WellType = 'gwd')) THEN 'Producing Gas Wells'
            WHEN w.WellStatus = 'p' AND w.WellType = 'ow' OR ((w.WellStatus = 'pai' OR w.WellStatus = 'pii') AND (w.WellType = 'owi' OR w.WellType = 'ogi' OR w.WellType = 'owd')) THEN 'Producing Oil Wells'
            WHEN w.WellStatus = 's' AND w.WellType = 'ow' OR ((w.WellStatus = 'sai' OR w.WellStatus = 'sii') AND (w.WellType = 'owi' OR w.WellType = 'ogi' OR w.WellType = 'owd')) THEN 'Shut-In Oil Wells'
            WHEN w.WellStatus = 's' AND w.WellType = 'gw' OR ((w.WellStatus = 'sai' OR w.WellStatus = 'sii') AND (w.WellType = 'gwi' or w.WellType = 'ggi' or w.WellType = 'gwd')) THEN 'Shut-In Gas Wells'
            WHEN w.WellStatus = 'a' AND w.WellType = 'wi' THEN 'Active Water Injection Wells'
            WHEN w.WellStatus = 'a' AND w.WellType = 'gi' THEN 'Active Gas Injection Wells'
            WHEN w.WellStatus = 'a' AND w.WellType = 'wd' THEN 'Active Water Disposal Wells'
            WHEN w.WellStatus = 'a' AND w.WellType = 'gs' THEN 'Active Gas Storage Wells'
            WHEN w.WellStatus = 'a' AND w.WellType = 'ws' THEN 'Active Water Source Wells'
            WHEN w.WellStatus = 'a' AND w.WellType = 'tw' THEN 'Active Test Holes'
            WHEN w.WellStatus = 'i' AND w.WellType = 'wi' THEN 'Inactive Water Injection Wells'
            WHEN w.WellStatus = 'i' AND w.WellType = 'gi' THEN 'Inactive Gas Injection Wells'
            WHEN w.WellStatus = 'i' AND w.WellType = 'wd' THEN 'Inactive Water Disposal Wells'
            WHEN w.WellStatus = 'i' AND w.WellType = 'gs' THEN 'Inactive Gas Storage Wells'
            WHEN w.WellStatus = 'i' AND w.WellType = 'ws' THEN 'Inactive Water Source Wells'
            WHEN w.WellStatus = 'i' AND w.WellType = 'tw' THEN 'Inactive Test Holes'
            WHEN w.WellStatus = 'ta' THEN 'Temporarily-Abandoned Wells'
            WHEN w.WellStatus = 'pa' THEN 'Plugged and Abandoned Wells'
            WHEN c.LateralStatus = 'NEW' THEN 'New Permits - Not Yet Approved'
            WHEN c.LateralStatus = 'APD' THEN 'Permits Approved - Not Yet Commenced'
            WHEN c.LateralStatus = 'DRL' THEN 'Drilling Commenced - Not Yet Completed'
            WHEN c.LateralStatus = 'OPS' THEN 'Drilling Operations Suspended'
            WHEN w.WellStatus IN ('drl','ops','p','s','ta','pai','pii','sai','sii','a','i') AND w.Operator = 101600 THEN 'Open Orphan Wells (no known operator)'
            WHEN w.WellStatus IN ('drl','ops') THEN 'Total Holes Not Yet Completed'
            WHEN ((w.WellStatus = 'p' or w.WellStatus = 's') AND w.WellType <> 'LI') OR (w.WellStatus = 'pai' OR w.WellStatus = 'pii' OR w.WellStatus = 'sai' OR w.WellStatus = 'sii') THEN 'Total Wells Capable of Production'
            WHEN (w.WellStatus = 'drl' OR w.WellStatus = 'ops' OR ((w.WellStatus = 'p' or w.WellStatus = 's') AND w.WellType <> 'LI') OR w.WellStatus = 'ta' OR w.WellStatus = 'pai' OR w.WellStatus = 'pii' OR w.WellStatus = 'sai' OR w.WellStatus = 'sii' OR w.WellStatus = 'a' OR w.WellStatus = 'i') THEN 'Total Non-Plugged Wells'
            WHEN (w.WellStatus = 'drl' or w.WellStatus = 'ops' or ((w.WellStatus = 'p' or w.WellStatus = 's') and w.WellType <> 'LI') or w.WellStatus = 'ta' or w.WellStatus = 'pai' or w.WellStatus = 'pii' or w.WellStatus = 'sai' or w.WellStatus = 'sii' or w.WellStatus = 'a' or w.WellStatus = 'i' or w.WellStatus = 'pa') THEN 'Total Wells Drilled'
        END AS WellCategory
    FROM HWell w
        LEFT JOIN HConstruct c ON c.WellKey = w.PKey
) dt
GROUP BY dt.WellCategory
ORDER BY dt.WellCategory DESC

这里是 table 上面的查询产生的结果:

Table#1

WellCategory Number of Wells Federal Indian State Fee (Private) Multiple Lease Types
Total Wells Capable of Production 2 2 0 0 0 0
Temporarily-Abandoned Wells 1 1 0 0 0 0
Shut-In Oil Wells 21 10 10 0 1 0
Shut-In Gas Wells 26 19 2 4 1 0
Producing Oil Wells 59 18 25 6 10 0
Producing Gas Wells 90 59 1 25 5 0
Plugged and Abandoned Wells 113 60 15 19 19 0
Permits Approved - Not Yet Commenced 14 4 2 2 4 2
New Permits - Not Yet Approved 1 0 1 0 0 0
Inactive Water Injection Wells 18 11 5 2 0 0
Drilling Operations Suspended 4 1 3 0 0 0
Drilling Commenced - Not Yet Completed 4 1 1 0 2 0
Active Water Injection Wells 6 1 5 0 0 0
Active Water Disposal Wells 1 0 0 1 0 0
NULL 140 83 28 14 15 0

这是 table 我知道可以生成相同的数据集和我想复制的数据集:

Table#2

Well Statuses Number Of Wells Federal Indian State Fee (Private) Multiple Lease Types
Producing Oil Wells 59 18 25 6 10 0
Producing Gas Wells 90 59 1 25 5 0
Shut-In Oil Wells 21 10 10 0 1 0
Shut-In Gas Wells 26 19 2 4 1 0
Active Water Injection Wells 6 1 5 0 0 0
Active Gas Injection Wells 0 0 0 0 0 0
Active Water Disposal Wells 1 0 0 1 0 0
Active Gas Storage Wells 0 0 0 0 0 0
Active Water Source Wells 0 0 0 0 0 0
Active Test Holes 0 0 0 0 0 0
Inactive Water Injection Wells 18 11 5 2 0 0
Inactive Gas Injection Wells 0 0 0 0 0 0
Inactive Water Disposal Wells 0 0 0 0 0 0
Inactive Gas Storage Wells 0 0 0 0 0 0
Inactive Water Source Wells 0 0 0 0 0 0
Inactive Test Holes 0 0 0 0 0 0
Temporarily-Abandoned Wells 1 1 0 0 0 0
Plugged and Abandoned Wells 113 60 15 19 19 0
New Permits - Not Yet Approved 1 0 1 0 0 0
Permits Approved - Not Yet Commenced 14 4 2 2 4 2
Drilling Commenced - Not Yet Completed 4 1 1 0 2 0
Drilling Operations Suspended 4 1 3 0 0 0
Open Orphan Wells (no known operator) 1 1 0 0 0 0
Total Holes Not Yet Completed 8 2 4 0 2 0
Total Wells Capable of Production 198 108 38 35 17 0
Total Non-Plugged Wells 232 123 52 38 19 0
Total Wells Drilled 345 183 67 57 38 0

Table #2 是使用更长的查询生成的,该查询使用多个子查询和临时 tables 来计算每个类别中有多少口井属于 DISTINCT w.WellID。此外,完整的数据集有更多的条目,通常每个类别中至少有一些井而不是那么多“0”。

我不知道如何告诉查询多次计算井的数量,如果它们属于几个井类别而不使查询超长并开始引入临时 tables,我不希望这样做去做。 *注意我不想为同一类别计算两次,每个类别只计算一次。

您可以执行此操作的一种方法是为每个条件生成一行,然后汇总那些 return 匹配项,这可以使用 cross applyvalues table 生成器确保所有 case 表达式都针对所有孔进行评估。

目前这种方法假设每个孔只有一个WellCategory值。如果可以有多个,那么您将需要自己进行这些更改。我也没有将输出包装在 pivot 中,因为我认为这通常最好在您的表示层中完成,而不是原始的 SQL.

我建议将 SQL 输出保留为这种格式,因为演示工具在动态处理新类别(例如添加了新的 LeaseType)方面比 SQL 更好,后者在这种规范化格式中不需要更改脚本或表示层,除非有任何定制标签。

如果您想保留旋转输出,只需将此查询包装在当前的外部 select:

查询

select c.LeaseType
      ,cat.WellCategory
      ,count(w.PKey) as Wells
from @HWell as w
    left join @HConstruct as c
        on w.Pkey = c.WellKey
    cross apply(values(case when w.WellStatus = 'p' AND w.WellType = 'gw' OR ((w.WellStatus = 'pai' OR w.WellStatus = 'pii') AND (w.WellType = 'gwi' OR w.WellType = 'ggi' OR w.WellType = 'gwd')) then 'Producing Gas Wells' end)
                     ,(case when w.WellStatus = 'p' AND w.WellType = 'ow' OR ((w.WellStatus = 'pai' OR w.WellStatus = 'pii') AND (w.WellType = 'owi' OR w.WellType = 'ogi' OR w.WellType = 'owd')) then 'Producing Oil Wells' end) -- Need to count DISTINCT WellID's to get to 4770
                     ,(case when w.WellStatus = 's' AND w.WellType = 'ow' OR ((w.WellStatus = 'sai' OR w.WellStatus = 'sii') AND (w.WellType = 'owi' OR w.WellType = 'ogi' OR w.WellType = 'owd')) then 'Shut-In Oil Wells' end)
                     ,(case when w.WellStatus = 's' AND w.WellType = 'gw' OR ((w.WellStatus = 'sai' OR w.WellStatus = 'sii') AND (w.WellType = 'gwi' or w.WellType = 'ggi' or w.WellType = 'gwd')) then 'Shut-In Gas Wells' end)
                     ,(case when w.WellStatus = 'a' AND w.WellType = 'wi' then 'Active Water Injection Wells' end)
                     ,(case when w.WellStatus = 'a' AND w.WellType = 'gi' then 'Active Gas Injection Wells' end)
                     ,(case when w.WellStatus = 'a' AND w.WellType = 'wd' then 'Active Water Disposal Wells' end)
                     ,(case when w.WellStatus = 'a' AND w.WellType = 'gs' then 'Active Gas Storage Wells' end)
                     ,(case when w.WellStatus = 'a' AND w.WellType = 'ws' then 'Active Water Source Wells' end)
                     ,(case when w.WellStatus = 'a' AND w.WellType = 'tw' then 'Active Test Holes' end)
                     ,(case when w.WellStatus = 'i' AND w.WellType = 'wi' then 'Inactive Water Injection Wells' end)
                     ,(case when w.WellStatus = 'i' AND w.WellType = 'gi' then 'Inactive Gas Injection Wells' end)
                     ,(case when w.WellStatus = 'i' AND w.WellType = 'wd' then 'Inactive Water Disposal Wells' end)
                     ,(case when w.WellStatus = 'i' AND w.WellType = 'gs' then 'Inactive Gas Storage Wells' end)
                     ,(case when w.WellStatus = 'i' AND w.WellType = 'ws' then 'Inactive Water Source Wells' end)
                     ,(case when w.WellStatus = 'i' AND w.WellType = 'tw' then 'Inactive Test Holes' end)
                     ,(case when w.WellStatus = 'ta' then 'Temporarily-Abandoned Wells' end)
                     ,(case when w.WellStatus = 'pa' then 'Plugged and Abandoned Wells' end)
                     ,(case when c.LateralStatus = 'NEW' then 'New Permits - Not Yet Approved' end)
                     ,(case when c.LateralStatus = 'APD' then 'Permits Approved - Not Yet Commenced' end)
                     ,(case when c.LateralStatus = 'DRL' then 'Drilling Commenced - Not Yet Completed' end)
                     ,(case when c.LateralStatus = 'OPS' then 'Drilling Operations Suspended' end)
                     ,(case when w.WellStatus IN ('drl','ops','p','s','ta','pai','pii','sai','sii','a','i') AND w.Operator = 101600 then 'Open Orphan Wells (no known operator)' end)
                     ,(case when w.WellStatus IN ('drl','ops') then 'Total Holes Not Yet Completed' end)
                     ,(case when ((w.WellStatus = 'p' or w.WellStatus = 's') AND w.WellType <> 'LI') OR (w.WellStatus = 'pai' OR w.WellStatus = 'pii' OR w.WellStatus = 'sai' OR w.WellStatus = 'sii') then 'Total Wells Capable of Production' end)
                     ,(case when (w.WellStatus = 'drl' OR w.WellStatus = 'ops' OR ((w.WellStatus = 'p' or w.WellStatus = 's') AND w.WellType <> 'LI') OR w.WellStatus = 'ta' OR w.WellStatus = 'pai' OR w.WellStatus = 'pii' OR w.WellStatus = 'sai' OR w.WellStatus = 'sii' OR w.WellStatus = 'a' OR w.WellStatus = 'i') then 'Total Non-Plugged Wells' end)
                     ,(case when (w.WellStatus = 'drl' or w.WellStatus = 'ops' or ((w.WellStatus = 'p' or w.WellStatus = 's') and w.WellType <> 'LI') or w.WellStatus = 'ta' or w.WellStatus = 'pai' or w.WellStatus = 'pii' or w.WellStatus = 'sai' or w.WellStatus = 'sii' or w.WellStatus = 'a' or w.WellStatus = 'i' or w.WellStatus = 'pa') then 'Total Wells Drilled' end)
               ) as cat(WellCategory)
where cat.WellCategory is not null
group by c.LeaseType
        ,cat.WellCategory
order by c.LeaseType
        ,Wells desc;

输出

LeaseType WellCategory Wells
F Total Wells Drilled 183
F Total Non-Plugged Wells 123
F Total Wells Capable of Production 108
F Plugged and Abandoned Wells 60
F Producing Gas Wells 59
F Shut-In Gas Wells 19
F Producing Oil Wells 18
F Inactive Water Injection Wells 11
F Shut-In Oil Wells 10
F Permits Approved - Not Yet Commenced 4
F Total Holes Not Yet Completed 2
F Drilling Commenced - Not Yet Completed 1
F Open Orphan Wells (no known operator) 1
F Temporarily-Abandoned Wells 1
F Active Water Injection Wells 1
F Drilling Operations Suspended 1
I Total Wells Drilled 67
I Total Non-Plugged Wells 52
I Total Wells Capable of Production 38
I Producing Oil Wells 25
I Plugged and Abandoned Wells 15
I Shut-In Oil Wells 10
I Active Water Injection Wells 5
I Inactive Water Injection Wells 5
I Total Holes Not Yet Completed 4
I Drilling Operations Suspended 3
I Permits Approved - Not Yet Commenced 2
I Shut-In Gas Wells 2
I New Permits - Not Yet Approved 1
I Producing Gas Wells 1
I Drilling Commenced - Not Yet Completed 1
IP Permits Approved - Not Yet Commenced 2
P Total Wells Drilled 38
P Total Non-Plugged Wells 19
P Plugged and Abandoned Wells 19
P Total Wells Capable of Production 17
P Producing Oil Wells 10
P Producing Gas Wells 5
P Permits Approved - Not Yet Commenced 4
P Total Holes Not Yet Completed 2
P Drilling Commenced - Not Yet Completed 2
P Shut-In Gas Wells 1
P Shut-In Oil Wells 1
S Total Wells Drilled 57
S Total Non-Plugged Wells 38
S Total Wells Capable of Production 35
S Producing Gas Wells 25
S Plugged and Abandoned Wells 19
S Producing Oil Wells 6
S Shut-In Gas Wells 4
S Permits Approved - Not Yet Commenced 2
S Inactive Water Injection Wells 2
S Active Water Disposal Wells 1

这里有一种方法可以做到这一点,同时又能使它易于阅读:

我将 CASE 语句分解为符合逻辑的片段。不重叠但看起来相关的部分进入它们自己的查询以确定类别。然后我UNION ALL将它们放在一起以避免重复数据删除。

有很多方法可以做到这一点,这只是其中之一...

此外,就像我在聊天会话中提到的那样,如果可以的话,我强烈建议您对这些数据进行规范化、创建查找 tables 等。

WITH cte_Wells AS (
    SELECT w.WellStatus, w.WellType, c.LateralStatus, c.LeaseType, w.Operator
    FROM dbo.HWell w
        LEFT JOIN dbo.HConstruct c ON c.WellKey = w.PKey
)
SELECT
    dt.WellCategory,
    ISNULL(SUM(CASE WHEN dt.LeaseType IN ('F','I','S','P') OR dt.LeaseType NOT IN ('F', 'I', 'S', 'P', 'U') THEN 1 END), 0) AS [Number of Wells], -- Federal + Indian + State + Fee + Multi-Lease
    ISNULL(SUM(CASE WHEN dt.LeaseType = 'F' THEN 1 END), 0) AS [Federal], -- Sums up how many wells from the subquery have a leasetype of "Federal"
    ISNULL(SUM(CASE WHEN dt.LeaseType = 'I' THEN 1 END), 0) AS [Indian],
    ISNULL(SUM(CASE WHEN dt.LeaseType = 'S' THEN 1 END), 0) AS [State],
    ISNULL(SUM(CASE WHEN dt.LeaseType = 'P' THEN 1 END), 0) AS [Fee (Private)],
    ISNULL(SUM(CASE WHEN dt.LeaseType NOT IN ('F', 'I', 'S', 'P', 'U') THEN 1 END), 0) AS [Multiple Lease Types]
FROM (
    SELECT w.LeaseType, x.WellCategory
    FROM cte_Wells w
        CROSS APPLY (
            SELECT WellCategory = CASE 
                                    WHEN w.WellStatus = 'p' AND w.WellType = 'gw' OR (w.WellStatus IN ('pai','pii') AND w.WellType IN ('gwi','ggi','gwd')) THEN 'Producing Gas Wells'
                                    WHEN w.WellStatus = 'p' AND w.WellType = 'ow' OR (w.WellStatus IN ('pai','pii') AND w.WellType IN ('owi','ogi','owd')) THEN 'Producing Oil Wells'
                                    WHEN w.WellStatus = 's' AND w.WellType = 'gw' OR (w.WellStatus IN ('sai','sii') AND w.WellType IN ('gwi','ggi','gwd')) THEN 'Shut-In Gas Wells'
                                    WHEN w.WellStatus = 's' AND w.WellType = 'ow' OR (w.WellStatus IN ('sai','sii') AND w.WellType IN ('owi','ogi','owd')) THEN 'Shut-In Oil Wells'
                                    WHEN w.WellStatus = 'a' AND w.WellType = 'wi' THEN 'Active Water Injection Wells'
                                    WHEN w.WellStatus = 'a' AND w.WellType = 'gi' THEN 'Active Gas Injection Wells'
                                    WHEN w.WellStatus = 'a' AND w.WellType = 'wd' THEN 'Active Water Disposal Wells'
                                    WHEN w.WellStatus = 'a' AND w.WellType = 'gs' THEN 'Active Gas Storage Wells'
                                    WHEN w.WellStatus = 'a' AND w.WellType = 'ws' THEN 'Active Water Source Wells'
                                    WHEN w.WellStatus = 'a' AND w.WellType = 'tw' THEN 'Active Test Holes'
                                    WHEN w.WellStatus = 'i' AND w.WellType = 'wi' THEN 'Inactive Water Injection Wells'
                                    WHEN w.WellStatus = 'i' AND w.WellType = 'gi' THEN 'Inactive Gas Injection Wells'
                                    WHEN w.WellStatus = 'i' AND w.WellType = 'wd' THEN 'Inactive Water Disposal Wells'
                                    WHEN w.WellStatus = 'i' AND w.WellType = 'gs' THEN 'Inactive Gas Storage Wells'
                                    WHEN w.WellStatus = 'i' AND w.WellType = 'ws' THEN 'Inactive Water Source Wells'
                                    WHEN w.WellStatus = 'i' AND w.WellType = 'tw' THEN 'Inactive Test Holes'
                                    WHEN w.WellStatus = 'ta' THEN 'Temporarily-Abandoned Wells'
                                    WHEN w.WellStatus = 'pa' THEN 'Plugged and Abandoned Wells'
                                    ELSE NULL
                                END
        ) x
    WHERE x.WellCategory IS NOT NULL
    -----------------
    UNION ALL
    -----------------
    SELECT w.LeaseType, x.WellCategory
    FROM cte_Wells w
        CROSS APPLY (
            SELECT WellCategory = CASE 
                                    WHEN w.LateralStatus = 'NEW' THEN 'New Permits - Not Yet Approved'
                                    WHEN w.LateralStatus = 'APD' THEN 'Permits Approved - Not Yet Commenced'
                                    WHEN w.LateralStatus = 'DRL' THEN 'Drilling Commenced - Not Yet Completed'
                                    WHEN w.LateralStatus = 'OPS' THEN 'Drilling Operations Suspended'
                                    ELSE NULL
                                END
        ) x
    WHERE x.WellCategory IS NOT NULL
    -----------------
    UNION ALL
    -----------------
    SELECT w.LeaseType, WellCategory = 'Open Orphan Wells (no known operator)'
    FROM cte_Wells w
    WHERE w.WellStatus IN ('drl','ops','p','s','ta','pai','pii','sai','sii','a','i') AND w.Operator = 101600
    -----------------
    UNION ALL
    -----------------
    SELECT w.LeaseType, WellCategory = 'Total Holes Not Yet Completed'
    FROM cte_Wells w
    WHERE w.WellStatus IN ('drl','ops')
    -----------------
    UNION ALL
    -----------------
    SELECT w.LeaseType, WellCategory = 'Total Wells Capable of Production'
    FROM cte_Wells w
    WHERE (w.WellStatus IN ('p','s') AND w.WellType <> 'LI') OR w.WellStatus IN ('pai','pii','sai','sii')
    -----------------
    UNION ALL
    -----------------
    SELECT w.LeaseType, WellCategory = 'Total Non-Plugged Wells'
    FROM cte_Wells w
    WHERE (w.WellStatus IN ('p','s') AND w.WellType <> 'LI') OR w.WellStatus IN ('drl','ops','ta','pai','pii','sai','sii','a','i')
    -----------------
    UNION ALL
    -----------------
    SELECT w.LeaseType, WellCategory = 'Total Wells Drilled'
    FROM cte_Wells w
    WHERE (w.WellStatus IN ('p','s') AND w.WellType <> 'LI') or w.WellStatus IN ('drl','ops','ta','pai','pii','sai','sii','a','i','pa')
) dt
GROUP BY dt.WellCategory
ORDER BY dt.WellCategory DESC

我已经根据您的示例数据和建议的预期结果检查了此查询的结果,它们似乎匹配。所以这会让你得到你想要的。但是,由于我不知道数据,您需要检查它是否存在逻辑错误和性能。

注意:此查询的结果不会生成空类别的计数。如果那是您需要的东西,请对此解决方案发表评论,我可以解决它。解决方法是添加一个包含所有类别的 table,然后 LEFT JOIN 到计数结果。