MSSQL-GROUP BY Date语句显示错误数据

时间:2018-07-27 11:43:34

标签: sql sql-server date group-by

我有一个GROUP BY语句,该语句根据周对我的数据进行排序。这里的问题是我整个星期都没有计算我的数据(不计算星期日)

这里是一个例子:

CREATE TABLE [dbo].[Products](
[ProductNR] [varchar](14) NULL,
[Location] [int] NULL,
[Date] [datetime] NULL);

INSERT INTO Products (ProductNR, Location, Date)
VALUES 
('12345678911' ,1, '2018-07-16 00:00:00.000'), -- Monday
('12345678912' ,1, '2018-07-16 00:00:00.000'), -- Monday
('12345678913' ,1, '2018-07-16 00:00:00.000'), -- Monday
('12345678914' ,1, '2018-07-16 00:00:00.000'), -- Monday
('12345678915' ,2, '2018-07-16 00:00:00.000'), -- Monday
('12345678916' ,3, '2018-07-22 00:00:00.000'); -- This is a sunday

这是我的Sql查询,它在2周内检索了上述数据分割,而该数据分割应该只是一个星期的计数:

SELECT count(Distinct ProductNR) AS [Count]
FROM Products
WHERE
YEAR (CREATED) = '2018'
Group by datepart(wk, created), year(created) 

结果:

|Count|
|  5  |
|  1  |

预期结果:

|Count|
|  6  |

4 个答案:

答案 0 :(得分:2)

您似乎想要ISO_WEEK

SELECT COUNT(DISTINCT ProductNR) AS [Count]
FROM Products
WHERE YEAR (CREATED) = '2018'
GROUP BY DATEPART(ISO_WEEK, created), YEAR(created); 

答案 1 :(得分:1)

SELECT 
  count(Distinct ProductNR) AS [Count]
FROM Products
WHERE
  CREATED >= '2018-01-01' and CREATED < '2019-01-01'
GROUP BY 
  datediff(d, 0, created) / 7

如果要包括星期,这里是一个完整的脚本,包括iso year和iso week

DECLARE @year DATE = '2018'

SELECT 
    count(Distinct ProductNR) AS [Count], 
    datepart(yy, dateadd(wk, datediff(d, 0, CREATED)/7, 3)) year,
    datepart(iso_week, CREATED) week
FROM Products
WHERE
    CREATED >= dateadd(wk, datediff(d, 0, dateadd(d, 3, @year))/7, 0)
    and CREATED < dateadd(wk, datediff(d, 0, dateadd(d, 3, dateadd(yy, 1, @year)))/7, 0)
GROUP BY 
    datepart(yy, dateadd(wk, datediff(d, 0, CREATED)/7, 3)),
    datepart(iso_week, CREATED)

答案 2 :(得分:0)

事情是某些地方从星期日的一周开始,所以我认为sql将2018-07-22视为下周的一部分

您可以尝试使用ISO_WEEK

SELECT count(Distinct ProductNR) AS [Count]
FROM Products
WHERE
YEAR (CREATED) = '2018'
Group by datepart(ISO_WEEK, created), year(created) 

答案 3 :(得分:0)

使用以下查询将星期一设置为一周的第一天: 设置日期第一
@MishMish这是我使用的代码段

<!-- language: sql -->
USE tempdb
IF EXISTS( SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'Products')
    DROP TABLE Products

CREATE TABLE [Products](
[ProductNR] [varchar](14) NULL,
[Location] [int] NULL,
[Date] [datetime] NULL);

INSERT INTO Products (ProductNR, Location, Date)
VALUES 
('12345678911' ,1, '2018-07-16 00:00:00.000'), -- Monday
('12345678912' ,1, '2018-07-16 00:00:00.000'), -- Monday
('12345678913' ,1, '2018-07-16 00:00:00.000'), -- Monday
('12345678914' ,1, '2018-07-16 00:00:00.000'), -- Monday
('12345678915' ,2, '2018-07-16 00:00:00.000'), -- Monday
('12345678916' ,3, '2018-07-22 00:00:00.000'); -- This is a sunday

--SELECT  datepart(wk, Date) as Week, * FROM Products

--Count Before Setting First Date To Monday
SET DATEFIRST 7  -- Sunday ( default, U.S. English)

SELECT count(Distinct ProductNR) AS [Count]
FROM Products
WHERE
YEAR (Date) = '2018'
Group by datepart(wk, Date), year(Date) 

--CHANGE FIRST Date To Monday
SET DATEFIRST 1

--Count AFTER Setting First Date To Monday
SELECT count(Distinct ProductNR) AS [Count]
FROM Products
WHERE
YEAR (Date) = '2018'
Group by datepart(wk, Date), year(Date)