早上好,我正在编写代码来更新我的客户表,
Update ALEKWE_CUSTOMER C
SET RECENTLY_PURCHASED= (SELECT CASE WHEN (DATE_PURCHASED BETWEEN ADD_MONTHS(SYSDATE,-12) AND SYSDATE) THEN 'Y' ELSE 'N' END
from ALEKWE_CUSTOMER_PRODUCT D
where C.customer_id=D.customer_id)
但它带来了错误
Single query returning more than one row
有没有更好的方法来编写这段代码? 谢谢你的帮助
答案 0 :(得分:0)
如果您需要了解客户是否已购买了一年的商品,那么您可以使用最大功能(''>' N')来获得一行:
UPDATE ALEKWE_CUSTOMER C
SET RECENTLY_PURCHASED = (SELECT MAX(CASE
WHEN (DATE_PURCHASED BETWEEN ADD_MONTHS(SYSDATE,-12) AND SYSDATE) THEN 'Y'
ELSE 'N'
END
)
FROM ALEKWE_CUSTOMER_PRODUCT D
WHERE C.customer_id=D.customer_id
)
答案 1 :(得分:0)
您收到错误消息“单个查询返回多行”,因为某些客户可能在不同日期购买了多个产品。因此,对于同一客户,您最终可能会得到“Y”和“N”,这是Oracle无法决定的,因此警告。
如果您将多个购买日期汇总到最新日期,即MAX(DATE_PURCHASED),则查询将有效:
UPDATE ALEKWE_CUSTOMER C
SET RECENTLY_PURCHASED=(
SELECT CASE WHEN (MAX(DATE_PURCHASED) BETWEEN ADD_MONTHS(SYSDATE,-12) AND SYSDATE)
THEN 'Y' ELSE 'N' END
FROM ALEKWE_CUSTOMER_PRODUCT D
WHERE C.customer_id=D.customer_id);
但我同意@zaratustra你可能不应该将最近的商店存储在客户表中......