简体   繁体   English

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

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

I have a sample set of data in this db<>fiddle .我在这个db<>fiddle中有一组样本数据。 The data represents a batch of wells that fall into different well-type categories based on some criteria.数据代表一批根据某些标准属于不同井型类别的井。 I am trying to group the wells by the category they fall into and then count how many wells are in each category based on another set of criteria.我正在尝试按井所属的类别对井进行分组,然后根据另一组标准计算每个类别中有多少井。

My current query partially works but only correctly counts wells that are higher in the CASE WHEN clause hierarchy.我当前的查询部分有效,但仅正确计算CASE WHEN子句层次结构中较高的井。 This is because the first CASE WHEN has the chance to assign well categories to all of the wells in the data set.这是因为第一个CASE WHEN有机会将井类别分配给数据集中的所有井。 However, as it goes through each CASE WHEN clause, the query "see's" fewer wells because it runs out of wells it can assign a category to.但是,当它遍历每个CASE WHEN子句时,查询“see's”的井数较少,因为它用完了可以为其分配类别的井。 By the time it reaches the end, almost all of the wells have already had a category assigned to them, preventing some category counts from occurring at all.到最后,几乎所有的井都已经分配了一个类别,从而根本无法进行某些类别计数。

Here is the current query I have, which is also in the db<>fiddle link above:这是我当前的查询,也在上面的 db<>fiddle 链接中:

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

Here is the table the query above produces:这是上面的查询生成的表:

Table #1表格1

WellCategory井类别 Number of Wells井数 Federal联邦 Indian印度人 State State Fee (Private)费用(私人) Multiple Lease Types多种租赁类型
Total Wells Capable of Production可生产井总数 2 2 2 2 0 0 0 0 0 0 0 0
Temporarily-Abandoned Wells暂时废弃的水井 1 1 1 1 0 0 0 0 0 0 0 0
Shut-In Oil Wells关闭油井 21 21 10 10 10 10 0 0 1 1 0 0
Shut-In Gas Wells关闭气井 26 26 19 19 2 2 4 4 1 1 0 0
Producing Oil Wells生产油井 59 59 18 18 25 25 6 6 10 10 0 0
Producing Gas Wells生产气井 90 90 59 59 1 1 25 25 5 5 0 0
Plugged and Abandoned Wells堵塞和废弃的井 113 113 60 60 15 15 19 19 19 19 0 0
Permits Approved - Not Yet Commenced许可证已获批准 - 尚未开始 14 14 4 4 2 2 2 2 4 4 2 2
New Permits - Not Yet Approved新许可证 - 尚未批准 1 1 0 0 1 1 0 0 0 0 0 0
Inactive Water Injection Wells非活性注水井 18 18 11 11 5 5 2 2 0 0 0 0
Drilling Operations Suspended钻井作业暂停 4 4 1 1 3 3 0 0 0 0 0 0
Drilling Commenced - Not Yet Completed钻探开始 - 尚未完成 4 4 1 1 1 1 0 0 2 2 0 0
Active Water Injection Wells活性注水井 6 6 1 1 5 5 0 0 0 0 0 0
Active Water Disposal Wells活性水处理井 1 1 0 0 0 0 1 1 0 0 0 0
NULL NULL 140 140 83 83 28 28 14 14 15 15 0 0

Here is the table I know the same data set can produce and the one I want to replicate:这是我知道同一数据集可以生成的表和我要复制的表:

Table #2表#2

