Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算区间折扣的SQL查询_Sql_Sql Server - Fatal编程技术网

计算区间折扣的SQL查询

计算区间折扣的SQL查询,sql,sql-server,Sql,Sql Server,我很难理解如何用T-SQL查询解决这个问题。 我有一个价格栏和一个数量栏。在另一个表中,a有不同数量级别的折扣。所以我的折扣表可以有如下值 (StartLevel, DiscountFactor) (0, 1); (25, 0.95); (50, 0.90); (100, 0.75) 我想要的是计算一个总价。如果体积是35,我希望它乘以 Price x ((35-25) x 0.95 + (25-0) x 1) 如果卷为200,则应为 Price x ((200-100) x 0.75 +

我很难理解如何用T-SQL查询解决这个问题。 我有一个价格栏和一个数量栏。在另一个表中,a有不同数量级别的折扣。所以我的折扣表可以有如下值

(StartLevel, DiscountFactor)
(0, 1);
(25, 0.95);
(50, 0.90);
(100, 0.75)
我想要的是计算一个总价。如果体积是35,我希望它乘以

Price x ((35-25) x 0.95 + (25-0) x 1)
如果卷为200,则应为

Price x ((200-100) x 0.75 + (100-50) x .9+(50-25) x .95+(25) x 1)

有人能帮我解决这个问题吗

对于给定的
数量
价格
,您可以使用SQL Server 2012+及以后版本中提供的
LEAD
获得基于时间间隔的折扣

样本数据

DECLARE @PriceTable TABLE(Volume INT,Price DECIMAL(9,2) )

DECLARE @Discount TABLE(StartLevel int, DiscountFactor DECIMAL(9,2))


INSERT INTO @PriceTable
VALUES(75, 20.5),
(150, 20),
(250, 20.5),
(0, 15);


INSERT INTO @Discount
VALUES(0, 1),
(25, 0.95),
(50, 0.90),
(100, 0.75);
查询

SELECT Volume,Price,FinalPrice
FROM @PriceTable P
CROSS APPLY(
SELECT SUM(CASE WHEN (MaxLevel >=StartLevel) THEN (MaxLevel-StartLevel) ELSE 0 END *DiscountFactor)*P.Price as FinalPrice
FROM 
(
SELECT CASE WHEN LEAD(StartLevel)OVER(ORDER BY StartLevel) < P.Volume THEN LEAD(StartLevel)OVER(ORDER BY StartLevel) ELSE P.Volume END MaxLevel,StartLevel, DiscountFactor
FROM @Discount
) IQ
)T
这有助于:

DECLARE @products TABLE
    (
      id INT ,
      price MONEY ,
      volume INT
    )
DECLARE @discounts TABLE
    (
      id INT ,
      Level INT ,
      Factor MONEY
    )

INSERT  INTO @products
VALUES  ( 1, 10, 35 ),
        ( 2, 15, 200 )

INSERT  INTO @discounts
VALUES  ( 1, 0, 1 ),
        ( 2, 25, 0.95 ),
        ( 3, 50, 0.90 ),
        ( 4, 100, 0.75 )



SELECT  p.id, p.price * SUM(ca.m)
FROM    @products p
        CROSS APPLY ( SELECT    * ,
                                Factor * ( -Level + LEAD(Level) OVER ( PARTITION BY p.id ORDER BY Level, d ) ) AS m
                      FROM      ( SELECT    1 AS d ,
                                            Level ,
                                            Factor
                                  FROM      @discounts
                                  WHERE     Level < p.volume
                                  UNION ALL
                                  SELECT    2 AS d ,
                                            p.volume ,
                                            0
                                ) t
                    ) ca
GROUP BY p.id, p.price 
然后,只需将
乘积和
m的
分组,即可得出:

id  Total
1   345.00
2   2531.25

还有一些样本数据,我还没有得到任何有效的结果。我在想一个交叉应用的方法。但我遇到的问题是,当我将列数拆分为每个级别的折扣时。MSSQL 2012或2014是我的版本我可能误解了sql版本。如果版本为mssql 2008,是否有不包含“LEAD”的解决方案
id  price   volume  d   Level   Factor  m
1   10.00   35      1   0       1.00    25.00
1   10.00   35      1   25      0.95    9.50
1   10.00   35      2   35      0.00    NULL
2   15.00   200     1   0       1.00    25.00
2   15.00   200     1   25      0.95    23.75
2   15.00   200     1   50      0.90    45.00
2   15.00   200     1   100     0.75    75.00
2   15.00   200     2   200     0.00    NULL
id  Total
1   345.00
2   2531.25