自动对商品应用最合适的折扣套餐

发布于 2024-09-17 19:13:34 字数 494 浏览 7 评论 0原文

我创建了一些折扣套餐:

Package1(Item1,Item2,Item5) Discount-5%.
Package2(Item2,Item5,Item8) Discount-8% 
Package3(Item1,Item2) Discount3%.

当一个人在线购买商品时(例如,他购买商品1、商品2、商品5、商品10),当我向他显示总价时,我需要自动对商品应用最合适的折扣。

在上述情况下,两个折扣将适用于所述选择,但是套餐1折扣是最好的,因为它给一个人最大的好处...所以我需要自动应用它。

有没有人遇到过这种情况或者可以帮助我?

DiscountID DiscountName ItemIds Disc%

1 Package1 1,2,5 5
2 Package2 2,3,5 8
3 Package3 1,2 3

我随身携带了一个人选择的所有 ItemId。现在需要申请最合适的折扣..

感谢您的帮助/指导。

I have created some discount packages:

Package1(Item1,Item2,Item5) Discount-5%.
Package2(Item2,Item5,Item8) Discount-8% 
Package3(Item1,Item2) Discount3%.

When a Person buy Items Online(for Ex. he buys Item1,Item2,Item5, Item10), while I show him the total price, I need to apply the best fitted discount on the items automatically.

In the above case, two discounts would be applicable on said selection, however Package1 Discount is best as it give a person max benefit... so this I need apply this automatically.

Is there anyone who came across this type of scenario or anyone who could help me?

DiscountID DiscountName ItemIds Disc%

1 Package1 1,2,5 5
2 Package2 2,3,5 8
3 Package3 1,2 3

I have all the ItemId with me, which a Person selected. Now need to apply best fit discount..

Appreciating your help/ guidance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦醒灬来后我 2024-09-24 19:13:34

试试这个

-- Test tables
CREATE TABLE #Package(Name varchar(50), Discount decimal(10,2))
CREATE TABLE #PackageItem(PackageName varchar(50), ProductName varchar(50))
CREATE TABLE #ShoppingCart(ProductName varchar(50))

-- Test data
INSERT INTO #Package VALUES ('Package1', 0.05)
INSERT INTO #PackageItem VALUES ('Package1', 'Item1')
INSERT INTO #PackageItem VALUES ('Package1', 'Item2')
INSERT INTO #PackageItem VALUES ('Package1', 'Item5')
INSERT INTO #Package VALUES ('Package2', 0.08)
INSERT INTO #PackageItem VALUES ('Package2', 'Item1')
INSERT INTO #PackageItem VALUES ('Package2', 'Item5')
INSERT INTO #PackageItem VALUES ('Package2', 'Item8')
INSERT INTO #Package VALUES ('Package3', 0.03)
INSERT INTO #PackageItem VALUES ('Package3', 'Item1')
INSERT INTO #PackageItem VALUES ('Package3', 'Item2')

INSERT INTO #ShoppingCart VALUES ('Item1')
INSERT INTO #ShoppingCart VALUES ('Item2')
INSERT INTO #ShoppingCart VALUES ('Item5')
INSERT INTO #ShoppingCart VALUES ('Item10')

