SQL-在WHERE子句中使用计算的日期

时间:2018-09-06 13:36:48

标签: sql sql-server date

我有一个带有未来值的表,我只想要最后一个整周。

意思是af想要找到最后一个星期日并有WHERE子句,其中日期在“最后一个星期日-7”和“最后一个星期日”之间。

意思是我有交易直到'31 -10-2018'并且想要介于'2018-10-28'和'2018-10-22'(包括两天)之间的时间。但是每周都会不断添加新数据,因此很快我就会拥有直到'2018-11-30'的数据,依此类推。

我尝试了如下查询:

var companiesList = //get companies list 
ViewBag.ListOfCompanies = companiesList.Select(x => new SelectListItem() {Text = x.ESTATE_Company, Value = x.ID_Company.ToString()});
....
@Html.DropDownListFor(x=>x.ID_Company, ViewBag.Items as IEnumerable<SelectListItem>)

注意:我知道这只会给我最后一天('2018-10-31',而不是最后一个星期日)

如何将计算出的日期包括在我的子句中,并将目标日期设置为最后一个星期日?

我的表格包含以下列:

SELECT *
FROM PlannedCounts
WHERE [Date] BETWEEN DATEADD(d,-6,MAX([Date])) and MAX([Date])

Picture of my dataset and expected result

2 个答案:

答案 0 :(得分:1)

对于给定的日期2018-10-31,您需要确定星期几(1 ... 7),以便可以计算前一个星期日。不幸的是,这不是直截了当的(see discussion):

CREATE TABLE #PlannedCounts(Date Date);
INSERT INTO #PlannedCounts(Date) VALUES
    ('2018-10-31'),
    ('2018-10-30'),
    ('2018-10-29'),
    ('2018-10-28'),
    ('2018-10-27'),
    ('2018-10-26'),
    ('2018-10-25'),
    ('2018-10-24'),
    ('2018-10-23'),
    ('2018-10-22'),
    ('2018-10-21'),
    ('2018-10-20'),
    ('2018-10-19'),
    ('2018-10-18'),
    ('2018-10-17'),
    ('2018-10-16'),
    ('2018-10-15'),
    ('2018-10-14'),
    ('2018-10-13'),
    ('2018-10-12');

DECLARE @LastSunday AS DATE;
DECLARE @PrevSunday AS DATE;

SELECT
    @LastSunday = DATEADD(DAY, -(@@DATEFIRST + DATEPART(dw, MAX(Date)) - 1) % 7 - 0, MAX(Date)),
    @PrevSunday = DATEADD(DAY, -(@@DATEFIRST + DATEPART(dw, MAX(Date)) - 1) % 7 - 6, MAX(Date))
FROM #PlannedCounts

-- @LastSunday = 2018-10-28
-- @PrevSunday = 2018-10-22

SELECT *
FROM #PlannedCounts
WHERE Date BETWEEN @PrevSunday AND @LastSunday

注意:可以用查询替换用户变量。

答案 1 :(得分:0)

您需要在where子句中使用case语句-

SELECT *
FROM PlannedCounts
WHERE [Date] >= CASE WHEN DAYNAME(MAX([Date])) = 'Monday' THEN 
                          DATEADD(d,-1,MAX([Date]))
                     WHEN DAYNAME(MAX([Date])) = 'Tuesday' THEN 
                          DATEADD(d,-2,MAX([Date]))
                     WHEN DAYNAME(MAX([Date])) = 'Wednesday' THEN 
                          DATEADD(d,-3,MAX([Date]))
                     WHEN DAYNAME(MAX([Date])) = 'Thursday' THEN 
                          DATEADD(d,-4,MAX([Date]))
                     WHEN DAYNAME(MAX([Date])) = 'Friday' THEN 
                          DATEADD(d,-5,MAX([Date]))
                     WHEN DAYNAME(MAX([Date])) = 'Saturday' THEN 
                          DATEADD(d,-6,MAX([Date]))
                 ELSE MAX([Date] END
AND [Date] <= CASE WHEN DAYNAME(MAX([Date])) = 'Monday' THEN 
                          DATEADD(d,-7,MAX([Date]))
                     WHEN DAYNAME(MAX([Date])) = 'Tuesday' THEN 
                          DATEADD(d,-8,MAX([Date]))
                     WHEN DAYNAME(MAX([Date])) = 'Wednesday' THEN 
                          DATEADD(d,-9,MAX([Date]))
                     WHEN DAYNAME(MAX([Date])) = 'Thursday' THEN 
                          DATEADD(d,-10,MAX([Date]))
                     WHEN DAYNAME(MAX([Date])) = 'Friday' THEN 
                          DATEADD(d,-11,MAX([Date]))
                     WHEN DAYNAME(MAX([Date])) = 'Saturday' THEN 
                          DATEADD(d,-12,MAX([Date]))
                 ELSE DATEADD(d,-6,MAX([Date])) END