You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为指定客户缺失定价的商品从ProductSellPriceBand获取兜底价格?

解决方案

你需要先确保所有指定商品都被纳入查询范围,再通过COALESCE函数优先取客户专属价格,无专属价格时兜底用AB区域8级的默认价格。修改后的SQL如下:

SELECT
    COALESCE(spr.Price, pspb.SellPrice) AS FinalPrice,
    Per.PerCode,
    p.ProductCode,
    p.Description
FROM
    Product p
LEFT JOIN SellPriceRule spr 
    ON p.ProductID = spr.ProductID
    AND EXISTS (
        SELECT 1 FROM Customer c 
        WHERE c.CustomerID = spr.CustomerID 
        AND c.CustomerCode = '7000192'
    )
LEFT JOIN Per 
    ON Per.PerID = spr.PricePerID
LEFT JOIN ProductSellPriceBand pspb 
    ON p.ProductID = pspb.ProductID
    AND pspb.BandNumber = 8
WHERE
    p.ProductCode IN ('27MIETE10B', '22RGR45DHS', '23RGWBBCA', '22RGED4WA', '22RGED45WA')

关键改动说明:

  • 以Product表为查询起点,确保指定的5个商品全部出现在结果中,不会因为无客户专属价格被过滤
  • 将客户筛选条件从WHERE移到SellPriceRule的连接条件中,避免左连后因CustomerCode为null被过滤
  • 用COALESCE(spr.Price, pspb.SellPrice)实现价格兜底逻辑:优先取SellPriceRule中的客户专属价,无值时取ProductSellPriceBand中BandNumber=8的默认价
  • 左连ProductSellPriceBand时直接筛选BandNumber=8,减少不必要的数据关联

内容的提问来源于stack exchange,提问作者Aaron Bosa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 04:00:52