gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:43:50 25 4
gpt4 key购买 nike

我的 SQL 查询中有一个场景是我必须找出具有给定日期但来自多年的婴儿记录认为我的选择日期是 = 01/Jan/2014 到 31/Jan/2014

我有另一个参数是年份,它要求我随机选择以前(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

最佳答案

这是我实现了我想要的结果,但我想使用 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

--这里是存储过程的使用

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

关于sql-server - SQL Server 中选择查询中的多个案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21927873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com