【问题标题】:ORACLE - How to display 3 different values from one columnORACLE - 如何从一列中显示 3 个不同的值
【发布时间】:2021-09-18 06:16:13
【问题描述】:

我有一个包含 4 种类型的表和一个“值”列,我需要在单个查询中为每个 id 显示 3 列。 类型 1= abc,类型 2= def,类型 3= ghi,类型 4= jkl 我当前的查询,现在在单个列中显示所有值,我需要每个 typeid 一个列,将是 4 个具有不同名称的列

SELECT 
 OBJECTID
,TRUNC(DATETIME + 1 / (24 * 12), 'HH24') DATETIME
,TIMEZONE
,VALUE as value
,LOADID LOADID
FROM PROD.TS_WSI_HOURLY_OBSERVATION
where typeid in(1,2,3,4);
id value
1 50
2 30
3 20
4 85

我需要如何显示这种结果:

abc def ghi jkl
50 30 20 85.
15 10 4 34.
34 30 12 8598

知道怎么做吗?我需要所有的行,而不是像 max 这样的聚合

问候

【问题讨论】:

    标签: oracle


    【解决方案1】:

    这就是我们通常的做法:

    SQL> with temp (gbc, id, value) as
      2    -- sample data
      3    (select 'a', 1, 50 from dual union all
      4     select 'b', 1, 15 from dual union all
      5     select 'c', 1, 34 from dual union all
      6     --
      7     select 'a', 2, 30 from dual union all
      8     select 'b', 2, 10 from dual union all
      9     select 'c', 2, 30 from dual union all
     10     --
     11     select 'a', 3, 20 from dual union all
     12     select 'b', 3,  4 from dual union all
     13     select 'c', 3, 12 from dual
     14    )
     15  select gbc,
     16         sum(case when id = 1 then value end) as abc,
     17         sum(case when id = 2 then value end) as def,
     18         sum(case when id = 3 then value end) as ghi,
     19         sum(case when id = 4 then value end) as jkl
     20  from temp
     21  group by gbc;
    
    GBC        ABC        DEF        GHI        JKL
    --- ---------- ---------- ---------- ----------
    a           50         30         20
    b           15         10          4
    c           34         30         12
    
    SQL>
    

    或者,如果我理解您的查询,您只需将其用作 CTE 并提取所需的值:

    with temp as
      (select 
          typeid,
          objectid
         ,trunc(datetime + 1 / (24 * 12), 'HH24') datetime
         ,timezone
         ,value as value
         ,loadid loadid
       from prod.ts_wsi_hourly_observation
       where typeid in(1,2,3,4)
      )
    select sum(case when typeid = 1 then value end) as abc,  
           sum(case when typeid = 2 then value end) as def,
           sum(case when typeid = 3 then value end) as ghi,
           sum(case when typeid = 4 then value end) as jkl
    from temp
    group by objectid; 
    

    【讨论】:

    • 嗨,我在这个表中有 300 条记录,如何在没有 sum 的情况下全部显示,因为如果我删除 sum 语句,每条记录只显示一个值,并且 3 列为空,我需要全部列填充
    • 没错。这就是我使用 SUM 的原因。
    • 但是总和只显示了一条记录,我需要显示300条记录,还有为什么在typeid时使用objectid?
    • 必须有其他内容(您可以在 SELECT 列列表中使用的另一列)不会被聚合,但将用于将结果“拆分”为单独的行。该列不必在 SELECT 中指定,但必须在 GROUP BY 子句中使用。为什么我使用 OBJECTID?因为我查看了您发布的查询,并且其 SELECT 列列表中没有 TYPEID。我发布的查询只是如何做到这一点的示例;调整它以适合您的需要(现在您知道如何操作了)。
    • 我编辑了之前发布的代码,使其包含 GBC 列(“GroupByColumn”的缩写)。带有样本数据的查询在 SELECT 和 GROUP BY 中都有。基于您的查询仅在 GROUP BY 中包含它,我假设它是 OBJECTID,您将按其对值进行分组。如果不是这样,请将其更改为其他列;我不知道是哪一个,但你应该知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2012-03-12
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    相关资源
    最近更新 更多