Well Statuses井状态 Number Of Wells井数 Federal联邦 Indian印度人 State State Fee (Private)费用(私人) Multiple Lease Types多种租赁类型
Producing Oil Wells生产油井 59 59 18 18 25 25 6 6 10 10 0 0
Producing Gas Wells生产气井 90 90 59 59 1 1 25 25 5 5 0 0
Shut-In Oil Wells关闭油井 21 21 10 10 10 10 0 0 1 1 0 0
Shut-In Gas Wells关闭气井 26 26 19 19 2 2 4 4 1 1 0 0
Active Water Injection Wells活性注水井 6 6 1 1 5 5 0 0 0 0 0 0
Active Gas Injection Wells主动注气井 0 0 0 0 0 0 0 0 0 0 0 0
Active Water Disposal Wells活性水处理井 1 1 0 0 0 0 1 1 0 0 0 0
Active Gas Storage Wells活性储气井 0 0 0 0 0 0 0 0 0 0 0 0
Active Water Source Wells活性水源井 0 0 0 0 0 0 0 0 0 0 0 0
Active Test Holes主动测试孔 0 0 0 0 0 0 0 0 0 0 0 0
Inactive Water Injection Wells非活性注水井 18 18 11 11 5 5 2 2 0 0 0 0
Inactive Gas Injection Wells惰性注气井 0 0 0 0 0 0 0 0 0 0 0 0
Inactive Water Disposal Wells非活性水处理井 0 0 0 0 0 0 0 0 0 0 0 0
Inactive Gas Storage Wells非活性储气井 0 0 0 0 0 0 0 0 0 0 0 0
Inactive Water Source Wells非活动水源井 0 0 0 0 0 0 0 0 0 0 0 0
Inactive Test Holes非活动测试孔 0 0 0 0 0 0 0 0 0 0 0 0
Temporarily-Abandoned Wells暂时废弃的水井 1 1 1 1 0 0 0 0 0 0 0 0
Plugged and Abandoned Wells堵塞和废弃的井 113 113 60 60 15 15 19 19 19 19 0 0
New Permits - Not Yet Approved新许可证 - 尚未批准 1 1 0 0 1 1 0 0 0 0 0 0
Permits Approved - Not Yet Commenced许可证已获批准 - 尚未开始 14 14 4 4 2 2 2 2 4 4 2 2
Drilling Commenced - Not Yet Completed钻探开始 - 尚未完成 4 4 1 1 1 1 0 0 2 2 0 0
Drilling Operations Suspended钻井作业暂停 4 4 1 1 3 3 0 0 0 0 0 0
Open Orphan Wells (no known operator) Open Orphan Wells(无已知运营商) 1 1 1 1 0 0 0 0 0 0 0 0
Total Holes Not Yet Completed尚未完成的孔总数 8 8 2 2 4 4 0 0 2 2 0 0
Total Wells Capable of Production可生产井总数 198 198 108 108 38 38 35 35 17 17 0 0
Total Non-Plugged Wells未堵塞井总数 232 232 123 123 52 52 38 38 19 19 0 0
Total Wells Drilled钻井总数 345 345 183 183 67 67 57 57 38 38 0 0

Table #2 is generated using a much longer query that uses several subqueries and temp tables to count how many wells belong to each category using their DISTINCT w.WellID .表 #2 是使用更长的查询生成的,该查询使用几个子查询和临时表来使用它们的DISTINCT w.WellID计算属于每个类别的井的数量。 In addition, the full data set has a lot more entries, usually each category has at least some wells in it and not so many "0's".此外,完整的数据集有更多的条目,通常每个类别至少有一些井,而不是那么多的“0”。

I don't know how to tell the query to count wells more than once if they fall into several well categories without making the query super long and start introducing temp tables, which I would prefer not to do.我不知道如何告诉查询多次计算井,如果它们属于几个井类别而不使查询超长并开始引入临时表,我不希望这样做。 *NOTE I do not want to count a well twice for same category, only count it once per category. *注意我不想为同一类别计算两次,每个类别只计算一次。

Here's one way you could do it while keeping it somewhat easy to read:这是您可以在保持其易于阅读的同时做到这一点的一种方法:

I broke the CASE statement into logical pieces.我将CASE语句分解为合乎逻辑的部分。 Parts that don't overlap but seem related go into their own query to determine the category.不重叠但似乎与 go 相关的部分进入他们自己的查询以确定类别。 Then I UNION ALL them together to avoid de-duping.然后我将它们UNION ALL在一起以避免重复数据删除。

There's tons of ways this could be done, and this is just one of them...有很多方法可以做到这一点,这只是其中之一......

Also, like I mentioned in the chat session, if you are able to, I would highly recommend working on normalizing this data, creating lookup tables, etc.另外,就像我在聊天 session 中提到的那样,如果可以的话,我强烈建议您对这些数据进行规范化,创建查找表等。

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

I've checked the results of this query against your sample data and proposed expected results, and they appear to match.我已根据您的示例数据和建议的预期结果检查了此查询的结果,它们似乎匹配。 So this will get you what you want.所以这会给你你想要的。 However, since I don't know the data, you'll need to inspect it for logic errors, and performance.但是,由于我不知道数据,您需要检查它的逻辑错误和性能。

NOTE: The results of this query do not produce counts for empty categories.注意:此查询的结果不会产生空类别的计数。 If that is something you need, then comment on this solution and I can fix it.如果这是您需要的,请对此解决方案发表评论,我可以修复它。 The fix is to add a table with all categories, then LEFT JOIN to that the results of the counts.解决方法是添加一个包含所有类别的表,然后 LEFT JOIN 到该计数结果。

One way in which you could do this would be to generate a row for each criteria and then aggregate up those that return a match, which can be done using cross apply and a values table generator that ensures all your case expressions are evaluated for all wells.您可以执行此操作的一种方法是为每个条件生成一行,然后汇总返回匹配项的那些,这可以使用cross applyvalues表生成器来完成,该生成器可确保针对所有井评估所有case表达式.

