1. <small id='CeBtJ'></small><noframes id='CeBtJ'>

      <tfoot id='CeBtJ'></tfoot>
      <i id='CeBtJ'><tr id='CeBtJ'><dt id='CeBtJ'><q id='CeBtJ'><span id='CeBtJ'><b id='CeBtJ'><form id='CeBtJ'><ins id='CeBtJ'></ins><ul id='CeBtJ'></ul><sub id='CeBtJ'></sub></form><legend id='CeBtJ'></legend><bdo id='CeBtJ'><pre id='CeBtJ'><center id='CeBtJ'></center></pre></bdo></b><th id='CeBtJ'></th></span></q></dt></tr></i><div id='CeBtJ'><tfoot id='CeBtJ'></tfoot><dl id='CeBtJ'><fieldset id='CeBtJ'></fieldset></dl></div>
      • <bdo id='CeBtJ'></bdo><ul id='CeBtJ'></ul>

        <legend id='CeBtJ'><style id='CeBtJ'><dir id='CeBtJ'><q id='CeBtJ'></q></dir></style></legend>

        在 Oracle 行的多列上使用数据透视表

        时间:2023-09-19
        <tfoot id='apx5K'></tfoot>

        <i id='apx5K'><tr id='apx5K'><dt id='apx5K'><q id='apx5K'><span id='apx5K'><b id='apx5K'><form id='apx5K'><ins id='apx5K'></ins><ul id='apx5K'></ul><sub id='apx5K'></sub></form><legend id='apx5K'></legend><bdo id='apx5K'><pre id='apx5K'><center id='apx5K'></center></pre></bdo></b><th id='apx5K'></th></span></q></dt></tr></i><div id='apx5K'><tfoot id='apx5K'></tfoot><dl id='apx5K'><fieldset id='apx5K'></fieldset></dl></div>
        <legend id='apx5K'><style id='apx5K'><dir id='apx5K'><q id='apx5K'></q></dir></style></legend>
          <tbody id='apx5K'></tbody>
            <bdo id='apx5K'></bdo><ul id='apx5K'></ul>

              • <small id='apx5K'></small><noframes id='apx5K'>

                  本文介绍了在 Oracle 行的多列上使用数据透视表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 Oracle 表 (tab1) 中有以下示例数据,我正在尝试将行转换为列.我知道如何在一列上使用 Oracle 数据透视表.但是是否可以将其应用于多列?

                  I have the following sample data in an Oracle table (tab1) and I am trying to convert rows to columns. I know how to use Oracle pivot on one column. But is it possible to apply it to multiple columns?

                  样本数据:

                  Type  weight  height  
                  A     50      10  
                  A     60      12  
                  B     40      8  
                  C     30      15  
                  

                  我的预期输出:

                  A-count B-count C-count A-weight B-weight C-weight A-height B-height C-height  
                  2       1       1       110      40       30       22       8        15  
                  

                  我能做什么:

                  with T AS 
                  (select type, weight from tab1 )
                  select * from T
                  PIVOT (
                  count(type)
                  for type in (A, B, C, D,E,F)
                  )  
                  

                  上面的查询给了我下面的结果

                  The above query gives me the below result

                  A B C  
                  2 1 1  
                  

                  我可以用 sum(weight)sum(height) 替换 count(*) 以旋转高度或重量.我想做但我不能做的是在一个查询中以所有三个(计数、重量和高度)为中心.

                  I can replace count(*) with sum(weight) or sum(height) to pivot height or weight. What I am looking to do, but I can't do, is pivot on all three (count, weight and height) in one query.

                  可以使用pivot来完成吗?

                  Can it be done using pivot?

                  推荐答案

                  As 文档显示,您可以有多个聚合函数子句.所以你可以这样做:

                  As the documentation shows, you can have multiple aggregate function clauses. So you can do this:

                  select * from (
                    select * from tab1
                  )
                  pivot (
                    count(type) as ct, sum(weight) as wt, sum(height) as ht
                    for type in ('A' as A, 'B' as B, 'C' as C)
                  );
                  
                  A_CT A_WT A_HT B_CT B_WT B_HT C_CT C_WT C_HT
                  ---- ---- ---- ---- ---- ---- ---- ---- ----
                     2  110   22    1   40    8    1   30   15 
                  

                  如果您希望列按您显示的顺序排列,请添加另一个级别的子查询:

                  If you want the columns in the order you showed then add another level of subquery:

                  select a_ct, b_ct, c_ct, a_wt, b_wt, c_wt, a_ht, b_ht, c_ht
                  from (
                    select * from (
                      select * from tab1
                    )
                    pivot (
                      count(type) as ct, sum(weight) as wt, sum(height) as ht
                      for type in ('A' as A, 'B' as B, 'C' as C)
                    )
                  );
                  
                  A_CT B_CT C_CT A_WT B_WT C_WT A_HT B_HT C_HT
                  ---- ---- ---- ---- ---- ---- ---- ---- ----
                     2    1    1  110   40   30   22    8   15 
                  

                  SQL 小提琴.

                  这篇关于在 Oracle 行的多列上使用数据透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:检查当前日期是否在两个日期之间 Oracle SQL 下一篇:SQL Server 中的可延迟约束

                  相关文章

                1. <small id='hPrJ4'></small><noframes id='hPrJ4'>

                2. <tfoot id='hPrJ4'></tfoot>

                  1. <i id='hPrJ4'><tr id='hPrJ4'><dt id='hPrJ4'><q id='hPrJ4'><span id='hPrJ4'><b id='hPrJ4'><form id='hPrJ4'><ins id='hPrJ4'></ins><ul id='hPrJ4'></ul><sub id='hPrJ4'></sub></form><legend id='hPrJ4'></legend><bdo id='hPrJ4'><pre id='hPrJ4'><center id='hPrJ4'></center></pre></bdo></b><th id='hPrJ4'></th></span></q></dt></tr></i><div id='hPrJ4'><tfoot id='hPrJ4'></tfoot><dl id='hPrJ4'><fieldset id='hPrJ4'></fieldset></dl></div>
                    1. <legend id='hPrJ4'><style id='hPrJ4'><dir id='hPrJ4'><q id='hPrJ4'></q></dir></style></legend>
                        <bdo id='hPrJ4'></bdo><ul id='hPrJ4'></ul>