在DB2中枢转数据时遇到问题

时间:2019-03-14 21:48:54

标签: sql db2 ibm-midrange

在此标记为重复项之前,请知道我已经从这些链接完成了关于DB2中透视的研究(即使DB2没有PIVOTPivoting in DB2 on SOIBM Developers,但我无法理解如何使用我的数据,需要帮助。我试图使用来自这两个链接的示例来操纵我的字符串,但无法使其正常工作。我不要求任何人为我编写完整的代码,而只是给我一个正确的方向,说明如何更改我的字符串以检索所需的结果。预先谢谢你。

当前字符串:

SELECT 
  cfna1 AS "Customer Name", cfrisk AS "Risk Rating", cfrirc AS "Rated By", date(digits(decimal(cfrid7 + 0.090000, 7, 0))) AS "Risk Rated Date",cfuc3n3 AS "Credit Score", date(digits(decimal(cf3ud7 + 0.090000, 7, 0))) AS "CR Date"
  FROM cncttp08.jhadat842.cfmast cfmast
  WHERE cfcif# IN ('T000714', 'T000713', 'T000716', 'T000715')
ORDER BY
  CASE cfcif# 
    WHEN 'T000714' THEN 1
    WHEN 'T000713' THEN 2
    WHEN 'T000716' THEN 3 
    WHEN 'T000715' THEN 4
  END

预期的字符串结果:

Customer Name | Risk Rating | Rated By | Risk Rated Date | Credit Score | CR Date
Elmer Fudd         8            MLA        2018-02-08        777           2018-02-08

我想要实现的结果:

               Elmer Fudd
Risk Rating        8
Rated By          MLA
Risk Rated Date  2018-02-08
Credit Score      777
CR Date          2018-02-08

2 个答案:

答案 0 :(得分:1)

使用开发人员链接中建议的unpivot方法,并使用强制转换将所有列转换为varchar。

示例:

select st1.id1, unpivot1.col1, unpivot1.val1
  from (
      select id1, char1 , date1, number1
      from sometable
    ) st1, 
    lateral (values 
      ('char col', cast(st1.char1 as varchar(100))), 
      ('date col', cast(st1.date1 as varchar(100))),
      ('number col', cast(st1.number1 as varchar(100))) 
    ) as unpivot1 (col1, val1)
  order by st1.id1

答案 1 :(得分:0)

我认为在sql中无法输出-您是说这样的东西吗?

id_group  Data_Type       Value
1         Name            Elmer Fudd
1         Risk Rating     8
1         Rated By        MLA
1         Risk Rated Date 2018-02-08
1         Credit Score    777
1         CR Date         2018-02-08

为此,我们需要另一列将所有元素组合在一起-我称其为“ id_group”,这是标识组的列