Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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 如何对包含常量/缺少列的多个列使用unpivot?_Sql_Oracle_Unpivot - Fatal编程技术网

Sql 如何对包含常量/缺少列的多个列使用unpivot?

Sql 如何对包含常量/缺少列的多个列使用unpivot?,sql,oracle,unpivot,Sql,Oracle,Unpivot,我试图使用UNPIVOT,以避免多个工会的老式方式 这样做的理由是我正在收集一些数据实验室测试,结果,单位。。。我想以一种适当的方式展示,以供审阅 比如说,我的原始数据如下所示: ID, TESTA, RESULTA, UNITA, TESTB, RESULTB, UNITB, OTHER_UNITB 1, 'T1', 10, 1, 'T2', 2.5, , 'kg', 2, 'T1', 15, 1, 'T2', 1.5,

我试图使用UNPIVOT,以避免多个工会的老式方式

这样做的理由是我正在收集一些数据实验室测试,结果,单位。。。我想以一种适当的方式展示,以供审阅

比如说,我的原始数据如下所示:

ID, TESTA, RESULTA, UNITA, TESTB, RESULTB, UNITB, OTHER_UNITB
1,  'T1',  10,      1,     'T2',  2.5,          , 'kg',
2,  'T1',  15,      1,     'T2',  1.5,     1,         ,
3,  'T1',    ,       ,     'T2',  1,       1,
以下操作非常有效:对于每个ID,我使用相应的结果和单位检索每个测试的1行:

select  id,
        property as test_code,
        decode(property, 'T1', 'Test 01', 'T2', 'Test 02', 'Unknown test') as test_name,
        result,
        unit
from    my_table
unpivot include nulls
        (
            (result, unit)
            for property in (
              (resulta, unita) as 'T1',
              (resultb, unitb) as 'T2'
            )
        )
;
但是,当我试图检索特定于测试“T2”的“其他单元”时,出现了问题。请记住,这是一个示例,我有很多测试,将近20个

我试过这个:

select  id,
        property as test_code,
        decode(property, 'T1', 'Test 01', 'T2', 'Test 02', 'Unknown test') as test_name,
        result,
        unit,
        other_unit
from    my_table
unpivot include nulls
        (
            (result, unit, other_unit)
            for property in (
              (resulta, unita, null) as 'T1',
              (resultb, unitb, other_unitb) as 'T2'
            )
        )
;
它失败,因为我在unpivot中输入的“null”语句的消息标识符无效

我还尝试使用常量,方式如下:

....
unpivot include nulls
        (
            (result, unit, other_unit)
            for property in (
              (resulta, unita, 0) as 'T1',
              (resultb, unitb, other_unitb) as 'T2'
            )
        )
;
这也失败了

我被困在这里,无法在不重写的情况下解决这一问题。所有内容都是工会声明列表——我想不惜一切代价避免,因为维护起来相当复杂:

select resulta as result,
       unita as unit,
       null as other_unit
from   my_table
union
select resultb as result,
       unitb as unit,
       other_unitbas other_unit
from   my_table
union
...
我还发现了一个丑陋的解决方案:

select  id,
        property as test_code,
        decode(property, 'T1', 'Test 01', 'T2', 'Test 02', 'Unknown test') as test_name,
        result,
        unit,
        other_unit
from    (
            select  m.*,
                    null as null_item
            from    my_table m
        )

unpivot include nulls
        (
            (result, unit, other_unit)
            for property in (
              (resulta, unita, null_item) as 'T1',
              (resultb, unitb, other_unitb) as 'T2'
            )
        )
;
但老实说,我很惭愧这么做


提前感谢您的支持

您可以查看语法图中的unpivot子句

它清楚地表明,列表中只能显示列,不能显示文字或表达式。

您可能需要在内联视图中创建所需数量的常量列

作为替代方法,您可以使用老式方法:交叉连接+解码

select id,
       test_code,
       decode(test_code, 'T1', 'Test 01', 'T2', 'Test 02', 'Unknown test') as test_name,
       decode(test_code, 'T1', resulta, 'T2', resultb) result,
       decode(test_code, 'T1', unita, 'T2', unitb) unit,
       decode(test_code, 'T2', other_unitb) other_unit
  from my_table t,
       (select 'T' || level test_code from dual connect by level <= 2)

你为什么为这个丑陋的解决方案感到羞耻?您生成的附加列具有适当的名称,并且查询清楚它的作用;没有什么好羞愧的,它并不难看,你应该使用它。我主要关心的是我必须为常量值创建尽可能多的伪列。这是可行的,我只是想知道是否有一种方法可以在unpivot-clauseThanks中写入默认值/空值,至少这澄清了在我的情况下不能做什么!