如何连接2个不包含外键列的表变量。
DECLARE @InventoryIDList TABLE(ID INT)
DECLARE @ProductSupplierIDList TABLE(ID INT)
例外输出
@InventoryList
--------------
123
456
789
111
@ProductSupplierIDList
--------------
999
888
777
666
@InventoryList ProductSupplierIDList
---------------------------------------
123 | 999
567 | 888
789 | 777
111 | 666
所有都是随机数据。我只想将2表变量结合起来如上所示。我尝试了所有类型的连接。但我需要提到上面提到的输出而没有null
值。
我尝试了CROSS APPLY
SELECT *
FROM @InventoryIDList invList CROSS APPLY @ProductSupplierIDList prdList
但它给了我5 ^ 2个元素作为重复的结果。
答案 0 :(得分:3)
我猜您需要Row_Number
和Full Outer Join
,考虑到这两个表之间没有关系
SELECT I.ID,
P.ID
FROM (SELECT Rn = Row_number()OVER(ORDER BY ID),*
FROM @InventoryList) I
FULL JOIN (SELECT Rn = Row_number()OVER(ORDER BY ID),*
FROM @ProductSupplierIDList) p
ON I.RN = P.RN
答案 1 :(得分:3)
由于ID不按顺序排列且可以是随机的,我建议在表变量上使用Identity并加入:
import { Pipe, PipeTransform } from '@angular/core';
import { Observable } from "rxjs/Observable";
import "rxjs/add/operator/map";
@Pipe({
name: 'map'
})
export class MapPipe implements PipeTransform {
transform(o$, propsNames): any {
return o$.map(res => {
return propsNames.reduce((acc, cur) => {
acc[cur] = res[cur];
return acc;
}, {})
})
}
}
答案 2 :(得分:2)
假设JOIN
条件是按递增ID顺序的相同“行号”:
WITH invList AS (
SELECT *,
ROW_NUMBER() OVER (ORDER BY ID) AS RN
FROM @InventoryIDList),
prdList AS (
SELECT *,
ROW_NUMBER() OVER (ORDER BY ID) AS RN
FROM @ProductSupplierIDList)
SELECT *
FROM invList IL
JOIN prdList PL ON IL.RN = PL.RN;