在SQL

时间:2016-02-08 14:46:37

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

我希望我一周的第一天成为星期日,但我的代码问题是它始终是星期一开始的。

SELECT 
    UID,
    DATEADD(DD,CONVERT(INT, (DATEDIFF(DD, '1/1/1900', t.DT)/7)) * 7,'1/1/1900') [WeekBeginDate], 
    SUM(HOURS) AS TOTAL_HOURS 
FROM 
    myTable t 
WHERE 
    DT >= DATEADD(WEEK, -6, GetDate()) 
GROUP BY 
    UID, CONVERT(INT, DATEDIFF(DD, '1/1/1900', t.DT)/7)

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题:

SELECT
  UID,  
  DATEADD(d, -1, DATEDIFF(d, -1, t.DT)/7 * 7) [WeekBeginDate] ,
  SUM(HOURS) AS TOTAL_HOURS 
FROM myTable t 
WHERE DT >= DATEADD(WEEK, -6, GetDate()) 
GROUP BY UID, DATEDIFF(d, -1, t.DT)/7