根据SQL Query的重复项将一列中的数据拆分为多列

时间:2016-02-29 19:26:54

标签: sql oracle

我有这样的当前数据: this image1

我在image 1

中有如下数据

我想要像这样修改数据 this image2

我想在oracle SQL developer中使用SQL查询在image 2中输出。一个orderID的付款方法的副本应该放在不同的列中。

2 个答案:

答案 0 :(得分:1)

SELECT RegistrationId,
       OrderId,
       TotalPrice,
       MAX( CASE rn WHEN 1 THEN online_attribute1 END ) AS online_attribute1,
       MAX( CASE rn WHEN 2 THEN online_attribute1 END ) AS online_attribute2,
       MAX( CASE rn WHEN 3 THEN online_attribute1 END ) AS online_attribute3
  SELECT RegistrationId,
         OrderId,
         TotalPrice,
         online_attribute1,
         ROW_NUMBER()
           OVER ( PARTITION BY RegistrationId, OrderId, TotalPrice
                  ORDER BY NULL ) AS rn
  FROM   Data
)
GROUP BY RegistrationId, OrderId, TotalPrice

答案 1 :(得分:0)

  

您好。我假设您基本上是在使用Oracle版本。   我试图在下面的代码片段中复制方案。希望这个   帮助

SELECT a.id,
  a.ord_id,
  a.price,
  SUBSTR(wmsys.wm_concat(a.attr),1,instr(wmsys.wm_concat(a.attr),',',1)-1) att1,
  SUBSTR(wmsys.wm_concat(a.attr),instr(wmsys.wm_concat(a.attr),',',1)  +1,LENGTH(wmsys.wm_concat(a.attr))) att2
FROM
  (SELECT 'av@gamil.com' AS id,
    1                    AS ord_id,
    1000 price,
    'master_card' attr
  FROM dual
  UNION ALL
  SELECT 'av@gamil.com' AS id, 1 AS ord_id,1000 price,'Other' attr FROM dual
  UNION ALL
  SELECT 'sh@gamil.com' AS id, 2 AS ord_id,1060 price,'aramax' attr FROM dual
  UNION ALL
  SELECT 'sh@gamil.com' AS id, 2 AS ord_id,1060 price,'fedex' attr FROM dual
  )a
GROUP BY a.id,
  a.ord_id,
  a.price;

----------------------------------OUTPUT---------------------------------

ID           ORD_ID PRICE   ATT1          ATT2
av@gamil.com    1   1000    master_card   Other
sh@gamil.com    2   1060    aramax        fedex

----------------------------------output-------------------------------------