简体   繁体   中英

Create column from values in row using SQL

Can anyone please help me to join two tables using sql? After join rows should appear as a column as shown below in the output.

I have two tables T1 and T2

T1
------------
Id.  Name  date
--------------
1    AAA    2019-05-06
2    BBB    2019-05-06



T2
---------------
Id.  attr_name. attr_value. date
--------------------
1 .   PP0         8       2019-05-06    
1 .   PD0        125.00     2019-05-06
1     PP1         2         2019-05-06
1     PD1        150.00      2019-05-06
3     PP0         5         2019-05-06
3     PD1        50.00      2019-05-06

when there is PP0, there will be corresponding PD0

My output should be
---------------------
Id    Name       attr_pp_name     attr_pp_value.  attr_pd_name     attr_pd_value
1     AAA          PP0                  8               PD0          125.00
1.    AAA          PP1                  2               PD1          150.00

I tried my query as below

select t.Id, t.name ea.attr_name ,ea.attr_value, ea1.attr_name, ea1.attr_value from T1 t
INNER JOIN T2 ea ON t.id = ea.id AND ea.attr_name IN ('PP0', 'PP1')
INNER JOIN T2 ea1 ON t.id = ea.id AND ea.attr_name IN ('PD0', 'PD1');

But above query gives the duplicate results as

Id    Name       attr_pp_name     attr_pp_value.  attr_pd_name     attr_pd_value
1     AAA          PP0                  8               PD0          125.00
1.    AAA          PP1                  2               PD1          150.00
1     AAA          PP0                  2               PD1          150.00
1.    AAA          PP1                  8               PD0          125.00

In MySQL 8+, you can use window functions to enumerate the attribute values, and then aggregate:

select t.*,
       max(case when ea.attr_name like 'PP%' then ea.attr_name end),
       max(case when ea.attr_name like 'PP%' then ea.attr_value end),
       max(case when ea.attr_name like 'PD%' then ea.attr_name end),
       max(case when ea.attr_name like 'PD%' then ea.attr_value end)
from T1 t join
     (select ea.*,
             row_number() over (partition by ea.id, left(ea.attr_name, 2) order by ea.id) as seqnum
      from t2 ea
     ) ea
     on ea.id = t.id
group by t.id, ea.seqnum;

EDIT:

One way to approach this in earlier versions is to use variables:

select t.*,
       max(case when ea.attr_name like 'PP%' then ea.attr_name end),
       max(case when ea.attr_name like 'PP%' then ea.attr_value end),
       max(case when ea.attr_name like 'PD%' then ea.attr_name end),
       max(case when ea.attr_name like 'PD%' then ea.attr_value end)
from T1 t join
     (select ea.*,
             (@rn := if(@ida = concat_ws(':', ea.id, left(ea.attr_name, 2)), @rn + 1,
                        if(@ida := concat_ws(':', ea.id, left(ea.attr_name, 2)), 1, 1)
                       )
             ) as seqnum
      from (select ea.*
            from t2 ea
            order by ea.id, left(ea.attr_name, 2)
           ) ea cross join
           (select @ida := '', @rn := 0) params
     ) ea
     on ea.id = t.id
group by t.id, ea.seqnum;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM