SQL Server中选择查询的多个案例

时间:2014-02-21 07:20:55

标签: sql-server datetime select case qsqlquery

我的SQL查询中有一个场景是我必须找出给定日期但多年的婴儿记录 假设 我的选择日期是2014年1月1日至2014年1月31日

我有另一个年份参数,它随机要求我选择以前(1〜3年)出生的婴儿的数据 假设我目前的选定日期是2014年,而我的年变量的值是2,那么

我必须分别将婴儿数据带到当前年份,也不得超过当前日期等等 以下是我的给定场景

--------------------------------------------
--  BABIES_TABLE
ID |    Name         |  Age |   DOB         |
--------------------------------------------
1           JKL         3       21/Jan/2012
2           DEF         2       09/Jan/2013
3           ABC         1       04/Jan/2014
4           XYZ         1       02/Feb/2014

-- Date Range [01/Jan/2014 - 31/Jan/2014]
-- Year = 2
---------------------------------------------
ID     |    Name    |   Age |   DOB         |
---------------------------------------------
3           ABC         1       04/Jan/2014
2           DEF         2       09/Jan/2013



-- Date Range [01/Jan/2014 - 31/Jan/2014]
-- Year = 3
---------------------------------------------
ID     |    Name    |   Age |   DOB         |
---------------------------------------------
3           ABC         1       04/Jan/2014
2           DEF         2       09/Jan/2013
1           JKL         3       21/Jan/2012

下面是我的SQl查询,它有歧义且有错误。我尝试过单日期比较,很好但是当我添加 OR 条件时,它会给我错误

--MY SQL Query is Below

DECLARE @startDate AS DATETIME
DECLARE @enddate AS DATETIME
DECLARE @y AS INT
SET @startDate='2012-10-12'
SET @enddate='2012-10-20'
SET @y=2

SELECT * FROM BABIES_TABLE
WHERE 
        CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= CASE            
            --If entered Year = 1
            WHEN @y = 1 THEN        
            (@startDate)                                     
            --If entered Year = 2
            WHEN @y = 2 THEN
                  CASE
                  (@startDate)
                  END                  
            OR
                  CASE
                  YEAR(@startDate-1)
                  END            

            WHEN @y = 3 THEN
                  CASE
                  (@startDate)
                  END          
            OR
                  CASE
                  YEAR(@startDate-1)
                  END
            OR
                  CASE
                  YEAR(@startDate-2)
                  END
        END
    END
  AND
        CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= CASE
            WHEN @y = 1 THEN        
            (@enddate)                                     

            WHEN @y = 2 THEN
                  CASE
                  (@enddate)
                  END                  
            OR
                  CASE
                  YEAR(@enddate-1)
                  END            

            WHEN @y = 3 THEN
                CASE
                (@enddate)
                END          
            OR
                CASE
                YEAR(@enddate-1)
                END
            OR
                CASE
                YEAR(@enddate-2)
                END
        END
    END

3 个答案:

答案 0 :(得分:4)

以下是我达到预期效果的结果,但我想可以使用Case in Where。

---------------------------------------------

ID |    Name         |  Age |   DOB         |
--------------------------------------------

1   Abid                 6  2008-01-01
2   Zahid                6  2008-01-10
3   Laila                5  2009-01-15
4   Ali                  5  2010-01-26
5   Kali                 4  2011-01-19
6   Sali                 3  2012-01-08
7   Brone                2  2013-01-11
8   Dilche               2  2013-01-29
9   Alpechino            3  2012-08-20

这是我的存储过程。

--MY SQL Query is Below
CREATE PROCEDURE BabyUnionData
@Year AS INT,
@Start_Date as varchar(30),
@End_Date as Varchar(30)
AS
BEGIN


DECLARE @startDate AS DATETIME
DECLARE @enddate AS DATETIME
DECLARE @y AS INT
SET @startDate=@Start_Date
SET @enddate=@End_Date
--SELECt * FROM BABIES_TABLE
PRINT 'For One Year'
SET @y=@Year

IF @y = 1       
    BEGIN
    PRINT 'For One Year'
        SELECT * FROM BABIES_TABLE
        WHERE 
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= @startDate
            AND
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= @enddate
    END
ELSE IF @y = 2  
    BEGIN
    PRINT 'For Two Years'
        SELECT * FROM BABIES_TABLE
        WHERE 
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= @startDate
            AND
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= @enddate
        UNION
        SELECT * FROM BABIES_TABLE
        WHERE 
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= DATEADD(Year,-1,@startDate)
            AND
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= DATEADD(Year,-1,@enddate)
    END
    ELSE IF @y = 3
    BEGIN
        PRINT 'For Three Years'
        SELECT * FROM BABIES_TABLE
        WHERE 
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= @startDate
            AND
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= @enddate
        UNION
        SELECT * FROM BABIES_TABLE
        WHERE 
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= DATEADD(Year,-1,@startDate)
            AND
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= DATEADD(Year,-1,@enddate)
        UNION
        SELECT * FROM BABIES_TABLE
        WHERE 
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= DATEADD(Year,-2,@startDate)
            AND
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= DATEADD(Year,-2,@enddate)
    END
    ELSE IF @y = 4
    BEGIN
    PRINT 'For Four Years'
        SELECT * FROM BABIES_TABLE
        WHERE 
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= @startDate
            AND
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= @enddate
        UNION
        SELECT * FROM BABIES_TABLE
        WHERE 
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= DATEADD(Year,-1,@startDate)
            AND
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= DATEADD(Year,-1,@enddate)
        UNION
        SELECT * FROM BABIES_TABLE
        WHERE 
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= DATEADD(Year,-2,@startDate)
            AND
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= DATEADD(Year,-2,@enddate)
        UNION
        SELECT * FROM BABIES_TABLE
        WHERE 
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= DATEADD(Year,-3,@startDate)
            AND
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= DATEADD(Year,-3,@enddate)
    END
    ELSE IF @y = 5
    BEGIN
    PRINT 'For Five Years'
        SELECT * FROM BABIES_TABLE
        WHERE 
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= @startDate
            AND
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= @enddate
        UNION
        SELECT * FROM BABIES_TABLE
        WHERE 
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= DATEADD(Year,-1,@startDate)
            AND
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= DATEADD(Year,-1,@enddate)
        UNION
        SELECT * FROM BABIES_TABLE
        WHERE 
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= DATEADD(Year,-2,@startDate)
            AND
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= DATEADD(Year,-2,@enddate)
        UNION
        SELECT * FROM BABIES_TABLE
        WHERE 
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= DATEADD(Year,-3,@startDate)
            AND
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= DATEADD(Year,-3,@enddate)
        UNION
        SELECT * FROM BABIES_TABLE
        WHERE 
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= DATEADD(Year,-4,@startDate)
            AND
            CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= DATEADD(Year,-4,@enddate)
    END
END

- 这是使用StoredProcedure

Exec BabyUnionData 5,'01-01-2014','01-31-2014'

答案 1 :(得分:0)

将您的开始和结束日期以及DOB标准化为当前年度

 SET @startDate=DATEADD(yy, YEAR(@startDate)-YEAR(getdate()), @startDate)
 SET @enddate=DATEADD(yy, YEAR(@enddate)-YEAR(getdate()), @enddate)

 select * from babies where 
     Age >= @y  -- check that baby is old enough 
     and DATEADD(yy, YEAR(dob)-YEAR(getdate()), dob) between @startDate and @enddate - and the DOB is within a range in a year.

答案 2 :(得分:0)

我有与Vittore相同的解决方案然后思考,问题是我没有按照我的理解来遵循你的要求。当它在今天的开始日期和前一年的结束时间为1时

这是一个无法解决的解决方案

create table #y (y tinyint)
insert into #y values (1)
insert into #y values (2)
insert into #y values (3)


DECLARE @startDate AS DATETIME
DECLARE @enddate AS DATETIME
DECLARE @y AS INT
SET @startDate='2012-10-12'
SET @enddate='2012-10-20'
SET @y=2

SELECT * 
FROM BABIES_TABLE
cross join #y 
WHERE CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) >= datediff(year, y - 1, @startdate )
AND CAST(CONVERT(VARCHAR, BABIES_TABLE.DOB, 111) AS DATETIME) <= DATEDIFF(year, y - 1, @enddate )
and y <= @y

或许可以使用cte

<强>更新

我有这个想法,以防你需要特定的日期测试。例如,你想要dob = 19/02/13或19/02/12或19/02/11,如果y = 2但不是dob在19/02/11和19/02/13之间。