This approach at present assumes that each well only has the one WellCategory value.目前这种方法假设每个井只有一个WellCategory值。 If there can be more than one then you will need to make those changes yourself.如果可以有多个,那么您将需要自己进行这些更改。 I also haven't wrapped the output in a pivot as I think this is generally something best done in your presentation layer rather than the raw SQL.我也没有将 output 包装在pivot中,因为我认为这通常是在您的表示层而不是原始 SQL 中完成的最佳方法。

I suggest keeping the SQL output in this format as presentation tools are much better at dynamically handling new categories (eg a new LeaseType is added) than SQL is, which in this normalised format wouldn't require a change in either the script or the presentation layer, barring any bespoke labels.我建议将 SQL output 保留在这种格式中,因为演示工具在动态处理新类别(例如添加新的LeaseType )方面比 Z9778840A0100CB30C982876741B0B5A2 需要更改脚本格式的演示文稿或正常演示文稿't需要更改层,除非有任何定制标签。

If you want to retain the pivoted output, you can simply wrap this query in your current outer select :如果你想保留旋转的 output,你可以简单地将这个查询包装在你当前的外部select

Query询问

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;

Output Output

LeaseType租赁类型 WellCategory井类别 Wells威尔斯
F F Total Wells Drilled钻井总数 183 183
F F Total Non-Plugged Wells未堵塞井总数 123 123
F F Total Wells Capable of Production可生产井总数 108 108
F F Plugged and Abandoned Wells堵塞和废弃的井 60 60
F F Producing Gas Wells生产气井 59 59
F F Shut-In Gas Wells关闭气井 19 19
F F Producing Oil Wells生产油井 18 18
F F Inactive Water Injection Wells非活性注水井 11 11
F F Shut-In Oil Wells关闭油井 10 10
F F Permits Approved - Not Yet Commenced许可证已获批准 - 尚未开始 4 4
F F Total Holes Not Yet Completed尚未完成的孔总数 2 2
F F Drilling Commenced - Not Yet Completed钻探开始 - 尚未完成 1 1
F F Open Orphan Wells (no known operator) Open Orphan Wells(无已知运营商) 1 1
F F Temporarily-Abandoned Wells暂时废弃的水井 1 1
F F Active Water Injection Wells活性注水井 1 1
F F Drilling Operations Suspended钻井作业暂停 1 1
I Total Wells Drilled钻井总数 67 67
I Total Non-Plugged Wells未堵塞井总数 52 52
I Total Wells Capable of Production可生产井总数 38 38
I Producing Oil Wells生产油井 25 25
I Plugged and Abandoned Wells堵塞和废弃的井 15 15
I Shut-In Oil Wells关闭油井 10 10
I Active Water Injection Wells活性注水井 5 5
I Inactive Water Injection Wells非活性注水井 5 5
I Total Holes Not Yet Completed尚未完成的孔总数 4 4
I Drilling Operations Suspended钻井作业暂停 3 3
I Permits Approved - Not Yet Commenced许可证已获批准 - 尚未开始 2 2
I Shut-In Gas Wells关闭气井 2 2
I New Permits - Not Yet Approved新许可证 - 尚未批准 1 1
I Producing Gas Wells生产气井 1 1
I Drilling Commenced - Not Yet Completed钻探开始 - 尚未完成 1 1
IP IP Permits Approved - Not Yet Commenced许可证已获批准 - 尚未开始 2 2
P Total Wells Drilled钻井总数 38 38
P Total Non-Plugged Wells未堵塞井总数 19 19
P Plugged and Abandoned Wells堵塞和废弃的井 19 19
P Total Wells Capable of Production可生产井总数 17 17
P Producing Oil Wells生产油井 10 10
P Producing Gas Wells生产气井 5 5
P Permits Approved - Not Yet Commenced许可证已获批准 - 尚未开始 4 4
P Total Holes Not Yet Completed尚未完成的孔总数 2 2
P Drilling Commenced - Not Yet Completed钻探开始 - 尚未完成 2 2
P Shut-In Gas Wells关闭气井 1 1
P Shut-In Oil Wells关闭油井 1 1
S小号 Total Wells Drilled钻井总数 57 57
S小号 Total Non-Plugged Wells未堵塞井总数 38 38
S小号 Total Wells Capable of Production可生产井总数 35 35
S小号 Producing Gas Wells生产气井 25 25
S小号 Plugged and Abandoned Wells堵塞和废弃的井 19 19
S小号 Producing Oil Wells生产油井 6 6
S小号 Shut-In Gas Wells关闭气井 4 4
S小号 Permits Approved - Not Yet Commenced许可证已获批准 - 尚未开始 2 2
S小号 Inactive Water Injection Wells非活性注水井 2 2
S小号 Active Water Disposal Wells活性水处理井 1 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM