计算' n'周数和周数年

时间:2016-11-24 06:45:47

标签: sql sql-server sql-server-2008

任何人都知道如何计算' n'周数(可能是周数或日期)来自周数。我知道年份和周数?我将在SQL服务器中执行此操作。

例如,我有周数,即3和年,即2016年,我必须计算最近13周的平均利润。

我添加了脚本&预期结果 -

CREATE TABLE [dbo].[Sales](
    [SalesID] [int] IDENTITY(1,1) NOT NULL,
    [SalesPersonID] [int] NOT NULL,
    [CustomerID] [int] NOT NULL,
    [ProductID] [int] NOT NULL,
    [profit] [int] NOT NULL,
    Year int Not Null,
    Quarter int Not Null,
    Week int Not Null,
 CONSTRAINT [SalesPK] PRIMARY KEY CLUSTERED 
(
    [SalesID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

INSERT INTO [Sales] ([SalesPersonID],[CustomerID],[ProductID],[profit],[Year],[Quarter],[week]) VALUES (17, 10482,  500,    500,2016,1,3)
INSERT INTO [Sales] ([SalesPersonID],[CustomerID],[ProductID],[profit],[Year],[Quarter],[week]) VALUES (5,  1964,   500 ,   810,2016,1,2)   
INSERT INTO [Sales] ([SalesPersonID],[CustomerID],[ProductID],[profit],[Year],[Quarter],[week]) VALUES (8,  12300,  500,    123,2016,1,1)
INSERT INTO [Sales] ([SalesPersonID],[CustomerID],[ProductID],[profit],[Year],[Quarter],[week]) VALUES (1,  4182,   500,    437,2015,4,52)
INSERT INTO [Sales] ([SalesPersonID],[CustomerID],[ProductID],[profit],[Year],[Quarter],[week]) VALUES (14, 15723,  500,    750,2015,4,51)
INSERT INTO [Sales] ([SalesPersonID],[CustomerID],[ProductID],[profit],[Year],[Quarter],[week]) VALUES (14, 6000,   500,        60, 2015,4,50)
INSERT INTO [Sales] ([SalesPersonID],[CustomerID],[ProductID],[profit],[Year],[Quarter],[week]) VALUES (5,  17541,  500,    373,2015,4,49)
INSERT INTO [Sales] ([SalesPersonID],[CustomerID],[ProductID],[profit],[Year],[Quarter],[week]) VALUES (20, 9423,   500,    687,2015,4,48)
INSERT INTO [Sales] ([SalesPersonID],[CustomerID],[ProductID],[profit],[Year],[Quarter],[week]) VALUES (1,  1,      500,        10, 2015,4,47)
INSERT INTO [Sales] ([SalesPersonID],[CustomerID],[ProductID],[profit],[Year],[Quarter],[week]) VALUES (11, 11241,  500,    310,2015,4,46)
INSERT INTO [Sales] ([SalesPersonID],[CustomerID],[ProductID],[profit],[Year],[Quarter],[week]) VALUES (2,  3023,   500,    623,2015,4,45)
INSERT INTO [Sales] ([SalesPersonID],[CustomerID],[ProductID],[profit],[Year],[Quarter],[week]) VALUES (17, 14664,  500,    937,2015,4,44)
INSERT INTO [Sales] ([SalesPersonID],[CustomerID],[ProductID],[profit],[Year],[Quarter],[week]) VALUES (20, 5241,   500,    250,2015,4,43)
INSERT INTO [Sales] ([SalesPersonID],[CustomerID],[ProductID],[profit],[Year],[Quarter],[week]) VALUES (8,  16482,  500,        560,2015,4,42)
INSERT INTO [Sales] ([SalesPersonID],[CustomerID],[ProductID],[profit],[Year],[Quarter],[week]) VALUES (22, 8264,   500,    873,2015,4,41)


select * from sales

---预期结果

因此,如果计算2016年第3周的最后13周平均利润 然后它应该将利润带到2015年第43周,并计算利润的平均值。

所以结果看起来像 -

ProductID| Avg Profit
500|451.53

提前致谢。

1 个答案:

答案 0 :(得分:0)

例如 让我们考虑当前年和当周。

所以年份将是2016年,周数将是48.将这些值传递给语句以获得结果。

DECLARE @YEAR VARCHAR(10)='2016', @WEEK INT = 48

--For the given week(48) of year(2016) Start datetime of week value: (Assumed Sunday is week start Day)

SELECT DATEADD(wk,DATEDIFF(wk,cast( 6 as DATETIME), @YEAR+'-01-01' ) + @WEEK-1,CAST( 6 AS DATETIME));

--For the given week(48) of year(2016) End datetime of week value:
SELECT DATEADD(wk,DATEDIFF(wk,CAST( 5 AS DATETIME), @YEAR+'-01-01' ) + @WEEK-1,CAST( 5 AS DATETIME));

结果将是

'2016-11-20 00:00:00.000'  --48th week start day (Sun Day)
'2016-11-26 00:00:00.000'  --48th week end day (Sat Day)

根据您的要求只需减去您需要返回的周数。

DECLARE @YEAR VARCHAR(10)='2016', @WEEK INT = 3 , @WEEKFROM INT 
SET @WEEKFROM = @WEEK-13
--13 weeks back week start date of (2016 year, 3rd week)
SELECT DATEADD(wk,DATEDIFF(wk,CAST(6 AS DATETIME), @YEAR+'-01-01' )+ @WEEKFROM-1,CAST(6 AS DATETIME));

--end datetime of given week (2016 year, 3rd week)
SELECT DATEADD(wk,DATEDIFF(wk,CAST(5 AS DATETIME), @YEAR+'-01-01' )+ @WEEK-1,CAST(5 AS DATETIME));

上面的输出是(使用这些日期时间,您可以在表中查询这些周的利润)。

'2015-10-11 00:00:00.000' --from date
'2016-01-16 00:00:00.000' --to date

它是如何工作的?

尝试将Numbers转换为日期时间

SELECT CAST(0 AS datetime)  --1900-01-01 00:00:00.000
SELECT CAST(1 AS datetime)  --1900-01-02 00:00:00.000
.
.
SELECT CAST(5 AS datetime)  --1900-01-06 00:00:00.000 Sat Day
SELECT CAST(6 AS datetime)  --1900-01-07 00:00:00.000 Sun Day

enter image description here

现在使用'1900-01-07 00:00:00.000'

计算'2016-01-01'DateDiff之间的周数
select DATEDIFF(wk,CAST(6 AS DATETIME), @YEAR+'-01-01' )+ @WEEKFROM-1  ---> 6040

现在使用1900-01-07

将其添加到DATEADD
SELECT DATEADD(wk,DATEDIFF(wk,CAST(6 AS DATETIME), @YEAR+'-01-01' )+ @WEEKFROM-1,CAST(6 AS DATETIME));

Result: 2015-10-11 00:00:00.000