我需要这个查询来返回结果,即使是重复的值。
查询示例:
SELECT p.SKU
, p.UPC
, sw.ProductName
FROM [TGB_Production]..[view_TGB_FutureProducts] p
INNER JOIN [TGB_Production]..[TGB_Products_SupWoW] sw ON p.SKU = sw.SKU
INNER JOIN [TGB_Production]..[view_TGB_FutureProducts_SupWoW] fsw ON p.SKU = fsw.SKU
and p.SKU in ('1839006B', '1839006B', '1839006C')
ORDER BY p.SKU DESC
当前结果:
SKU UPC ProductName
1839006C xxx xxx
1839006B xxx xxx
所需结果:
SKU UPC ProductName
1839006C xxx xxx
1839006B xxx xxx
1839006B xxx xxx
我在这里找到了类似的问题/答案,但是在合并解决方案时遇到了困难。
SQL query results Need to Return all records in WHERE clause even duplicates
view_TGB_FutureProducts_SupWoW:
SKU SalePrice CanadianSalePrice Minimum UnitIncrements
1839006B 40.00 0.00 2 2
TGB_Products_SupWoW:
SKU ProductName SkuGroup
1839006B Boucle Sweater Duster GRP_1839006
view_TGB_FutureProducts:
SKU ProductName UPC
1839006B Boucle Sweater Duster 45765751839006
答案 0 :(得分:1)
使用join
代替in
:
SELECT p.SKU, p.UPC, sw.ProductName
FROM [TGB_Production]..[view_TGB_FutureProducts] p JOIN
[TGB_Production]..[TGB_Products_SupWoW] sw
ON p.SKU = sw.SKU JOIN
[TGB_Production]..[view_TGB_FutureProducts_SupWoW] fsw
ON p.SKU = fsw.SKU JOIN
(VALUES ('1839006B'), ('1839006B'), ('1839006C')
) v(SKU)
ON v.SKU = p.SKU
ORDER BY p.SKU DESC;
如果您还想包含IN
列表中的值,而不是原始数据中的值,则需要外部联接。
答案 1 :(得分:0)
这可能有帮助:
SELECT tbl.SKU
, p.UPC
, sw.ProductName
FROM
(Select SKU FROM view_TGB_FutureProducts UNION
ALL
Select SKU FROM TGB_Products_SupWoW UNION ALL
Select SKU FROM view_TGB_FutureProducts_SupWoW
) tbl inner join
[TGB_Production]..[view_TGB_FutureProducts] p on p.Sku
= tbl.SKU
INNER JOIN [TGB_Production]..[TGB_Products_SupWoW]
sw ON tbl.SKU = sw.SKU
INNER JOIN [TGB_Production]..
[view_TGB_FutureProducts_SupWoW] fsw ON tbl.SKU =
fsw.SKU
and tbl.SKU in ('1839006B', '1839006B', '1839006C')
ORDER BY p.SKU DESC