Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic pivot table for every single row (MySql)

I have a table Customers like:

ID       Type       Date          Address         SSN
RT124    MASTER     12/15/2005    7 Hill st       12345 
RT542    MASTER     06/14/2006    7 Hill st       12345
HT457    UNIQUE     10/27/2009    10 PARK WAY     24569   
QA987    UNIQUE     08/28/2010    10 PARK WAY     24569
AH825    UNIQUE     10/12/2012    10 PARK WAY     24569
14837    SINGLE     05/05/2010    2 TED ROAD      11111
24579    MARRIED    06/24/2014    2 TED ROAD      11111

What I want is to create a new column +# for every duplicate address and SSN and always the ID #1 should be the Date most recent.

Note: this table only has duplicate rows based on the address and SSN but unique ID and it doesn't require any sum.

So the output should be like this (Click on the image to zoom): enter image description here

I have done some research and tried some examples but nothing work to get this output.

I will appreciate any help !

like image 241
JC_BI Avatar asked Feb 18 '26 16:02

JC_BI


2 Answers

You need to enumerate the rows and aggregate. In MySQL (pre V8), it looks like:

select address, ssn,
       max(case when rn = 1 then id end) as id1,
       max(case when rn = 1 then type end) as type1,
       max(case when rn = 1 then date end) as date1,
       max(case when rn = 2 then id end) as id2,
       max(case when rn = 2 then type end) as type2,
       max(case when rn = 2 then date end) as date2
       . . .
from (select c.*,
             (@rn := if(@as = concat_ws(':', address, ssn), @rn + 1,
                        if(@as := concat_ws(':', address, ssn), 1, 1)
                       )
             ) as rn
      from (select c.* from customers c order by address, ssn, date desc) c cross join
           (select @as := '', @rn := 0) params
     ) c
group by address, ssn;

Note that this doesn't repeat address and ssn. That doesn't seem useful, but you can of course repeat those columns in each group.

like image 187
Gordon Linoff Avatar answered Feb 21 '26 13:02

Gordon Linoff


Is there a limit to the number of times an address can be duplicated? If there is a known limit, you could have a number of left joins for each duplicate. The following would be a solution if you knew there would only ever be 6 or fewer duplicates:

with a as (
select 
    ID
    ,type
    ,date
    ,address
    ,SSN
    row_number() over(partition by address, SSN order by date desc) as R
from Customers
)

select 
    a.id ID1
    ,a.type TYPE1
    ,a.date DATE1
    ,a.address ADDRESS1
    ,a.ssn SSN1

    ,b.id ID2
    ,b.type TYPE2
    ,b.date DATE2
    ,b.address ADDRESS2
    ,b.ssn SSN2

    ,c.id ID3
    ,c.type TYPE3
    ,c.date DATE3
    ,c.address ADDRESS3
    ,c.ssn SSN3

    ,d.id ID4
    ,d.type TYPE4
    ,d.date DATE4
    ,d.address ADDRESS4
    ,d.ssn SSN4

    ,e.id ID5
    ,e.type TYPE5
    ,e.date DATE5
    ,e.address ADDRESS5
    ,e.ssn SSN5

    ,f.id ID6
    ,f.type TYPE6
    ,f.date DATE6
    ,f.address ADDRESS6
    ,f.ssn SSN6

from a
left join
    (select * from a
    where r=2
    ) b
on a.address=b.address and a.ssn=b.ssn

left join
    (select * from a
    where r=3
    ) c
on a.address=c.address and a.ssn=c.ssn

left join
    (select * from a
    where r=4
    ) d
on a.address=d.address and a.ssn=d.ssn

left join
    (select * from a
    where r=5
    ) e
on a.address=e.address and a.ssn=e.ssn

left join
    (select * from a
    where r=6
    ) f
on a.address=f.address and a.ssn=f.ssn

where r=1

If you have more than 6, just add another set of columns to the select statement:

    ,f.id ID6
    ,f.type TYPE6
    ,f.date DATE6
    ,f.address ADDRESS6
    ,f.ssn SSN6

and a new left join to the from statement:

left join
    (select * from a
    where r=6
    ) f
on a.address=f.address and a.ssn=f.ssn
like image 28
Golden Ratio Avatar answered Feb 21 '26 13:02

Golden Ratio