在新 CASE 中使用 CASE 的输出 (SQL)
Using output from CASE in a new CASE (SQL)
有人能告诉我如何使用 CASE
语句的输出作为 t-SQL 中下一个 CASE
语句的输入吗?
exampleTable
id | itemNo | samplesLeft
-------------------------
1 | 001 | 104
2 | 003 | 53
3 | 002 | 193
4 | 001 | 32
我想要一个查询 returns 所有这些,以及一个布尔值来判断它是否是出厂错误。如果使用的样本少于 10 个,则错误被认为是工厂产生的。我的想法是先用CASE
做一个柱子,显示使用了多少样本,像这样:
查询
SELECT
id,
itemNo,
samplesLeft,
CASE
WHEN itemNo= 001
THEN 200 - samplesLeft
WHEN itemNo= 002
THEN 300 - samplesLeft
WHEN itemNo= 003
THEN 400 - samplesLeft
ELSE 100 - samplesLeft
END AS samplesUsed
FROM exampleTable
然后我想使用 samplesUsed 作为另一个 CASE
的输入来创建一个名为 factoryError
的布尔值。如果 samplesUsed < 10
,则为 TRUE。如何做到这一点?
这是一种糟糕的方法,但是如果您有一列包含样本起始数量的信息,您可以修复查询并使其变得更好....但这是一种获得您想要的东西的方法
create table #TempTableSam (itemNo nvarchar(5), samplesLeft int)
insert into #TempTableSam (itemNo, samplesLeft)
values ('001',50),('002',10),('003',20), ('004',80)
select itemNo, samplesLeft,
case
WHEN itemNo = '001' and 200 - samplesLeft < 190 then 'More than 10 samples has been used'
WHEN itemNo = '002' and 300 - samplesLeft < 280 then 'More than 10 samples has been used'
WHEN itemNo = '003' and 200 - samplesLeft < 190 then 'More than 10 samples has been used'
WHEN itemNo = '004' and 200 - samplesLeft < 190 then 'More than 10 samples has been used'
else 'Less then 10 samples has been used'
END as UsedSamples
from #TempTableSam
输出会是这样的
itemNo samplesLeft UsedSamples
001 50 More than 10 samples has been used
002 10 Less then 10 samples has been used
003 20 More than 10 samples has been used
004 80 More than 10 samples has been used
您可以使用像这样的子查询来做到这一点,
select
x.*,
case
when samplesUsed < 10 then 'Error'
else 'right'
end as status
from (
select
t.*,
CASE
WHEN itemNo = 001 THEN 200 - samplesLeft
WHEN itemNo = 002 THEN 300 - samplesLeft
WHEN itemNo = 003 THEN 400 - samplesLeft
ELSE 100 - samplesLeft
END AS samplesUsed
from tbl
) x
您可以将您的工作查询作为 CTE 并在下面继续...
WITH YourSELECTasCTE AS
(
SELECT
id,
itemNo,
samplesLeft,
CASE
WHEN itemNo= 001
THEN 200 - samplesLeft
WHEN itemNo= 002
THEN 300 - samplesLeft
WHEN itemNo= 003
THEN 400 - samplesLeft
ELSE 100 - samplesLeft
END AS samplesUsed
FROM exampleTable
)
SELECT * --do whatever you want here
FROM YourSELECTasCTE
你可以使用这个:
SELECT id ,
itemNo ,
samplesLeft ,
CASE WHEN ( CASE WHEN itemNo = '001' THEN 200 - samplesLeft
WHEN itemNo = '002' THEN 300 - samplesLeft
WHEN itemNo = '003' THEN 400 - samplesLeft
ELSE 100 - samplesLeft
END ) < 10 THEN 'factory born error'
ELSE 'ok'
END AS samplesUsed
FROM exampleTable
测试:
有人能告诉我如何使用 CASE
语句的输出作为 t-SQL 中下一个 CASE
语句的输入吗?
exampleTable
id | itemNo | samplesLeft
-------------------------
1 | 001 | 104
2 | 003 | 53
3 | 002 | 193
4 | 001 | 32
我想要一个查询 returns 所有这些,以及一个布尔值来判断它是否是出厂错误。如果使用的样本少于 10 个,则错误被认为是工厂产生的。我的想法是先用CASE
做一个柱子,显示使用了多少样本,像这样:
查询
SELECT
id,
itemNo,
samplesLeft,
CASE
WHEN itemNo= 001
THEN 200 - samplesLeft
WHEN itemNo= 002
THEN 300 - samplesLeft
WHEN itemNo= 003
THEN 400 - samplesLeft
ELSE 100 - samplesLeft
END AS samplesUsed
FROM exampleTable
然后我想使用 samplesUsed 作为另一个 CASE
的输入来创建一个名为 factoryError
的布尔值。如果 samplesUsed < 10
,则为 TRUE。如何做到这一点?
这是一种糟糕的方法,但是如果您有一列包含样本起始数量的信息,您可以修复查询并使其变得更好....但这是一种获得您想要的东西的方法
create table #TempTableSam (itemNo nvarchar(5), samplesLeft int)
insert into #TempTableSam (itemNo, samplesLeft)
values ('001',50),('002',10),('003',20), ('004',80)
select itemNo, samplesLeft,
case
WHEN itemNo = '001' and 200 - samplesLeft < 190 then 'More than 10 samples has been used'
WHEN itemNo = '002' and 300 - samplesLeft < 280 then 'More than 10 samples has been used'
WHEN itemNo = '003' and 200 - samplesLeft < 190 then 'More than 10 samples has been used'
WHEN itemNo = '004' and 200 - samplesLeft < 190 then 'More than 10 samples has been used'
else 'Less then 10 samples has been used'
END as UsedSamples
from #TempTableSam
输出会是这样的
itemNo samplesLeft UsedSamples
001 50 More than 10 samples has been used
002 10 Less then 10 samples has been used
003 20 More than 10 samples has been used
004 80 More than 10 samples has been used
您可以使用像这样的子查询来做到这一点,
select
x.*,
case
when samplesUsed < 10 then 'Error'
else 'right'
end as status
from (
select
t.*,
CASE
WHEN itemNo = 001 THEN 200 - samplesLeft
WHEN itemNo = 002 THEN 300 - samplesLeft
WHEN itemNo = 003 THEN 400 - samplesLeft
ELSE 100 - samplesLeft
END AS samplesUsed
from tbl
) x
您可以将您的工作查询作为 CTE 并在下面继续...
WITH YourSELECTasCTE AS
(
SELECT
id,
itemNo,
samplesLeft,
CASE
WHEN itemNo= 001
THEN 200 - samplesLeft
WHEN itemNo= 002
THEN 300 - samplesLeft
WHEN itemNo= 003
THEN 400 - samplesLeft
ELSE 100 - samplesLeft
END AS samplesUsed
FROM exampleTable
)
SELECT * --do whatever you want here
FROM YourSELECTasCTE
你可以使用这个:
SELECT id ,
itemNo ,
samplesLeft ,
CASE WHEN ( CASE WHEN itemNo = '001' THEN 200 - samplesLeft
WHEN itemNo = '002' THEN 300 - samplesLeft
WHEN itemNo = '003' THEN 400 - samplesLeft
ELSE 100 - samplesLeft
END ) < 10 THEN 'factory born error'
ELSE 'ok'
END AS samplesUsed
FROM exampleTable
测试: