在以下代码中,purchase_price将按语句覆盖案例顺序。
SELECT
i.products_mpn, i.active,
i.distributor_code, i.sales_price
FROM
@duplicates d
LEFT JOIN
import i ON i.products_mpn = d.products_mpn
AND i.distributor_code = (SELECT TOP 1 distributor_code
FROM dbo.kting_ICECAT_import
WHERE products_mpn = d.products_mpn
ORDER BY
(SELECT
CASE
WHEN (i.distributor_code = 'x' or i.distributor_code = 'y') AND CAST(stock as int) > 0
THEN 2
WHEN CAST(stock as int) > 0
THEN 1
ELSE 0
END) DESC,
CAST(purchase_price AS decimal(28,12)))
有没有办法让它通过case语句然后purchase_price
进行排序?
答案 0 :(得分:1)
如果您希望首先应用CASE
语句的排序,然后排序purchase_price
,那么您的ORDER BY
已经将条款放在正确的位置。我不知道SELECT
表达式中CASE
正在做什么,或者即使当前查询运行也是如此。这样的事情可能就是你想到的:
ORDER BY CASE WHEN (i.distributor_code = 'x' or i.distributor_code = 'y') AND
CAST(stock as int) > 0 THEN 2
WHEN CAST(stock as int) > 0 THEN 1
ELSE 0
END DESC,
CAST(purchase_price AS DECIMAL(28,12))
答案 1 :(得分:0)
问题出在ORDER BY SELECT中。我曾使用左边的列连接“i.distributor_code”应该是distributor_code