SQL Server-一周结束

SQL Server-一周结束,sql,sql-server,date,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

我有下面的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]
我只想在另一列中以年为结束的一周,这样我就可以按年分组,然后按周结束。我能帮忙吗

你不能这样做吗?:

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-01-02

2016-01-09

使用示例:

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
orderYear   orderSumForGrouping numberOfOrdersWithinGrouping
2015    2900    6
2016    4600    8
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
按年份分组的订单金额:

SELECT  
    year(orderDate) AS orderYear, 
    sum(orderAmount) AS orderSumForGrouping, 
    count(1) AS numberOfOrdersWithinGrouping
FROM    @table o
GROUP   BY year(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
orderYear   orderSumForGrouping numberOfOrdersWithinGrouping
2015    2900    6
2016    4600    8
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
按周末日期分组的订单金额:

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))
输出:

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
orderYear   orderSumForGrouping numberOfOrdersWithinGrouping
2015    2900    6
2016    4600    8
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-02
2016-01-09
2016-01-16
都是星期六-一周的最后一天。(其他日期也适用,但这些日期刚刚出现,因此最容易检查)

您不能这样做吗?:

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-01-02

2016-01-09

使用示例:

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
orderYear   orderSumForGrouping numberOfOrdersWithinGrouping
2015    2900    6
2016    4600    8
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
按年份分组的订单金额:

SELECT  
    year(orderDate) AS orderYear, 
    sum(orderAmount) AS orderSumForGrouping, 
    count(1) AS numberOfOrdersWithinGrouping
FROM    @table o
GROUP   BY year(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
orderYear   orderSumForGrouping numberOfOrdersWithinGrouping
2015    2900    6
2016    4600    8
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
按周末日期分组的订单金额:

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))
输出:

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
orderYear   orderSumForGrouping numberOfOrdersWithinGrouping
2015    2900    6
2016    4600    8
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-02
2016-01-09
2016-01-16
都是星期六-一周的最后一天。(其他日期也适用,但这些日期刚刚出现,因此最容易检查)

我可以将其缩小一点的一种方法是为
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我所做的是确定性的,即使使用带有模式绑定的函数)。这将非常好,因为持久化的计算列可以被索引

我可以将其缩小一点的一种方法是为
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我所做的是确定性的,即使使用带有模式绑定的函数)。这将非常好,因为持久化的计算列可以被索引

这就是我所做的,但我想要的是结束一周的实际日期,而不是周数。我发布的内容给出的是实际日期,而不是周数。我想我是这样开始的,但这肯定更好。有时你走错了路,需要从头开始。这就是我所做的,但我想要的是结束一周的实际日期,而不是周数。我发布的内容给出的是实际日期,而不是周数。我想我是这样开始的,但确实更好。有时你走错了路,需要从头开始。对于年份,你可以使用内置函数
Year
Year(GETDATE())一样作为“Year”
,而不是强迫我对代码进行反向工程,并对你的语言设置进行假设,你如何定义一周结束?周末是星期六、星期天还是别的什么?而且,即使基于此,您希望计算什么值?对于年,您可以使用内置函数
Year
Year(GETDATE())一样作为“年”
,而不是强迫我对代码进行反向工程,并对您的语言设置进行假设,您如何定义一周结束?周末是星期六、星期天还是别的什么?即使基于此,你想计算什么值?