Oracle将2行合并为一个具有不同连接条件的行

时间:2014-11-17 16:34:38

标签: sql oracle pivot

没有customerId的记录总是有DT01-DT09和WT01,而带有customerId的记录有WIF ... CustomerId的记录基本上缺少DT,但它是同一个客户。 我如何选择记录以将它们排成一行?

enter image description here

Full Size

我正在调整ATM的脚本如下:

 select * from (
                        select
                           tqs.transactiondetailid as transactiondetailid,
                          q.productid as productid,
                          tqs.answer as QAnswer,
                          QCODE,
                          tqs.transactionversion as transactionversion,
                          tqs.CUSTOMERID AS CUSTOMERID
                        from TRANSACTIONDETAILQSHIS tqs
                        inner join question q on q.questionid = tqs.questionid 
                        and (
                              q.qcode like 'DT%'
                              or  q.qcode like 'WT%'
                              or (q.qcode like 'WIF%' and q.isthunderheadonly = 0))
        ) pivot (
        min(QAnswer) for QCODE in (
        'DT01', 'DT02', 'DT03', 'DT04', 'DT05', 'DT06', 'DT07', 'DT09', 'WT01', 'WIF3.1', 'WIF4.1', 'WIF4.2', 'WIF6.1', 'WIF7.1', 'WIF7.2', 'WIF7.3', 'WIF7.7', 'WIF7.10', 'WIF9.1', 'WIF9.6', 'WIF10.1', 'WIF10.2', 'WIF12.1', 'WIF13.1', 'WIF13.1.1', 'WIF14.1'
               ))
               where transactiondetailid =  5845  

最后transactiondetailid = 5845只是为了清晰起见,通常都是记录。

3 个答案:

答案 0 :(得分:0)

您可以使用GROUP BY并在PIVOT

之前的子查询中根据客户ID,交易明细ID,产品ID,交易版本获取Qcode值中的最大值
              select
                      tqs.transactiondetailid as transactiondetailid,
                      q.productid as productid,
                      max(tqs.answer) as QAnswer,
                      tqs.transactionversion as transactionversion,
                      max(tqs.CUSTOMERID) AS CUSTOMERID
                      max(qcode) as QCODE
                    from TRANSACTIONDETAILQSHIS tqs
                    inner join question q on q.questionid = tqs.questionid 
                    and (
                          q.qcode like 'DT%'
                          or  q.qcode like 'WT%'
                          or (q.qcode like 'WIF%' and q.isthunderheadonly = 0))
                    group by tqs.transactiondetailid, q.productid, q.productid

答案 1 :(得分:0)

您可以像这样使用GROUP BY:

select tqs.transactiondetailid as transactiondetailid,
       q.productid as productid,
       MAX(tqs.answer) as QAnswer, -- not sure about that
       MAX(QCODE) as QCODE,
       tqs.transactionversion as transactionversion,
       MAX(tqs.CUSTOMERID) AS CUSTOMERID
  from TRANSACTIONDETAILQSHIS tqs
 inner join question q
    on q.questionid = tqs.questionid 
   and (q.qcode like 'DT%'
        or  q.qcode like 'WT%'  
        or (q.qcode like 'WIF%' and q.isthunderheadonly = 0))
 GROUP BY tqs.transactiondetailid, q.productid, tqs.transactionversion

答案 2 :(得分:0)

自己想出来。
上述所有答案中的问题是,如果您在过滤之前消除了CustomerId - 您将获得其他客户的数据(transactionversion 9和10有两个客户),诀窍是过滤记录在customerid上没有从内部选择中选择customerid - 这样,在从第二个客户跳过记录时,行将合并为一个。

我这样做的方式我在上面创建了一个子选择(视图),其中包含我需要的customerid,然后加入该视图以过滤匹配的客户,如果我需要该customerid,我总能得到它来自customerv

SELECT *
FROM
  ( SELECT tqs.transactiondetailid AS transactiondetailid,
           q.productid AS productid,
           tqs.answer AS QAnswer,
           QCODE,
           tqs.transactionversion AS transactionversion
   FROM TRANSACTIONDETAILQSHIS tqs
   INNER JOIN question q ON q.questionid = tqs.questionid
   INNER JOIN customerv curr ON curr.transactiondetailid = tqs.transactiondetailid
   WHERE curr.CUSTOMERID = tqs.CUSTOMERID
     OR tqs.customerid IS NULL ) pivot ( min(QAnswer)
                                        FOR QCODE IN ( 'DT01', 'DT02', 'DT03', 'DT04', 'DT05', 'DT06', 'DT07', 'DT09', 'WT01', 'WIF3.1', 'WIF4.1', 'WIF4.2', 'WIF6.1', 'WIF7.1', 'WIF7.2', 'WIF7.3', 'WIF7.7', 'WIF7.10', 'WIF9.1', 'WIF9.6', 'WIF10.1', 'WIF10.2', 'WIF12.1', 'WIF13.1', 'WIF13.1.1', 'WIF14.1' )) x
WHERE x.transactiondetailid = 5845 

见下面的结果:

enter image description here