2014-02-21 73 views
3

我在我的SQL查詢中有一個方案是我必須找出給定日期但多年的嬰兒記錄 假設 我的選擇日期是= 01/Jan/2014至31/Jan/2014在SQL Server中選擇查詢中的多個案例

我還有一個參數是年份,它要求我隨機選擇以前(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查詢是不明確的,並且具有error.I有單日期比較,它的正常,但當我添加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 
+0

我不明白的要求是正確的,但在這裏也一樣,我注意到,您正在使用的where子句中的功能。你應該看看這個鏈接 http://stackoverflow.com/questions/799584/what-makes-a-sql-statement-sargable –

+0

我想選擇的多重年同一日期範圍內我剛纔所說的數據,我想做的事情是寫下精確的查詢,可以從用戶的年份多年選擇 - 給出 – DareDevil

+0

也許我很笨,但我不明白你的情況在最後。如果@y = 2,那麼...爲什麼從無條件的另一個案例開始?爲什麼你有答案或答案。你想怎麼做?你想要enddate或enddate -1嗎?還有最後一件事,因爲你的條件<=如果它小於enddate-1它總是<=比enddate它自己所以你爲什麼需要或子句? –

回答

4

下面是我已經達到了我想要的結果,但我想這可能是可能的情況下在哪裏。

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

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 @[email protected]_Date 
SET @[email protected]_Date 
--SELECt * FROM BABIES_TABLE 
PRINT 'For One Year' 
SET @[email protected] 

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 

--Here是StoredProcedure的

使用
Exec BabyUnionData 5,'01-01-2014','01-31-2014' 
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. 
+0

** <3 **僅用於數據查看,顯然我有以整數格式存儲的數據。 – DareDevil

0

我有同樣的解決方案維託雷和ñ思維,問題是我沒有按照你的要求從我的理解。當它今天開始日期結束日期和同一間1回吐了前一年

這裏是一個uneleguant解決方案,我有

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

UPDATE

我有這個想法如果你需要特定的日期測試不長。例如,如果y = 2,則需要dob = 19/02/13或19/02/12或19/02/11,但在19/02/11和19/02/13之間不需要dob。

+0

這裏Y = 2只是一個例子,在一個例子中我必須找出兩年的數據,這個數值可以從1〜4變化 – DareDevil