SELECT  TOP 1 *
FROM    (
    -- Join #ShoppingCart with PackageItem and count matched rows
    SELECT      #Package.Name, #Package.Discount,
                COUNT(#Package.Name) AS [Count]
    FROM        #ShoppingCart
    LEFT JOIN   #PackageItem 
                ON #PackageItem.ProductName = #ShoppingCart.ProductName
    LEFT JOIN   #Package ON #Package.Name = #PackageItem.PackageName
    GROUP BY    #Package.Name, #Package.Discount
        ) A
JOIN
        (
    -- Count how many products each package have
    SELECT      #Package.Name,
                COUNT(#Package.Name) AS [Count]
    FROM        #Package
    LEFT JOIN   #PackageItem ON #Package.Name = #PackageItem.PackageName
    GROUP BY    #Package.Name, #Package.Discount
        ) B
-- if same package contains same number of products, pick it
-- (so you can't have a same item twice in your cart;
--  but you probably already have a quantity column)
ON A.Name = B.Name AND A.[Count] = B.[Count]
-- just greater discount matters
ORDER BY A.Discount DESC

-- Clear test stuff
DROP TABLE #Package
DROP TABLE #PackageItem
DROP TABLE #ShoppingCart

Try this

-- Test tables
CREATE TABLE #Package(Name varchar(50), Discount decimal(10,2))
CREATE TABLE #PackageItem(PackageName varchar(50), ProductName varchar(50))
CREATE TABLE #ShoppingCart(ProductName varchar(50))

-- Test data
INSERT INTO #Package VALUES ('Package1', 0.05)
INSERT INTO #PackageItem VALUES ('Package1', 'Item1')
INSERT INTO #PackageItem VALUES ('Package1', 'Item2')
INSERT INTO #PackageItem VALUES ('Package1', 'Item5')
INSERT INTO #Package VALUES ('Package2', 0.08)
INSERT INTO #PackageItem VALUES ('Package2', 'Item1')
INSERT INTO #PackageItem VALUES ('Package2', 'Item5')
INSERT INTO #PackageItem VALUES ('Package2', 'Item8')
INSERT INTO #Package VALUES ('Package3', 0.03)
INSERT INTO #PackageItem VALUES ('Package3', 'Item1')
INSERT INTO #PackageItem VALUES ('Package3', 'Item2')

INSERT INTO #ShoppingCart VALUES ('Item1')
INSERT INTO #ShoppingCart VALUES ('Item2')
INSERT INTO #ShoppingCart VALUES ('Item5')
INSERT INTO #ShoppingCart VALUES ('Item10')

SELECT  TOP 1 *
FROM    (
    -- Join #ShoppingCart with PackageItem and count matched rows
    SELECT      #Package.Name, #Package.Discount,
                COUNT(#Package.Name) AS [Count]
    FROM        #ShoppingCart
    LEFT JOIN   #PackageItem 
                ON #PackageItem.ProductName = #ShoppingCart.ProductName
    LEFT JOIN   #Package ON #Package.Name = #PackageItem.PackageName
    GROUP BY    #Package.Name, #Package.Discount
        ) A
JOIN
        (
    -- Count how many products each package have
    SELECT      #Package.Name,
                COUNT(#Package.Name) AS [Count]
    FROM        #Package
    LEFT JOIN   #PackageItem ON #Package.Name = #PackageItem.PackageName
    GROUP BY    #Package.Name, #Package.Discount
        ) B
-- if same package contains same number of products, pick it
-- (so you can't have a same item twice in your cart;
--  but you probably already have a quantity column)
ON A.Name = B.Name AND A.[Count] = B.[Count]
-- just greater discount matters
ORDER BY A.Discount DESC

-- Clear test stuff
DROP TABLE #Package
DROP TABLE #PackageItem
DROP TABLE #ShoppingCart
爱的那么颓废 2024-09-24 19:13:34

您需要将每组购买的商品应用于每个套餐,如果不符合条件,则返回折扣百分比或零,然后查询这些结果以获取您的套餐组中的最大折扣。

You need to apply each set of purchased items to each package, either returning the discount %, or zero, if they don't qualify, then query those results for the max discount from your set of packages.

扛刀软妹 2024-09-24 19:13:34

乍一看,这似乎是背包问题的一个实例,它是NP-hard

本文似乎解决了与您类似的问题。

暴力解决方案

一种暴力解决方案是将折扣套餐的每个有效组合应用于订单。

想象一棵树,其中对于给定节点,每个祖先代表已应用于订单的折扣包,其每个子代代表可应用于订单中其余商品的有效折扣包。

当没有更多的包裹可以应用于订单时,节点是叶子。

如果您有大量商品并且提供超过 1 个折扣套餐,我不会推荐您这样做。

At first glance, it seems like this is an instance of the Knapsack Problem, which is NP-hard.

This paper seems to address a similar problem to yours.

Brute Force Solution

One brute force solution would be to apply every valid combination of discount packages to the order.

Picture a tree where, for a given node, each ancestor represents a discount package that has already been applied to the order, and each of its children represent a valid discount package that can be applied to the remaining items in the order.

A node is a leaf when no more packages can be applied to the order.

I wouldn't recommend this if you have a large number of items and more than 1 discount package being offered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文