将行转换为同一 table 中的列并从 table 中删除该行
Convert row to columns in the same table and delete the row from the table
如何通过将匹配的行转换为同一 table 中的列并从 table 中删除该行,从而从 table 中 select 行。有4个帐号(100,200,111,222),其中帐号100关联到111,帐号200关联到222。这里每个公司账户(200)都与姊妹公司账户(222)相关联。原始图片请参考下图table 和预期的结果。可能有没有关联公司的行。如果一家公司有关联的行,最终结果应该是它们需要转换为一行中的列和单独的行。我尝试使用 Pivot 但无法获得结果。任何建议都可以帮助
Table structure and expected result
您可以使用自连接和解析函数如下:
Select * from
(Select t.comapny, t.acct, t.amount,
S.comapny as company_1, s.acct as acct_1, s.amount as amount_1,
Row_number()
over (partition by least(t.comapny, s.comapny) order by t.amount) as rn
From your_table t
Left join your_table s on t.sister_company = s.company) t
Where rn = 1
如何通过将匹配的行转换为同一 table 中的列并从 table 中删除该行,从而从 table 中 select 行。有4个帐号(100,200,111,222),其中帐号100关联到111,帐号200关联到222。这里每个公司账户(200)都与姊妹公司账户(222)相关联。原始图片请参考下图table 和预期的结果。可能有没有关联公司的行。如果一家公司有关联的行,最终结果应该是它们需要转换为一行中的列和单独的行。我尝试使用 Pivot 但无法获得结果。任何建议都可以帮助
Table structure and expected result
您可以使用自连接和解析函数如下:
Select * from
(Select t.comapny, t.acct, t.amount,
S.comapny as company_1, s.acct as acct_1, s.amount as amount_1,
Row_number()
over (partition by least(t.comapny, s.comapny) order by t.amount) as rn
From your_table t
Left join your_table s on t.sister_company = s.company) t
Where rn = 1