SQL - 在哪个商店查询中查找哪个产品的销售速度更快

时间:2016-12-07 09:55:19

标签: sql sql-server

我在表格中有以下数据,其中记录了所有分支的所有销售额。我需要弄清楚哪家商店比其他商店更快销售哪种商品,以便我们决定将哪种商品发送到哪家商店

示例数据:

Ref    | Colour | Size | Branch | QtySold | DatePosted
012345 | BLU    | 01   | ABC    | 1       | 2016-08-01 17:37:29.310 
012345 | BLU    | 01   | CFR    | 1       | 2016-08-01 17:37:29.310
012345 | BLU    | 01   | CFD    | 2       | 2016-08-01 17:37:29.310
021456 | YEL    | 06   | ABC    | 5       | 2016-08-02 17:37:29.310
021456 | YEL    | 06   | AMC    | 2       | 2016-08-02 15:37:29.310

我写了以下SP,它接受FromDate和ToDate以及其他参数。

ALTER PROCEDURE [dbo].[GetSalesByDate] 
(
  @FromDate datetime = NULL, 
  @ToDate datetime = NULL,
  @Ref nvarchar(MAX) = NULL, 
  @ColourCode nvarchar(20) = NULL,
  @StoreCode nvarchar(6) = NULL,
  @SizeIndex nvarchar(3) = NULL
)

AS
BEGIN

SELECT
YEAR(S.DatePosted)  'Year', DATEPART(week,S.DatePosted ) NumberOfTheWeek,
S.Ref, 
S.Colour ColourCode, 
S.Sno SizeIndex, 
S.Branch StoreCode,
DATEDIFF(day,@FromDate, @ToDate) TotalDays,
SUM(S.Qty) QtyPerUnit

FROM SalesHistory S

WHERE 
(S.DatePosted BETWEEN @FromDate AND @ToDate)
AND (S.Ref IN (SELECT * from dbo.SplitList(@Ref, ',')) OR @Ref IS null)
AND (S.Colour = @ColourCode OR @ColourCode IS null)
AND (S.Sno = @SizeIndex OR @SizeIndex IS null)

AND (S.Branch = @StoreCode OR @StoreCode IS NULL)

GROUP BY 
S.Branch, YEAR(S.DatePosted), DATEPART(week,S.DatePosted),S.Ref,S.Colour,S.Sno

ORDER BY 
S.Ref, S.Colour

我觉得上面的SP没有回复我想要的东西...... 我想比较每个分支中每个参考,颜色,尺寸(产品)的销售额

  1. 根据FromDate和ToDate,返回该期间的数据,并将其与上周和去年同一周进行比较。
  2. 请有人帮忙吗?

    结果应该是这样的。

    Year | No.OfThheWeek | Ref    | Colour | Size | Branch | TotalQtySoldForThePeriod | LastWeekTotalQtySold    | LastYearTotalQtySold
    2016 |  32           | 012345 | BLU    | 01   | ABC    |    125                 |  205                      | 310
    2016 |  33           | 012345 | BLU    | 01   | ABC    |    55                  |  155                      | 214
    2016 |  33           | 021456 | YEL    | 06   | AMC    |    74                  |  46                       | 83
    

2 个答案:

答案 0 :(得分:1)

首先,您应该更正当前的查询。 Group By这么多列是alwys wrong.Group由anID列更可取,然后你可以加入CTE来获取其他列名。

AnyWay我假设您当前的查询输出正确,那么您可以继续这样做。 注意:这是为了理解

