如何 Select 忽略关系的多列上的不同行?

How do I Select distinct rows on multiple columns ignoring relations?

假设我们有这样一个 table:

ID1         ID2         ID3         ID3
----------- ----------- ----------- -----------
101         44          40          46
108         44          37          46
108         44          40          46
101         44          30          36
101         44          30          46
181         44          40          46
397         44          40          46
138         44          40          46
108         44          37          43
108         44          30          46

期望的输出是一个 select 查询,给出了这个:

ID1         ID2         ID3         ID4
----------- ----------- ----------- -----------
101         44          30          36
108         NULL        37          43
111         NULL        40          46
138         NULL        NULL        NULL
181         NULL        NULL        NULL
397         NULL        NULL        NULL

我有这个 select 查询几乎可以满足我的要求:

USE databaseName
DECLARE @start DATE
DECLARE @stop DATE
set @start = '2021-01-01'
set @stop = '2021-12-31'

SELECT DISTINCT t.ID1, NULL ID2, NULL ID3, NULL ID4
FROM ..IdTable t WHERE CreateDate BETWEEN @start AND @stop
UNION
SELECT DISTINCT NULL ID1, t.ID2, NULL ID3, NULL ID4
FROM ..IdTable t WHERE CreateDate BETWEEN @start AND @stop
UNION
SELECT DISTINCT NULL ID1, NULL ID2, t.ID3, NULL ID4
FROM ..IdTable t WHERE CreateDate BETWEEN @start AND @stop
UNION
SELECT DISTINCT NULL ID1, NULL ID2, NULL ID3, t.ID4
FROM ..IdTable t WHERE CreateDate BETWEEN @start AND @stop

输出:

ID1         ID2         ID3         ID4
----------- ----------- ----------- -----------
NULL        NULL        NULL        36
NULL        NULL        NULL        43
NULL        NULL        NULL        46
NULL        NULL        30          NULL
NULL        NULL        37          NULL
NULL        NULL        40          NULL
NULL        44          NULL        NULL
101         NULL        NULL        NULL
108         NULL        NULL        NULL
111         NULL        NULL        NULL
138         NULL        NULL        NULL
181         NULL        NULL        NULL
397         NULL        NULL        NULL

也许可以使用 dense_rank()

select ID1,ID2,ID3,ID4
from 
(
Select distinct ID1, r= dense_rank() over (order by ID1 asc) from IdTable 
)T1
full join 
(
Select distinct ID2, r=dense_rank() over (order by ID2 asc) from IdTable 
)T2 on T1.R=T2.R 
full join 
(
Select distinct ID3, r=dense_rank() over (order by ID3 asc) from IdTable 
)T3 on T1.R=T3.R
full join 
(
Select distinct ID4, r=dense_rank() over (order by ID4 asc) from IdTable 
)T4 on T1.R=T4.R