汇总表水平和垂直枢轴plsql

时间:2019-07-06 15:34:09

标签: sql oracle oracle11g

执行枢轴操作后,是否可以在水平和垂直方向上对表格进行总计?

我已经阅读并寻找了汇总功能,但是缺点是列是动态的

#include <stdio.h>

int main() 
{
  char a[20];

  if (fgets(a, sizeof(a), stdin) != NULL) {
    for (size_t j = 0; a[j] != 0; ++j) {
      char c = a[j];

      if (c != '\n') {
        size_t count = 1;

        for (size_t i = j + 1; a[i] != 0; i++) {
          if (a[j] == a[i]) {
            count += 1;
            a[i] = '\n'; /* to not count it again */
          }
        }

        printf("%c : %zu\n", a[j], count);
      }
    }
  }

  return 0;
}

结果

pi@raspberrypi:/tmp $ g++ -pedantic -Wall -Wextra c.c
pi@raspberrypi:/tmp $ ./a.out
tree
t : 1
r : 1
e : 2
pi@raspberrypi:/tmp $ ./a.outpi@
output input
o : 1
u : 3
t : 3
p : 2
  : 1
i : 1
n : 1
pi@raspberrypi:/tmp $ 

我要使用的数据结构如下,以利用枢轴,但是我们必须考虑到列是动态的,并且要使用枢轴(我使用LISTAGG)来为枢轴构建一个字符串

PRODUCT | VALUE
:------ | ----:
Shirts  |  1200
tax     |    15
Stocks  |   500
tax     |    20

1 个答案:

答案 0 :(得分:0)

使用条件聚合。您可以通过以下方式获得想要的东西:

select sum(case when product = 'Shirts' then value end) as shirts,
       sum(case when product = 'tax' then value end) as tax_1,
       sum(case when product = 'Stock' then value end) as stock,
       sum(case when product = 'tax' then value end) as tax_2 ,
       sum(value) as total     
from t;

我不理解您的数据格式,其中tax被计算两次,因此我没有包括在内。当您已经将某行中的内容总计时,我也不明白为什么要在总计中单独显示一行。

相关问题