Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

Self Join multiple rows in sql and create new columns

I'm trying to join multiple rows having same column DN value and creating view by showing them in multiple columns.

Table is:

OrderID DN Attributecode Attributevalue Itemcode
150000565101091 14768703 contractId 100000022 Silver_Bundle
150000565101091 14768703 activationDateInherited 11-JAN-21 Silver_Bundle
150000565101091 14768703 contractExpiryDate 11-APR-21 Silver_Bundle

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use conditional aggregation as follows:

Select orderid, dn, 
       Max(case when atrributecode = 'contractId' then attributevalue end) as contractId,
      Max(case when atrributecode = 'activationDateInherited' then attributevalue end) as activationDateInherited,
      Max(case when atrributecode = 'contractExpiryDate' then attributevalue end) as contractExpiryDate,
       Itemcode
  From t
Group by orderid, dn, itemcode

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...