Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在ORACLE上按问题透视/分组_Sql_Oracle - Fatal编程技术网

Sql 在ORACLE上按问题透视/分组

Sql 在ORACLE上按问题透视/分组,sql,oracle,Sql,Oracle,我的第一个问题是我在哪里遇到的问题:现在肯定解决了 然而,我有一个新问题。我尝试对其进行转换,再次获得以下输出: | EMAIL | WIFI | ... - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Yes |

我的第一个问题是我在哪里遇到的问题:现在肯定解决了

然而,我有一个新问题。我尝试对其进行转换,再次获得以下输出:

| EMAIL | WIFI | ... - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Yes | 20 | 24 | ... - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - No | 4 | 0 | ... - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Unknown | 1 | 1 | ... - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 这里的数据可以帮助您构建这样的输出。我尝试再次使用unpivot/pivot来处理René在我引用的已解决问题中给我的查询,但不幸的是,我得到的错误是 ORA-56901:pivot | unpivot值SIGH不允许使用非常量表达式

with count_table as ( select 1001 device_id, 4 quantity from dual union all select 1002 device_id, 20 quantity from dual union all select 1003 device_id, 1 quantity from dual ), device_table as ( select 1001 id, 'Yes' wifi, 'No' email, 'No' bluetooth from dual union all select 1002 id, 'Yes' wifi, 'Yes' email, 'No' bluetooth from dual union all select 1003 id, 'Unknown' wifi, 'Unknown' email, 'Yes' bluetooth from dual )
也许有一个更简单的解决方案?我肯定需要读一本关于关系数据库的书:

如果输出表的列数是灵活的,您可能可以使用一些过程解决方案;PL/SQL或Java


在PL/SQL中,您可以创建二维集合并填充它,然后将其打印出来。当然,您可以使用dbms_SQL包创建/生成一个动态SQL查询。

参考您之前的文章后,它看起来非常简单。。 请尝试下面的查询

with 
count_table as (
     select 1001 device_id,  4 quantity from dual union all
     select 1002 device_id, 20 quantity from dual union all
     select 1003 device_id,  1 quantity from dual 
),
device_table as (
     select 1001 id, 'Yes'     wifi, 'No'       email, 'No'  bluetooth from dual union all
     select 1002 id, 'Yes'     wifi, 'Yes'      email, 'No'  bluetooth from dual union all
     select 1003 id, 'Unknown' wifi, 'Unknown'  email, 'Yes' bluetooth from dual 
)
----------------------------------------
select * from (
      select
        feature,
        yes_no_unknown,
        sum(quantity)  quantity
      from 
         count_table  c join 
         device_table d on c.device_id = d.id
      unpivot  ( yes_no_unknown
                 for feature in (wifi, email, bluetooth)
      ) 
      group by 
      feature,
      yes_no_unknown
)  
pivot ( sum (quantity)
        -- only this line I have changed  ..
        for feature in ('WIFI' as Wifi, 'EMAIL' as Email, 'BLUETOOTH' as Bluetooth)
);

非常感谢你!顺便说一句,这是一个输入错误,我开始更好地理解这个PIVOT/UNPIVOT功能;