简体   繁体   English

每一行的动态数据透视表(MySql)

[英]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. 我想要的是为每个重复的地址和SSN创建一个新的列+#,并且始终ID#1应该是最近的日期。

Note: this table only has duplicate rows based on the address and SSN but unique ID and it doesn't require any sum. 注意:此表仅具有基于地址和SSN但具有唯一ID的重复行,并且不需要任何总和。

So the output should be like this (Click on the image to zoom): 所以输出应该是这样的(单击图像进行缩放): 在此输入图像描述

I have done some research and tried some examples but nothing work to get this output. 我已经做了一些研究并尝试了一些例子,但没有任何工作来获得这个输出。

I will appreciate any help ! 我将不胜感激任何帮助!

You need to enumerate the rows and aggregate. 您需要枚举行和聚合。 In MySQL (pre V8), it looks like: 在MySQL(V8之前版本)中,它看起来像:

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 . 请注意,这不会重复addressssn That doesn't seem useful, but you can of course repeat those columns in each group. 这似乎没用,但你当然可以在每个组中重复这些列。

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: 如果您知道只有6个或更少的重复项,以下将是一个解决方案:

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: 如果您有超过6个,只需在select语句中添加另一组列:

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

and a new left join to the from statement: 和一个新的左连接到from语句:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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