SQL - 从机器中选择当前日期并进行比较

时间:2016-09-06 08:22:05

标签: sql sql-server tsql

对于以下查询 情景1:当前年度(上周) - 例如 - 第31周

sum(case when s.Date between '2016-07-17' and '2016-07-23' then s.SELLINC else 0 end) ActualSales 

情景2:去年(上周) - 例如 - 第31周

sum(case when s.Date between '2015-07-19' and '2015-07-25' then s.SELLINC else 0 end) LastYrVarianc 

场景3:从当年年初到今天的日期之间挑选日期

sum(case when s.Date between '2016-01-01' and '2016-09-05' then s.SELLINC else 0 end) YrToDateActual 

情景4:从去年年初到今年去年的截止日期

sum(case when s.Date between '2015-01-01' AND '2015-09-05' then s.SELLINC else 0 end) LastYrToDateActual

而不是硬编码日期。我想从机器中选择当前日期并进行比较。

周从星期日开始,到星期六结束。有什么帮助吗?

5 个答案:

答案 0 :(得分:2)

首先,GETDATE()是今天日期的SQL Server函数

DATEADD(..)是向日期添加内容的功能

1) case when s.date between DATEADD(dd,-6,getdate()) and getdate()) then...

2) case when s.date between DATEADD(yy,-1,DATEADD(dd,-6,getdate())) and DATEADD(yy,-1,getdate()) then ...

3) case when s.date between DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) and getdate() then ...

4) case when s.date between dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)) and DATEADD(yy,-1,GETDATE()) then ..

来自here

的一点帮助

答案 1 :(得分:1)

我假设你的一周从星期日到星期六开始。

您可以使用此查询

场景-1(PreviousWeekStartDate和PreviousWeekEndDate)

s.Date between convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) - 1) and convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) + 5)

场景-2(LastYearPreviousWeekStartDate和LastYearPreviousWeekEndDate)

s.Date between convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) - 1) and convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5)

场景-3(StartOfYear和CurrentDate)

s.Date between convert(date,DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0)) and convert(date,getdate())

场景-4(StartOfLastYear和CurrentDateLastYear)

s.Date between convert(date,DATEADD(yy, DATEDIFF(yy, 0, dateadd(YEAR, - 1, getdate())), 0)) and convert(date,dateadd(YEAR, - 1, getdate()))

答案 2 :(得分:0)

使用日期算术

declare @weekNo int = 31; 
--start of the year
declare @ys datetime = dateadd(year,datediff(year,0,getdate()),0) 
-- start of the first week of the year (may start in December of prev year)
declare @y1ws datetime = dateadd(week,datediff(week,0,@ys),0) 

select @ys, @y1ws, dateadd(week, @weekNo-1, @y1ws) [week31 start], dateadd(week, @weekNo, @y1ws) [week32 start]
--    use it this way for week 31
-- .. where somedate >= [week31 start] and somedate < [week32 start]

答案 3 :(得分:0)

SELECT
    GETDATE(),                                          -- Today
    DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0),     -- Start of this year
    DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0), -- Start of last year
    DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0),     -- Start of this week
    DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()) - 1, 0)  -- Start of last week

答案 4 :(得分:0)

我认为使用DATEPART (Transact-SQL)是一种更便携的解决方案。我对场景1和场景2的回答是指从周日开始的自然周,这与JohnHC的答案不同(参考最近7天)。

--
-- Scenario 1: Current Year (Previous Week) - For eg- Week 31
--
CASE WHEN
    DATEPART(ww, s.Date) = DATEPART(ww, GETDATE()) - 1 AND
    DATEPART(yy, s.Date) = DATEPART(yy, GETDATE())
THEN ...

--
-- Scenario 2: Last Year (Previous Week) - For eg- Week 31
--
CASE WHEN
    DATEPART(ww, s.Date) = DATEPART(ww, GETDATE()) - 1 AND
    DATEPART(yy, s.Date) = DATEPART(yy, GETDATE()) - 1
THEN ...

--
-- Scenario 3: Picking dates between beginning of current year till today's date
--
CASE WHEN
    DATEPART(yy, s.Date) = DATEPART(yy, GETDATE())
THEN ...

--
-- Scenario 4: Picking dates between beginning of last year till last year today's date
--
CASE WHEN
    DATEPART(yy, s.Date) >= DATEPART(yy, GETDATE()) - 1
THEN ...