1

嗨,我有以下声明:

DECLARE @currentWEEK INT
DECLARE @previousWEEK INT
DECLARE @CURRENDATE  DATETIME
DECLARE @PREVIOUSDATE  DATETIME

SET @PREVIOUSDATE = YEAR(DATEADD(YEAR, -1, GETDATE()))
SET @CURRENDATE = GETDATE()
SET @currentWEEK = RIGHT(DATENAME(wk,'2013-05-17 00:00:00.000'),2)
SET @previousWEEK = RIGHT(DATENAME(wk,'2012-05-17 00:00:00.000'),2)

SELECT COUNT(*)
FROM vevents 
WHERE RIGHT(DATENAME(wk,dbo.vEvents.DateCreated),2) = @currentWEEK
AND
YEAR(dbo.vEvents.DateCreated) = YEAR(@CURRENDATE)

SELECT COUNT(*)
FROM vevents 
    WHERE RIGHT(DATENAME(wk,dbo.vEvents.DateCreated),2) = @previousWEEK
AND
YEAR(dbo.vEvents.DateCreated) = @PREVIOUSDATE

本质上它比较给定一周内两个日期的总数,但我想结合给定日期的两个选择语句以返回单个记录集(例如 CURRENTYEARCOUNT 和 PREVIOUSYEARCOUNT)

非常感谢您的帮助

4

1 回答 1

0

试试这个——

DECLARE 
      @currentWEEK INT
    , @previousWEEK INT
    , @CURRENDATE DATETIME
    , @PREVIOUSDATE DATETIME

SELECT 
      @PREVIOUSDATE = YEAR(DATEADD(YEAR, -1, GETDATE()))
    , @CURRENDATE = GETDATE()
    , @currentWEEK = RIGHT(DATENAME(wk, '20130517'), 2)
    , @previousWEEK = RIGHT(DATENAME(wk, '20120517'), 2)

SELECT 
      COUNT(CASE WHEN YEAR(e.DateCreated) = @PREVIOUSDATE THEN 1 END)
    , COUNT(CASE WHEN YEAR(e.DateCreated) = YEAR(@CURRENDATE) THEN 1 END)
FROM dbo.vEvents e
WHERE RIGHT(DATENAME(wk, e.DateCreated), 2) = @currentWEEK
于 2013-09-26T06:02:42.313 回答