gpt4 book ai didi

sql - 在 SQL Server 2008 中使用嵌套 CASE 语句

转载 作者:行者123 更新时间:2023-12-04 18:26:33 25 4
gpt4 key购买 nike

我的逻辑一定有点古怪,因为当我测试以确保这些 CASE 语句为真时,我不会得到预期的结果......

例如,如果 Sample1 = 'FI'Sample2 = 'GOVT'Sample3 = 'GOVT',我不明白'OKFI' ...我知道我一定有一些古怪的逻辑...

对这些事情的最佳实践有什么想法吗?

CREATE FUNCTION SampleFunction
(
@Sample1 varchar(50),
@Sample2 varchar(50) = null,
@Sample3 varchar(50) = null
)
RETURNS VARCHAR(255)
AS
BEGIN

declare @output varchar(255)



SET @output =

CASE WHEN @Sample1 = 'CASH' THEN 'CASH'

WHEN @Sample1 = 'ST' THEN
CASE WHEN @Sample2 ='TBILL' THEN
CASE WHEN @Sample3 = 'TBILL' THEN 'OKFI'

WHEN @Sample1 = 'FI' THEN
CASE WHEN @Sample2 = 'GOVT' THEN
CASE WHEN @Sample3 = 'GOVT' THEN 'OKFI'

WHEN @Sample1 = 'DERV' THEN
CASE WHEN @Sample2 = 'CURRCONT' THEN
CASE WHEN @Sample3 = 'FORWARD' THEN 'CASH'
WHEN @Sample3 = 'SPOT' THEN 'CASH'
WHEN @Sample2 = 'SWAP' THEN
CASE WHEN @Sample3 = 'CURRSWAP' THEN 'CASH'
WHEN @Sample3 = 'INDXSWAP' THEN 'OTHER'

WHEN @Sample2 = 'FUTURE' THEN
CASE WHEN @Sample3 = 'COMFUT' THEN 'COM'
WHEN @Sample3 = 'BFUT' THEN 'OKFI'
WHEN @Sample3 = 'NDXFUT' THEN 'OKSHARES'
WHEN @Sample3 = 'EQFUT' THEN 'OKSHARES'
WHEN @Sample3 = 'CURRFUT' THEN 'CASH'

WHEN @Sample2 = 'OPTION' THEN
CASE WHEN @Sample3 = 'CUROPTION' THEN 'CASH' END

ELSE 'OTHER'

END END END
END END END
END END END RETURN @Output

END -- FUNCTION

最佳答案

您应该创建@table 变量,甚至是包含列 Sample1、Sample2、Sample3、Output 的物理永久表 - 在其中插入所有组合和所需的输出,然后只需选择 Output 基于您的 @Sample1@Sample2@Sample3 变量。

这样在 CASE 中有几十个 CASE,你只会迷路。

类似这样的:

CREATE FUNCTION SampleFunction2
(
@Sample1 VARCHAR(50) ,
@Sample2 VARCHAR(50) = NULL ,
@Sample3 VARCHAR(50) = NULL
)
RETURNS VARCHAR(255)
AS
BEGIN

DECLARE @output VARCHAR(255)


DECLARE @tempResults TABLE
(
Sample1 VARCHAR(50) ,
Sample2 VARCHAR(50) ,
Sample3 VARCHAR(50) ,
[Output] VARCHAR(255)
)

INSERT INTO @tempResults
VALUES ( 'CASH', NULL, NULL, 'CASH' )
,( 'ST', 'TBILL', 'TBILL', 'OKFI' )
,( 'FI', 'GOVT', 'GOVT', 'OKFI' )
,( 'DERV', 'CURRCONT', 'FORWARD', 'CASH' )
,( 'DERV', 'CURRCONT', 'SPOT', 'CASH' )
,( NULL, 'SWAP', 'CURRSWAP', 'CASH' )
,( NULL, 'SWAP', 'INDXSWAP', 'OTHER' )
,( NULL, 'FUTURE', 'COMFUT', 'COM' )
,( NULL, 'FUTURE', 'BFUT', 'OKFI' )
,( NULL, 'FUTURE', 'NDXFUT', 'OKSHARES' )
,( NULL, 'FUTURE', 'EQFUT', 'OKSHARES' )
,( NULL, 'FUTURE', 'CURRFUT', 'CASH' )
,( NULL, 'OPTION', 'CUROPTION', 'CASH' )


SELECT @output = [Output]
FROM @tempResults
WHERE (@Sample1 IS NULL OR Sample1 = @Sample1)
AND (@Sample2 IS NULL OR Sample2 = @Sample2)
AND (@Sample3 IS NULL OR Sample3 = @Sample3)

IF @output IS NULL SET @output = 'OTHER'

RETURN @output
END

SQLFiddle DEMO

如果您想坚持使用 CASE,则原始查询的问题是放错了 ENDs :

CREATE FUNCTION SampleFunction
(
@Sample1 varchar(50),
@Sample2 varchar(50) = null,
@Sample3 varchar(50) = null
)
RETURNS VARCHAR(255)
AS
BEGIN

declare @output varchar(255)



SET @output =

CASE WHEN @Sample1 = 'CASH' THEN 'CASH'

WHEN @Sample1 = 'ST' THEN
CASE WHEN @Sample2 ='TBILL' THEN
CASE WHEN @Sample3 = 'TBILL' THEN 'OKFI'
END
END

WHEN @Sample1 = 'FI' THEN
CASE WHEN @Sample2 = 'GOVT' THEN
CASE WHEN @Sample3 = 'GOVT' THEN 'OKFI'
END
END

WHEN @Sample1 = 'DERV' THEN
CASE WHEN @Sample2 = 'CURRCONT' THEN
CASE WHEN @Sample3 = 'FORWARD' THEN 'CASH'
WHEN @Sample3 = 'SPOT' THEN 'CASH'
END
END

WHEN @Sample2 = 'SWAP' THEN
CASE WHEN @Sample3 = 'CURRSWAP' THEN 'CASH'
WHEN @Sample3 = 'INDXSWAP' THEN 'OTHER'
END

WHEN @Sample2 = 'FUTURE' THEN
CASE WHEN @Sample3 = 'COMFUT' THEN 'COM'
WHEN @Sample3 = 'BFUT' THEN 'OKFI'
WHEN @Sample3 = 'NDXFUT' THEN 'OKSHARES'
WHEN @Sample3 = 'EQFUT' THEN 'OKSHARES'
WHEN @Sample3 = 'CURRFUT' THEN 'CASH'
END

WHEN @Sample2 = 'OPTION' THEN
CASE WHEN @Sample3 = 'CUROPTION' THEN 'CASH'
END

ELSE 'OTHER'


END RETURN @Output

END -- FUNCTION

SQLFiddle DEMO

关于sql - 在 SQL Server 2008 中使用嵌套 CASE 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17679799/

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