微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将行转置为来自多个表的列,无需汇总

如何解决将行转置为来自多个表的列,无需汇总

我需要从具有 nar_numseq_num公共字段的多个不同表中提取信息,但每个数据集都有不同的 accountid

我想要做的是找到 nar_numseq_num 在两个数据集中相同的地方,然后分配一个 accountid,它要么是 accountid一个数据集或一个新数字。

例如:我的第一个数据集有

accountid  nar_num  seq_num
1009       68428      1
1009       68429      2
1044       50005      1
1045      211981      1
1046      244155      1

我的第二个数据集有:

accountid   nar_num seq_num
12486      68428      1
12486      68429      2
12487      50005      1
12488     211981      1
12489     244155      1
12489      5005       2

我最终希望它看起来像这样:

accountid   seq_num1    seq_num2
1009         68428        68429
1004         5005   
1045         211981 
1046         244155 
98001        241155       5005

其中序列号和 nar_nums 匹配来自源 1 和 2 然后保留来自源 1 的 accountid,在那里它们不分配一个新的 accountid 从指定数字开始并递增为添加了更多记录。

我当前的 scipt 如下所示:

--misc debtors
with CTE_debtor_list (accountid,nar_num,seq_num)
as
(Select distinct
    ab.deb_num as accountid,ar.nar_no1 as nar_num,1 as seq_num
    from auarmast ar  
    inner join auarbals ab on ar.deb_num = ab.deb_num and ab.stu_cde = 1 and not(ab.ctg_cde in (17,99))
    inner join aunrmast nr on ar.nar_no1 = nr.nar_num 
    where ab.stu_cde = 1 and not(ab.ctg_cde in (7,99))
UNION
Select distinct
    ab.deb_num as accountid,ar.nar_no2 as nar_num,2 as seq_num
    from auarmast ar  
    inner join auarbals ab on ar.deb_num = ab.deb_num and ab.stu_cde = 1 and not(ab.ctg_cde in (17,99))
    inner join aunrmast nr on ar.nar_no2 = nr.nar_num 
    where ab.stu_cde = 1 and not(ab.ctg_cde in (7,ar.nar_no3 as nar_num,3 as seq_num
    from auarmast ar  
    inner join auarbals ab on ar.deb_num = ab.deb_num and ab.stu_cde = 1 and not(ab.ctg_cde in (17,99))
    inner join aunrmast nr on ar.nar_no3 = nr.nar_num 
    where ab.stu_cde = 1 and not(ab.ctg_cde in (7,ar.nar_no4 as nar_num,4 as seq_num
    from auarmast ar  
    inner join auarbals ab on ar.deb_num = ab.deb_num and ab.stu_cde = 1 and not(ab.ctg_cde in (17,99))
    inner join aunrmast nr on ar.nar_no4 = nr.nar_num 
    where ab.stu_cde = 1 and not(ab.ctg_cde in (7,99))
UNION
    select ass_num as accountid,seq_num = ROW_NUMBER() OVER(PARTITION BY aurtownr.ass_num ORDER BY aurtownr.ass_num)
    from aurtownr)

我将添加其他数据源。

目前我有多达 seq_num = 17 但这是一个基于来自一个数据集的数据的移动目标

Jointables

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。