SQL - 最多3天销售量

时间:2018-01-23 18:03:06

标签: sql-server

我试图确定在日期范围内3天内销售的最大数量是多少。用户将提供日期范围(在此示例中,我们的值范围为2015-03-30至2015-04-05),我们想知道所有销售产品在该日期范围内任何给定3天期间的最大销售量在那个日期范围内。

使用下面的值我们可以看到,衬衫中等的日期范围为3-30到4-1,售出8件物品,日期范围为3-31到4-2,衬衫中等,售出6件物品,a衬衫中等4-1到4-3的范围,卖3 ...等等。

衬衫中等任何特定时期内销售的最大值为8。

CREATE TABLE #SalesDataTest ( Product NVARCHAR(MAX), Quantity INT, DateSold DATE);
INSERT #SalesDataTEST VALUES 
  ('Shirt Medium', 1, '2015-03-30')
, ('Shirt Medium', 1, '2015-03-30')
, ('Shirt Medium', 2, '2015-03-31')
, ('Shirt Medium', 1, '2015-03-31')
, ('Pants Large', 1, '2015-03-31')
, ('Shirt Medium', 2, '2015-03-31')
, ('Pants Large', 1, '2015-03-31')
, ('Shirt Medium', 1, '2015-04-01')
, ('Shirt Medium', 2, '2015-04-03')
, ('Shirt Medium', 1, '2015-04-05')
, ('Pants Large', 1, '2015-03-30')
, ('Pants Medium', 2, '2015-03-31')
, ('Pants Small', 5, '2015-03-31')

我希望看到的结果是

--Product--     --Max sold 3 day Window--
'Pants Small'   5
'Pants Medium'  2
'Pants Large'   3
'Shirt Medium'  8

如果我们可以包含每种产品的最佳3天销售窗口的开始日期和结束日期,则可获得奖励。

2 个答案:

答案 0 :(得分:1)

http://sqlfiddle.com/#!18/50a0a/10

SELECT Product, SUM(Quantity)
FROM SalesDataTest
WHERE DateSold BETWEEN '2015-03-30' AND '2015-04-01'
GROUP BY Product

答案 1 :(得分:0)

太长但工作:)

declare @SalesDataTest table (Product nvarchar(max), Quantity int, DateSold date);
insert @SalesDataTest values 
    ('Shirt Medium' , 1 , '2015-03-30'),
    ('Shirt Medium' , 1 , '2015-03-30'),
    ('Shirt Medium' , 2 , '2015-03-31'),
    ('Shirt Medium' , 1 , '2015-03-31'),
    ('Pants Large'  , 1 , '2015-03-31'),
    ('Shirt Medium' , 2 , '2015-03-31'),
    ('Pants Large'  , 1 , '2015-03-31'),
    ('Shirt Medium' , 1 , '2015-04-01'),
    ('Shirt Medium' , 2 , '2015-04-03'),
    ('Shirt Medium' , 1 , '2015-04-05'),
    ('Pants Large'  , 1 , '2015-03-30'),
    ('Pants Medium' , 2 , '2015-03-31'),
    ('Pants Small'  , 5 , '2015-03-31');

declare @MinDate date;
declare @DateDiff int;

select 
    @MinDate  = min(DateSold),
    @DateDiff = datediff(day, min(DateSold), max(DateSold)) 
from 
    @SalesDataTest;    

with Numbers as 
(
    select n from (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) t(n)    
),
Dates as
( 
    select top (@DateDiff + 1)
        CalendarDate = dateadd(day, row_number() over(order by (select 1)) - 1, @MinDate)
    from 
        Numbers n1, Numbers n2, Numbers n3
),
Products as
(
    select distinct
        Product
    from
        @SalesDataTest
),
NormalizedData as 
(
    select
        CalendarDate,
        p.Product,
        Quantity = sum(isnull(Quantity, 0))    
    from 
        Dates d cross join Products p  
        left join @SalesDataTest sdt on sdt.DateSold = d.CalendarDate and sdt.Product = p.Product
    group by
        CalendarDate,
        p.Product
), 
AggregatedData as
(
    select 
        Product,
        FirstDate = CalendarDate,        
        QuantitySum = Quantity 
                    + isnull(Lead(Quantity, 1) over( partition by Product order by CalendarDate) ,0)
                    + isnull(Lead(Quantity, 2) over( partition by Product order by CalendarDate) ,0)
    from 
        NormalizedData
)
select top 1 with ties
    Product,
    FirstDate,
    SecondDate = dateadd(day, 1, FirstDate), 
    ThirdDate = dateadd(day, 2, FirstDate), 
    QuantitySum
from 
    AggregatedData 
order by
    row_number() over(partition by Product order by QuantitySum desc);