一直坚持找出这个问题。
商品数据库包含以下列: Customer_ID,Date,Trigger,Priority,PriorityOverRide。
我有查询选择Priority和PriorityOverride之间的最大值,但我似乎无法选择从中获取值的行。
目前我的查询是:
SELECT
Customer_ID,
CASE WHEN offers.PriorityOverride > 0 THEN offers.PriorityOverride ELSE
Priority END AS 'Priority'
FROM
(
SELECT
Customer_ID,
MAX(Priority) AS Priority,
MAX(PriorityOverride) AS PriorityOverride
FROM
offers
GROUP BY
CUstomer_ID
) AS offers
上述查询选择Priority和PriorityOverride的MAX值,并选择更大的值。
现在我无法选择选择优先级的日期和触发列。
编辑: 更多信息: PriorityOverride可以为null或为空但如果它不为空或为null,则其值将取代优先级所具有的值。
EDIT2: 这是我更新的查询:
SELECT Customer_ID, Date, Trigger_1, Priority, PriorityOverride
FROM Offers
WHERE (Customer_ID, Priority) in (
SELECT
Customer_ID,
CASE WHEN
MAX(PriorityOverride) > 0
THEN
MAX(PriorityOverride)
ELSE
MAX(Priority)
END AS 'Priority'
FROM
Offers
GROUP BY
Customer_ID
)
OR (Customer_ID, PriorityOverride) in (
SELECT
Customer_ID,
CASE WHEN
MAX(PriorityOverride) > 0
THEN
MAX(PriorityOverride)
ELSE
MAX(Priority)
END AS 'Priority'
FROM
Offers
GROUP BY
Customer_ID
)
但是上面的查询给了我一个错误"错误:在预期条件的上下文中指定的非布尔类型的表达式,在'附近,'。'
答案 0 :(得分:2)
您可以使用“最大”来获取max valeus和过滤器之间的较大位,以便在Customer_ID,Priority或Customer_ID,PriorityOverride上匹配值
select Customer_ID, Date, Trigger, Priority, PriorityOverRide
from offers
where (Customer_ID, Priority) in (
SELECT
Customer_ID,
max( case when Priority > PriorityOverride
THEN Priority
ELSE PriorityOverride
end )
FROM offers
GROUP BY CUstomer_ID
)
or (Customer_ID, PriorityOverride) in (
SELECT
Customer_ID,
max(case when Priority > PriorityOverride
THEN Priority
ELSE PriorityOverride
end )
FROM offers
GROUP BY CUstomer_ID
)