declare @Reftbl table(ref varchar(20))
insert into @Reftbl
SELECT col1 from dbo.SplitList(@Ref, ',')

 declare @FromDate datetime = '2016-08-01' ,  @ToDate datetime = '2016-08-31'
 declare @lastWeekFrom datetime=dateadd(day,-7,@FromDate)
 declare @lastWeekTo datetime=dateadd(day,6,@lastWeekFrom)
 declare @lastYearSameWeekFrom datetime=dateadd(year,-1,@FromDate)
 declare @lastYearSameWeekFromTo datetime=dateadd(day,6,@lastYearSameWeekFrom)

 select @lastWeekFrom , @lastWeekTo,@lastYearSameWeekFrom,@lastYearSameWeekFromTo
 ;with CurrentCTE
 (
  SELECT
YEAR(S.DatePosted)  'Year', DATEPART(week,S.DatePosted ) NumberOfTheWeek,
S.Ref, 
S.Colour ColourCode, 
S.Sno SizeIndex, 
S.Branch StoreCode,
DATEDIFF(day,@FromDate, @ToDate) TotalDays,
SUM(S.Qty) QtyPerUnit

FROM SalesHistory S

WHERE 
(S.DatePosted BETWEEN @FromDate AND @ToDate)
AND (S.Ref IN (SELECT ref from @Reftbl) OR @Ref IS null)
AND (S.Colour = @ColourCode OR @ColourCode IS null)
AND (S.Sno = @SizeIndex OR @SizeIndex IS null)

AND (S.Branch = @StoreCode OR @StoreCode IS NULL)

GROUP BY 
S.Branch, YEAR(S.DatePosted), DATEPART(week,S.DatePosted),S.Ref,S.Colour,S.Sno

--ORDER BY 
--S.Ref, S.Colour
 )
 ,LastWeekCTE
 (
   SELECT

S.Ref, 
S.Colour ColourCode, 
S.Sno SizeIndex, 
S.Branch StoreCode,
SUM(S.Qty) LastWeekTotalQtySold

FROM dbo.SalesHistory S

WHERE 
(S.DatePosted BETWEEN @lastWeekFrom AND @lastWeekTo)
AND (S.Ref IN (SELECT ref from @Reftbl) OR @Ref IS null)
AND (S.Colour = @ColourCode OR @ColourCode IS null)
AND (S.Sno = @SizeIndex OR @SizeIndex IS null)

AND (S.Branch = @StoreCode OR @StoreCode IS NULL)

GROUP BY 
S.Branch, S.Ref,S.Colour,S.Sno
 )
 ,LastYearSameWeekCTE
 (
  SELECT

S.Ref, 
S.Colour ColourCode, 
S.Sno SizeIndex, 
S.Branch StoreCode,
SUM(S.Qty) LastYearTotalQtySold

FROM dbo.SalesHistory S

WHERE 
(S.DatePosted BETWEEN @lastYearSameWeekFrom AND @lastYearSameWeekFromTo)
AND (S.Ref IN (SELECT ref from @Reftbl) OR @Ref IS null)
AND (S.Colour = @ColourCode OR @ColourCode IS null)
AND (S.Sno = @SizeIndex OR @SizeIndex IS null)

AND (S.Branch = @StoreCode OR @StoreCode IS NULL)

GROUP BY 
S.Branch, S.Ref,S.Colour,S.Sno
 )

 select c.*,c1.LastWeekTotalQtySold
 ,c2.LastYearTotalQtySold
  from 
 CurrentCTE C
 inner join 
 LastWeekCTE C1 on c.somecol=c1.somecol
 inner join LastYearSameWeekCTE c2 on c.somecol=c2.somecol

你可以看到使用table @ Reftbl.so,你不必多次使用split函数。

答案 1 :(得分:1)

首先:@FromDate和@ToDate没什么意义,因为显然他们应该只有一个星期,你怎么可能比较它与上周和去年同一周#34;?我想"最后"意味着"之前"在这里,我们正在考虑一些给定的七天,前一天的七天和七天。最好是忽略@ToDate,只需将7天添加到@FromDate,以确保谈论七天。

以下是一种方法:

SELECT 
  ref, colour, size, branch, 
  SUM(CASE WHEN dateposted BETWEEN @FromDate 
                           AND DATEADD(day, 7, @FromDate) 
           THEN qty 
      END) AS this_week_qty,
  SUM(CASE WHEN dateposted BETWEEN DATEADD(day, -8, @FromDate) 
                           AND DATEADD(day, -1, @FromDate)
           THEN qty 
      END) AS previous_week_qty,
  SUM(CASE WHEN dateposted BETWEEN DATEADD(year, -1, @FromDate) 
                                   AND DATEADD(day, 7, DATEADD(year, -1, @FromDate)) 
           THEN qty 
      END) AS previous_year_week_qty
sum(qty) as sum_qty 
FROM saleshistory 
WHERE dateposted BETWEEN @FromDate 
                 AND DATEADD(day, 7, @FromDate) -- requested week
   OR dateposted BETWEEN DATEADD(day, -8, @FromDate) 
                 AND DATEADD(day, -1, @FromDate) -- previous week
   OR dateposted BETWEEN DATEADD(year, -1, @FromDate) 
                 AND DATEADD(day, 7, DATEADD(year, -1, @FromDate)) -- week in previous year
GROUP BY ref, colour, size, branch;