计算促销最优组合方案

--1.建表
CREATE TABLE #A
(
id INT IDENTITY(1,1),
model VARCHAR(10),
price DECIMAL(18,2)
)
--2.插入数据
INSERT INTO #A
SELECT 'A',140
UNION
SELECT 'B',130
UNION
SELECT 'C',80
UNION
SELECT 'D',70
UNION
SELECT 'E',60
UNION
SELECT 'F',50
UNION
SELECT 'G',40
--3.查询
;WITH ct
AS
(
	SELECT model, CONVERT(VARCHAR(max),model) AS models,price,CONVERT(INT,price/200) AS Z  FROM #A 
	UNION ALL
	SELECT b.model,a.models+'+'+b.model,CONVERT(DECIMAL(18,2),a.price+b.price),
	CONVERT(INT,(a.price+b.price)/200) AS Z
	FROM ct A INNER JOIN #A B  ON CHARINDEX(b.model,models)=0 AND a.model

型号 价格
A 140
B 130
C 80
D 70
E 60
F 50
G 40
规则:每满200送一件
如何计算出买哪些型号赠送哪些型号是最优的方案?

计算促销最优组合方案_第1张图片

你可能感兴趣的:(SQL,Server)