如果客户列表中缺少代码但仍在 WHERE IN 子句中,希望能够使用单独的查询提取代码。因此,对于此查询,我仅适用于客户“测试”时主查询中存在的项目。
如果找不到下面的 WHERE IN 子句中列出的
itemcode
,我需要它来提取下一个选项 and [item_or_pricecode] in ('ABC1234','X123456','BR23456','CX23456')
。
如果没有找到 BR23456 的项目代码(我已经从数据中删除以测试这一点),我需要它在这个示例中使用的参数,它需要 blvl = 8 并且客户是“lvlholder”。
由于我的情况,我不确定是否需要在第一个语句周围进行选择
WHERE p.[Customer] ='test'
AND p.[itemcode] IN ('ABC1234', 'X123456', 'BR23456', 'CX23456')
仅查找客户“测试”中存在的
itemcode
。
这是代码
SELECT
p.[itemcode],
p.[uom],
p.[trtype],
p.[alvl],
p.[blvl],
CASE
WHEN p.[alvl] > 0
THEN (SELECT TOP 1 x.start_date FROM pricing x
WHERE x.itemcode = p.itemcode AND x.blvl = p.alvl
AND customer = 'lvlholder'
ORDER BY start_date DESC)
WHEN p.[trtype] = ''
THEN (SELECT TOP 1 x.start_date FROM pricing x
WHERE x.itemcode = p.itemcode AND x.blvl = 8
AND customer = 'lvlholder'
ORDER BY start_date DESC)
ELSE p.[start_date]
END as start_date,
CASE
WHEN p.[trtype] = 'Quot'
THEN p.[price]
WHEN p.[alvl] > 0
THEN (SELECT TOP 1 x.price FROM pricing x
WHERE x.itemcode = p.itemcode AND x.blvl = p.alvl
AND customer = 'lvlholder'
ORDER BY start_date DESC)
WHEN p.[trtype] = ''
THEN (SELECT TOP 1 x.price FROM pricing x
WHERE x.itemcode = p.itemcode AND x.blvl = 8
AND customer = 'lvlholder'
ORDER BY start_date DESC)
ELSE 0
END AS LevelResult ,
p.price
FROM
pricing p
WHERE
p.[Customer] = 'test'
AND p.[itemcode] IN ('ABC1234', 'X123456', 'BR23456', 'CX23456')
ORDER BY
p.[start_date] DESC
这就是我希望的最终结果
商品代码 | 乌姆 | 价格 | 开始日期 | 水平结果 |
---|---|---|---|---|
ABC1234 | EA | 0.00 | 2024 年 1 月 15 日 | 29.57 |
X123456 | CT | 144.10 | 2024 年 5 月 15 日 | 144.10 |
BR23456 | 电脑 | 0.00 | 2024 年 1 月 1 日 | 15.56 |
CX23456 | PNL | 0.00 | 2024 年 1 月 1 日 | 15.36 |
下面是表格及其[示例数据][1]的数据
create table pricing(Customer,itemcode,uom,trtype,Alvl,Blvl,price,start_date)as values
('test','ABC1234','EA','',6,0,0.0,'5/27/2024'::date)
,('test','X123456','CT','Quot',0,0,144.1,'5/15/2024')-----------
,('test','CX23456','PNL','',0,0,0,'4/21/2024')
,('lvlholder','ABC1234','EA','',0,3,44.9,'07/11/23')
,('lvlholder','ABC1234','EA','',0,3,44.9,'11/30/2023')
,('lvlholder','ABC1234','EA','',0,5,41.38,'07/11/23')
,('lvlholder','ABC1234','EA','',0,5,41.38,'11/30/2023')
,('lvlholder','ABC1234','EA','',0,2,34.81,'1/15/2024')
,('lvlholder','ABC1234','EA','',0,5,30.49,'1/15/2024')
,('lvlholder','ABC1234','EA','',0,8,28.39,'1/15/2024')
,('lvlholder','ABC1234','EA','',0,2,49.89,'07/11/23')
,('lvlholder','ABC1234','EA','',0,2,49.89,'11/30/2023')
,('lvlholder','ABC1234','EA','',0,7,37.24,'07/11/23')
,('lvlholder','ABC1234','EA','',0,7,37.24,'11/30/2023')
,('lvlholder','ABC1234','EA','',0,4,31.43,'1/15/2024')
,('lvlholder','ABC1234','EA','',0,7,28.97,'1/15/2024')
,('lvlholder','ABC1234','EA','',0,1,62.36,'07/11/23')
,('lvlholder','ABC1234','EA','',0,1,62.36,'11/30/2023')
,('lvlholder','ABC1234','EA','',0,8,36.5,'07/11/23')
,('lvlholder','ABC1234','EA','',0,8,36.5,'11/30/2023')
,('lvlholder','ABC1234','EA','',0,1,38.65,'1/15/2024')
,('lvlholder','ABC1234','EA','',0,3,33.05,'1/15/2024')
,('lvlholder','ABC1234','EA','',0,6,29.57,'1/15/2024')------------
,('lvlholder','ABC1234','EA','',0,4,42.66,'07/11/23')
,('lvlholder','ABC1234','EA','',0,4,42.66,'11/30/2023')
,('lvlholder','ABC1234','EA','',0,6,40.13,'07/11/23')
,('lvlholder','ABC1234','EA','',0,6,40.13,'11/30/2023')
,('lvlholder','X123456','CT','',0,1,251.68,'01/01/53')
,('lvlholder','X123456','CT','',0,2,201.52,'01/01/53')
,('lvlholder','X123456','CT','',0,3,191.4,'01/01/53')
,('lvlholder','X123456','CT','',0,4,181.72,'01/01/53')
,('lvlholder','X123456','CT','',0,5,176.22,'01/01/53')
,('lvlholder','X123456','CT','',0,6,170.94,'01/01/53')
,('lvlholder','X123456','CT','',0,7,165.88,'01/01/53')
,('lvlholder','X123456','CT','',0,8,156.63,'01/01/53')
,('lvlholder','BR23456','PC','',0,5,16.23,'07/05/23')
,('lvlholder','BR23456','PC','',0,5,16.23,'11/30/2023')
,('lvlholder','BR23456','PC','',0,8,14.82,'07/05/23')
,('lvlholder','BR23456','PC','',0,8,14.82,'11/30/2023')
,('lvlholder','BR23456','PC','',0,1,23.51,'07/05/23')
,('lvlholder','BR23456','PC','',0,1,23.51,'11/30/2023')
,('lvlholder','BR23456','PC','',0,3,17.99,'07/05/23')
,('lvlholder','BR23456','PC','',0,3,17.99,'11/30/2023')
,('lvlholder','BR23456','PC','',0,4,17.09,'07/05/23')
,('lvlholder','BR23456','PC','',0,4,17.09,'11/30/2023')
,('lvlholder','BR23456','PC','',0,3,18.89,'01/01/24')
,('lvlholder','BR23456','PC','',0,4,17.94,'01/01/24')
,('lvlholder','BR23456','PC','',0,5,17.04,'01/01/24')
,('lvlholder','BR23456','PC','',0,6,16.54,'01/01/24')
,('lvlholder','BR23456','PC','',0,7,16.04,'01/01/24')
,('lvlholder','BR23456','PC','',0,8,15.56,'01/01/24')--------
,('lvlholder','BR23456','PC','',0,2,19.99,'07/05/23')
,('lvlholder','BR23456','PC','',0,2,19.99,'11/30/2023')
,('lvlholder','BR23456','PC','',0,7,15.28,'07/05/23')
,('lvlholder','BR23456','PC','',0,7,15.28,'11/30/2023')
,('lvlholder','BR23456','PC','',0,1,24.69,'01/01/24')
,('lvlholder','BR23456','PC','',0,2,20.99,'01/01/24')
,('lvlholder','BR23456','PC','',0,6,15.75,'07/05/23')
,('lvlholder','BR23456','PC','',0,6,15.75,'11/30/2023')
,('lvlholder','CX23456','PNL','',0,1,24.38,'01/01/24')
,('lvlholder','CX23456','PNL','',0,2,19.74,'07/05/23')
,('lvlholder','CX23456','PNL','',0,2,19.74,'11/30/2023')
,('lvlholder','CX23456','PNL','',0,5,16.03,'07/05/23')
,('lvlholder','CX23456','PNL','',0,5,16.03,'11/30/2023')
,('lvlholder','CX23456','PNL','',0,7,15.09,'07/05/23')
,('lvlholder','CX23456','PNL','',0,7,15.09,'11/30/2023')
,('lvlholder','CX23456','PNL','',0,2,20.73,'01/01/24')
,('lvlholder','CX23456','PNL','',0,3,18.66,'01/01/24')
,('lvlholder','CX23456','PNL','',0,4,17.72,'01/01/24')
,('lvlholder','CX23456','PNL','',0,5,16.83,'01/01/24')
,('lvlholder','CX23456','PNL','',0,6,16.33,'01/01/24')
,('lvlholder','CX23456','PNL','',0,7,15.84,'01/01/24')
,('lvlholder','CX23456','PNL','',0,8,15.36,'01/01/24')------
,('lvlholder','CX23456','PNL','',0,8,14.63,'07/05/23')
,('lvlholder','CX23456','PNL','',0,8,14.63,'11/30/2023')
,('lvlholder','CX23456','PNL','',0,1,23.22,'07/05/23')
,('lvlholder','CX23456','PNL','',0,1,23.22,'11/30/2023')
,('lvlholder','CX23456','PNL','',0,3,17.77,'07/05/23')
,('lvlholder','CX23456','PNL','',0,3,17.77,'11/30/2023')
,('lvlholder','CX23456','PNL','',0,4,16.88,'07/05/23')
,('lvlholder','CX23456','PNL','',0,4,16.88,'11/30/2023')
,('lvlholder','CX23456','PNL','',0,6,15.55,'07/05/23')
,('lvlholder','CX23456','PNL','',0,6,15.55,'11/30/2023')
;
谢谢
由于您的之前的问题标记了 SQL Server,我将在这里假设相同。
您无法从
IN (...)
表达式中提取不匹配的值。您可以将所选的itemcode
列表定义为单独的源表,或者在VALUES
子选择中定义,您可以LEFT JOIN
到您的prices
表。
原始代码片段:
FROM pricing P
WHERE P.Customer ='test'
and P.itemcode in ('ABC1234','X123456','BR23456','CX23456')
然后会变成这样:
FROM (
VALUES ('ABC1234'), ('X123456'), ('BR23456'), ('CX23456')
) I(itemcode)
LEFT JOIN pricing P
ON P.Customer = 'test'
AND P.itemcode = I.itemcode
然后修改查询的其余部分以处理由不匹配的左连接产生的空值。对
p.itemcode
的引用将更改为 i.itemcode
。 ISNULL()
或 COALESCE()
可用于其他列。
将此应用到我之前基于
OUTER APPLY
的答案(此处),您将得到:
DECLARE @DefaultCustomerLevel INT = 8 -- This should come from a customer table
SELECT
I.itemcode,
COALESCE(PLookup.uom, P.uom) AS uom,
ISNULL(P.price, 0) AS price,
COALESCE(PLookup.start_date, P.start_date) AS start_date,
COALESCE(PLookup.price, P.price) AS LevelResult
FROM (
VALUES ('ABC1234'), ('X123456'), ('BR23456'), ('CX23456')
) I(itemcode)
LEFT JOIN pricing P
ON P.Customer = 'test'
AND P.itemcode = I.itemcode
OUTER APPLY (
SELECT TOP 1 *
FROM pricing P1
WHERE ISNULL(P.trtype, '') = ''
AND P1.customer = 'lvlholder'
AND P1.itemcode = I.itemcode -- Maybe
AND P1.Blvl = COALESCE(NULLIF(P.Alvl, 0), @DefaultCustomerLevel)
--AND P1.start_date <= COALESCE(P.start_date, GETDATE()) -- Maybe
ORDER BY P1.start_date DESC
) PLookup
结果:
商品代码 | 乌姆 | 价格 | 开始日期 | 等级结果 |
---|---|---|---|---|
ABC1234 | EA | 0 | 2024-01-15 | 29.57 |
X123456 | CT | 144.1 | 2024-05-15 | 144.1 |
BR23456 | 电脑 | 0 | 2024-01-01 | 15.56 |
CX23456 | PNL | 0 | 2024-01-01 | 15.36 |
可以对 BartMcEndree 的答案进行类似的修改,但您可能需要在结果中添加另一个子选择。
请参阅 this db<>fiddle 进行演示。