sql—通过另外两个id列将相关id链接在一起

vom3gejh  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(290)

我有一个大约10万行的表格,布局如下:

+----+-----------+------------+-------------------+
| ID |    PIN    |    RAID    | Desired Output ID |
+----+-----------+------------+-------------------+
|  1 | 80602627  | 1737852-1  |                 1 |
|  2 | 80602627  | 34046655-1 |                 1 |
|  3 | 351418172 | 33661      |                 2 |
|  4 | 351418172 | 33661      |                 2 |
|  5 | 351418172 | 33661      |                 2 |
|  6 | 351418172 | 34443321-1 |                 2 |
|  7 | 491863017 | 26136      |                 3 |
|  8 | 491863017 | 34575      |                 3 |
|  9 | 491863017 | 34575      |                 3 |
| 10 | 661254727 | 26136      |                 3 |
| 11 | 661254727 | 26136      |                 3 |
| 12 | NULL      | 7517       |                 4 |
| 13 | NULL      | 7517       |                 4 |
| 14 | NULL      | 7517       |                 4 |
| 15 | NULL      | 7517       |                 4 |
| 16 | NULL      | 7517       |                 4 |
| 17 | 554843813 | 33661      |                 2 |
| 18 | 554843813 | 33661      |                 2 |
+----+-----------+------------+-------------------+

id列具有唯一的值,pin和raid列是两个单独的标识号,用于将链接的id分组在一起。所需的output id列就是我希望sql所做的,本质上是查看pin列和raid列,找出它们之间的关系。
例如,如果期望的输出id=2,ids 3-6在pin=351418172上匹配,那么ids 17-18也匹配,因为33661的raid在ids 3-5的行中。
此外,null也将出现在pin列中,但不会出现在任何其他列中。
我确实发现了一个类似的问题文本,但因为它是在bigquery中,我不确定它是否会有帮助。
已经尝试破解这个一段时间没有运气,任何帮助非常感谢。

laik7k3q

laik7k3q1#

我想你的问题可以解决。我不确定pin和raid的组合应该是什么,但我想你可以想出这样的方法:

SELECT *,DENSE_RANK( )  over (ORDER BY isnull(pin,id) ),DENSE_RANK( )  over (ORDER BY raid)
FROM accounts
ubby3x7f

ubby3x7f2#

我相信我已经找到了一个解决这个问题的方法。当它一行一行地运行时,它运行得非常慢,在pin/raid上只会深入两个链接,但这对于99%以上的情况应该足够了。
如果有任何明显的迹象,我们将非常感谢您的建议。
上面帖子中的id是debtorno代码:

DECLARE     @Counter INT = 1
DECLARE     @EndCounter INT = 0

IF OBJECT_ID('Tempdb..#OrigACs') IS NOT NULL 
BEGIN 
DROP TABLE  #OrigACs
END 

SELECT      DebtorNo,
            Name,
            PostCode,
            DOB,
            RAJoin,
            COALESCE(PIN,DebtorNo COLLATE DATABASE_DEFAULT) AS PIN,
            RelatedAssets,
            RAID,
            PINRelatedAssets

INTO        #OrigACs

FROM        MIReporting..HC_RA_Test_Data RA

IF OBJECT_ID('Tempdb..#Accounts') IS NOT NULL 
BEGIN 
DROP TABLE  #Accounts
END 

SELECT      *,
            ROW_NUMBER() OVER (ORDER BY CAST(RA.DebtorNo AS INT)) AS Row

INTO        #Accounts

FROM        #OrigACs RA

ORDER BY    CAST(RA.DebtorNo AS INT)

CREATE INDEX Temp_HC_Index ON #OrigACs (RAID,PIN)

SET         @EndCounter = (SELECT MAX(Row) FROM #Accounts)

WHILE       @Counter <= @EndCounter 

BEGIN

IF OBJECT_ID('Tempdb..#RAID1') IS NOT NULL 
BEGIN 
DROP TABLE  #RAID1
END 

SELECT      *

INTO        #RAID1

FROM        #OrigACs A

WHERE       A.RAID IN (SELECT RAID FROM #Accounts WHERE [Row] = @Counter)

IF OBJECT_ID('Tempdb..#PIN1') IS NOT NULL 
BEGIN 
DROP TABLE  #PIN1
END 

SELECT      *

INTO        #PIN1

FROM        #OrigACs A

WHERE       A.PIN IN (SELECT PIN FROM #RAID1)

IF OBJECT_ID('Tempdb..#RAID2') IS NOT NULL 
BEGIN 
DROP TABLE  #RAID2
END 

SELECT      *

INTO        #RAID2

FROM        #OrigACs A

WHERE       A.RAID IN (SELECT RAID FROM #PIN1)

IF OBJECT_ID('Tempdb..#PIN2') IS NOT NULL 
BEGIN 
DROP TABLE  #PIN2
END 

SELECT      *

INTO        #PIN2

FROM        #OrigACs A

WHERE       A.PIN IN (SELECT PIN FROM #RAID2)

INSERT INTO MIReporting..HC_RA_Final_ACs

SELECT      DebtorNo,
            Name,
            PostCode,
            DOB,
            RAJoin,
            CASE
                WHEN PIN = DebtorNo COLLATE DATABASE_DEFAULT THEN NULL
                ELSE PIN
            END AS PIN,
            RelatedAssets,
            RAID,
            PINRelatedAssets,
            COALESCE((SELECT MAX(FRAID) FROM MIReporting..HC_RA_Final_ACs),0) + 1 AS FRAID

FROM        #PIN2

SET         @Counter = (SELECT MIN([ROW]) FROM #Accounts O WHERE O.DebtorNo NOT IN (SELECT DebtorNo FROM MIReporting..HC_RA_Final_ACs));

END;

SELECT      *

FROM        MIReporting..HC_RA_Final_ACs

DROP TABLE  #OrigACs
DROP TABLE  #Accounts
DROP TABLE  #RAID1
DROP TABLE  #PIN1
DROP TABLE  #RAID2
DROP TABLE  #PIN2

相关问题