还有另一种写方法吗?
SELECT src.ID, factDeviceBuild.ID
FROM #factDeviceBuild as src
INNER JOIN AppsFlyer.FactDeviceBuild AS factDeviceBuild
ON src.[DimDevice_Id] = factDeviceBuild.[DimDevice_Id] AND
src.[DimDeviceModel_Id] = factDeviceBuild.[DimDeviceModel_Id] AND
src.[DimPlatform_Id] = factDeviceBuild.[DimPlatform_Id] AND
src.[DimOSVersion_Id] = factDeviceBuild.[DimOSVersion_Id] AND
src.[DimSDKVersion_Id] = factDeviceBuild.[DimSDKVersion_Id] AND
src.[DimCarrier_Id] = factDeviceBuild.[DimCarrier_Id] AND
src.[DimOperator_Id] = factDeviceBuild.[DimOperator_Id]
我一直在尝试像这样做一些不同的事情(不起作用)
SELECT *, factDeviceBuild.ID
FROM #factDeviceBuild
WHERE EXISTS (
SELECT [DimDevice_Id], [DimDeviceModel_Id], [DimPlatform_Id],
[DimOSVersion_Id], [DimSDKVersion_Id], [DimCarrier_Id],
[DimOperator_Id]
FROM AppsFlyer.FactDeviceBuild AS factDeviceBuild
)
或类似的方法(也不起作用):
SELECT factDeviceBuild.ID,
factDeviceBuild.[ID]
FROM (
SELECT [DimDevice_Id], [DimDeviceModel_Id], [DimPlatform_Id],
[DimOSVersion_Id], [DimSDKVersion_Id], [DimCarrier_Id],
[DimOperator_Id]
FROM AppsFlyer.FactDeviceBuild AS factDeviceBuild
INTERSECT
SELECT [DimDevice_Id], [DimDeviceModel_Id], [DimPlatform_Id],
[DimOSVersion_Id], [DimSDKVersion_Id], [DimCarrier_Id],
[DimOperator_Id]
FROM AppsFlyer.#factDeviceBuild AS factDeviceBuild
) AS A
我只是在进行一些查询调整。 EXCEPT
和INTERSECT
尤其有趣,因为它们对待NULLS
的方式。
很明显,我可以使用CROSS JOIN
或OUTER JOIN
来构造我的INNER JOIN
表格草稿,但是我看不到那里有什么特别的收获。
答案 0 :(得分:2)
我相信您正在寻找这样的东西:
SELECT src.ID, fact.ID
FROM #factDeviceBuild as src
INNER JOIN AppsFlyer.FactDeviceBuild AS fact
ON EXISTS (
SELECT src.DimDevice_Id, src.DimDeviceModel_Id, src.DimPlatform_Id,
src.DimOSVersion_Id, src.DimSDKVersion_Id, src.DimCarrier_Id,
src.DimOperator_Id
INTERSECT
SELECT fact.DimDevice_Id, fact.DimDeviceModel_Id, fact.DimPlatform_Id,
fact.DimOSVersion_Id, fact.DimSDKVersion_Id, fact.DimCarrier_Id,
fact.DimOperator_Id
)
使用这种INTERSECT
语法(而不是通常的条件)具有将NULL-视为相同值的优点。例如,如果仅DimCarrier_Id
和DimOperator_Id
列允许NULL,则等效条件将是:
SELECT src.ID, fact.ID
FROM #factDeviceBuild as src
INNER JOIN AppsFlyer.FactDeviceBuild AS fact
ON src.DimDevice_Id = fact.DimDevice_Id AND
src.DimDeviceModel_Id = fact.DimDeviceModel_Id AND
src.DimPlatform_Id = fact.DimPlatform_Id AND
src.DimOSVersion_Id = fact.DimOSVersion_Id AND
src.DimSDKVersion_Id = fact.DimSDKVersion_Id AND
(src.DimCarrier_Id = fact.DimCarrier_Id OR src.DimCarrier_Id IS NULL AND fact.DimCarrier_Id IS NULL) AND
(src.DimOperator_Id = fact.DimOperator_Id OR src.DimOperator_Id IS NULL AND fact.DimOperator_Id IS NULL)
答案 1 :(得分:0)
关注相同
SELECT src.ID, factDeviceBuild.ID
FROM #factDeviceBuild as src, AppsFlyer.FactDeviceBuild AS factDeviceBuild
WHERE
src.[DimDevice_Id] = factDeviceBuild.[DimDevice_Id] AND
src.[DimDeviceModel_Id] = factDeviceBuild.[DimDeviceModel_Id] AND
src.[DimPlatform_Id] = factDeviceBuild.[DimPlatform_Id] AND
src.[DimOSVersion_Id] = factDeviceBuild.[DimOSVersion_Id] AND
src.[DimSDKVersion_Id] = factDeviceBuild.[DimSDKVersion_Id] AND
src.[DimCarrier_Id] = factDeviceBuild.[DimCarrier_Id] AND
src.[DimOperator_Id] = factDeviceBuild.[DimOperator_Id]
答案 2 :(得分:0)
没有数据或预期结果的可视化,我的猜测是您需要将7个id类型“取消透视”成更少的列,从而降低了连接语法的复杂性。例如:
select
src.id, f.fact_id, ca.id_type, ca.id_value
from #factDeviceBuild as src
cross apply (
values
('DimDevice_Id',src.[DimDevice_Id])
,('DimDeviceModel_Id',src.[DimDeviceModel_Id])
,('DimPlatform_Id',src.[DimPlatform_Id])
,('DimOSVersion_Id',src.[DimOSVersion_Id])
,('DimSDKVersion_Id',src.[DimSDKVersion_Id])
,('DimCarrier_Id',src.[DimCarrier_Id])
,('DimOperator_Id',src.[DimOperator_Id])
) ca (id_type, id_value)
inner join (
select
fact.id fact_id, ca.id_type, ca.id_value
from AppsFlyer.FactDeviceBuild AS fact
cross apply (
values
('DimDevice_Id',fact.[DimDevice_Id])
,('DimDeviceModel_Id',fact.[DimDeviceModel_Id])
,('DimPlatform_Id',fact.[DimPlatform_Id])
,('DimOSVersion_Id',fact.[DimOSVersion_Id])
,('DimSDKVersion_Id',fact.[DimSDKVersion_Id])
,('DimCarrier_Id',fact.[DimCarrier_Id])
,('DimOperator_Id',fact.[DimOperator_Id])
) ca (id_type, id_value)
where ca.id_value IS NOT NULL
) as f on ca.id_type = f.id_type and ca.id_value = f.id_value
注意,我没有使用TSQL的“ unpivot”功能,因为我更喜欢上面看到的语法。使用此apply/values
语法时,没有其他性能劣势。
NB:所有这些id类型列中的所有7个必须是“兼容”数据类型,“ unpivot”才能正常工作。例如,全部7为整数,这会使id_value
成为整数列。