【问题标题】:How do I count subquery CASE WHEN categories with an outer query?如何计算带有外部查询的子查询 CASE WHEN 类别?
【发布时间】:2022-01-02 00:36:16
【问题描述】:

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

我当前的查询部分有效,但只能正确计算CASE WHEN 子句层次结构中较高的井。这是因为第一个CASE WHEN 有机会将井类别分配给数据集中的所有井。但是,当它遍历每个CASE WHEN 子句时,查询“see's”的井数减少,因为它用完了可以分配类别的井。到最后,几乎所有的井都已经分配了一个类别,从而根本无法进行某些类别计数。

这是我当前的查询,也在上面的 dbfiddle 链接中:

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

这是上面的查询产生的表格:

表 #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

这是我知道的同一数据集可以生成的表以及我要复制的表:

表 #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

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

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

【问题讨论】:

  • 你可能说过,我错过了。但是,一口井有可能存在于多个类别中吗?
  • @ChadBaldwin 是的,一口井可以同时出现在多个类别中,但只能计算一次/类别。
  • 啊,好吧,这就解释了这个问题。您需要计算多个井可能存在的类别,但 CASE 语句将只返回第一个命中的类别。因此,您需要一个能够按类别生成重复项的解决方案。我会看看我能想出什么。
  • @ChadBaldwin 我想是这样......一口井可以满足几个 CASE 语句的标准。但是,在我的查询中,一旦满足第一个 case 语句,就会分配井类别并且不能将其分配给另一个类别。一旦到最后一个 CASE 语句,就几乎没有任何井可以分配了。
  • 是的,我想我明白了。因此,如果您有 1 口井,即“生产气井”和“已批准的许可证”,那么您希望将其计入这两个类别。但由于 CASE 语句的工作方式……它只会计入“生产气井”

标签: sql sql-server count sql-server-2012 subquery


【解决方案1】:

这里有一种方法可以让你在保持它易于阅读的同时做到这一点:

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

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

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

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

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

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

【讨论】:

  • 这基本上是一种更冗长的方式来做我的回答正在做的事情,几乎可以肯定执行起来不会那么有效。
  • @iamdave 你的回答没有产生预期的结果
  • @iamdave 除了抨击别人试图提供帮助之外,我也不确定您的评论还有什么意义。如果它不起作用或效率较低,则由 OP(和社区)来测试他们收到的不同答案,确定最适合他们的答案,然后投票选出他们认为是最佳解决方案的答案他们的问题。
  • 您的答案没有产生预期的结果 - 不完全是,但 SO 不是脚本编写服务,我已经解释了为什么我没有包含pivot。除了 pivotrollup(我认为应该将其留在 SQL 脚本之外)之外,我的答案是生成所需的数据。
  • @iamdave 您的回答不需要pivotrollup。您所要做的就是将它放在子查询中并添加它们的外部 GROUP BYSUM 操作,您将产生他们要求的确切结果......即使您的答案更有效,我的由于更完整,可能会被选为答案。做出改变,然后你是对的,它会更有效,并且会产生预期的结果(减去空类别,我的也是这样)。
【解决方案2】:

您可以执行此操作的一种方法是为每个条件生成一行,然后汇总返回匹配项的那些,这可以使用cross applyvalues 表生成器来确保您的所有@ 987654324@ 表达式对所有孔求值。

目前这种方法假设每个孔只有一个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

【讨论】:

    猜你喜欢
    • 2017-07-07
    • 1970-01-01
    • 2022-12-15
    • 2017-05-30
    • 2018-11-15
    • 1970-01-01
    • 2016-02-23
    • 2011-04-17
    • 2021-12-22
    相关资源
    最近更新 更多