SQL Server - 周末结束

时间:2016-01-14 19:36:34

标签: sql sql-server date

我有以下可用的T-SQL代码,但我认为可能会减少但不确定如何。

SELECT 
    datepart(YEAR, CONVERT(DATE, GETDATE())) AS 'Year', 
    Cast(Datepart(month, DATEADD (D, -1 * DatePart (DW, getdate()) + 7, getdate())) AS VARCHAR) + '-' + Cast(Datepart(DAY, DATEADD (D, -1 * DatePart (DW, getdate()) + 7, getdate()))AS VARCHAR) [Week Ending]

我只是希望以另一列中的年份格式结束这一周,以便我可以按年份分组,然后按周结束。可以帮助一些。

2 个答案:

答案 0 :(得分:0)

你不能这样做吗?:

DATEADD(DAY, 7 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [WeekEnd]

没有必要在单独的专栏中将年份分开。

编辑:我不确定你的意见是什么意思。这打印出日期和日期格式而不是varchar - 不是周数,而是您的评论建议。

的示例:

DECLARE @dateTime datetime
SET     @dateTime = '2016-01-01'
select  DATEADD(DAY, 7 - DATEPART(WEEKDAY, @dateTime), CAST(@dateTime AS DATE)) [WeekEnd]
  

2016年1月2日

DECLARE @dateTime datetime
SET     @dateTime = '2016-01-04'
select  DATEADD(DAY, 7 - DATEPART(WEEKDAY, @dateTime), CAST(@dateTime AS DATE)) [WeekEnd]
  

2016年1月9日

使用样本:

DECLARE @table TABLE (orderDate datetime, orderAmount float)
INSERT INTO @table
(
    orderDate,
    orderAmount
)
SELECT '2015-01-02', 500
union all SELECT '2015-01-04', 500
union all SELECT '2015-01-05', 500
union all SELECT '2015-01-05', 500
union all SELECT '2015-01-06', 500
union ALL SELECT '2015-01-11', 400
union all SELECT '2016-01-01', 500
union all SELECT '2016-01-02', 500
union all SELECT '2016-01-04', 500
union all SELECT '2016-01-05', 500
union all SELECT '2016-01-05', 500
union all SELECT '2016-01-06', 500
UNION ALL SELECT '2016-01-11', 400
UNION ALL SELECT '2016-12-11', 1200

每个日期的订单金额:

SELECT  
    orderDate, 
    sum(orderAmount) AS orderSumForGrouping, 
    count(1) AS numberOfOrdersWithinGrouping
FROM    @table o
GROUP   BY orderDate

输出:

orderDate   orderSumForGrouping numberOfOrdersWithinGrouping
2015-01-02 00:00:00.000 500 1
2015-01-04 00:00:00.000 500 1
2015-01-05 00:00:00.000 1000    2
2015-01-06 00:00:00.000 500 1
2015-01-11 00:00:00.000 400 1
2016-01-01 00:00:00.000 500 1
2016-01-02 00:00:00.000 500 1
2016-01-04 00:00:00.000 500 1
2016-01-05 00:00:00.000 1000    2
2016-01-06 00:00:00.000 500 1
2016-01-11 00:00:00.000 400 1
2016-12-11 00:00:00.000 1200    1

按年份分组的订单金额:

SELECT  
    year(orderDate) AS orderYear, 
    sum(orderAmount) AS orderSumForGrouping, 
    count(1) AS numberOfOrdersWithinGrouping
FROM    @table o
GROUP   BY year(orderDate)

输出:

orderYear   orderSumForGrouping numberOfOrdersWithinGrouping
2015    2900    6
2016    4600    8

按周末日期分组的订单金额:

SELECT  
    DATEADD(DAY, 7 - DATEPART(WEEKDAY, orderDate), CAST(orderDate AS DATE)) AS ordersPerWeek, 
    sum(orderAmount) AS orderSumForGrouping, 
    count(1) AS numberOfOrdersWithinGrouping
FROM    @table o
GROUP   BY DATEADD(DAY, 7 - DATEPART(WEEKDAY, orderDate), CAST(orderDate AS DATE))

输出:

ordersPerWeek   orderSumForGrouping numberOfOrdersWithinGrouping
2015-01-03  500 1
2015-01-10  2000    4
2015-01-17  400 1
2016-01-02  1000    2
2016-01-09  2000    4
2016-01-16  400 1
2016-12-17  1200    1

观察2016-01-022016-01-092016-01-16是星期六 - 一周的最后一天。 (对于其他日期也适用,但这些日期刚刚发生,因此最容易检查)

答案 1 :(得分:0)

我可以通过为WeekEnd创建自动计算列并在查询中重复使用它的一种方法:

<强>设定:

-- drop table testTable
create table testTable
(
    MyDate SMALLDATETIME,
    WeekEnd AS DATEADD (D, -1 * DatePart(DW, MyDate) + 7, MyDate)
)
GO

INSERT INTO testTable VALUES ('20141201'), ('20150101'), ('20150130')
GO

select * from testTable
GO

查询:

select CAST(Datepart(month, WeekEnd) AS VARCHAR) + ' - ' + CAST(Datepart(day, WeekEnd) AS VARCHAR) AS [Week Ending]
from testTable

不幸的是,我无法使列保持不变(我无法说服SQL Server我正在做的事情是确定性的,即使使用带有模式绑定的函数)。这将是很好的,因为可以索引持久计算列。