从嵌套在 CASE 中的 SELECT 返回多个值

发布于 2024-11-26 17:45:19 字数 1345 浏览 0 评论 0原文

我试图在单个列中返回链接到位置编号(1 到 6)的多个值,但仅返回在每种情况下处于特定位置的那些值。基本上我想要创建 6 列,每个位置 1 列,并返回与该位置编号关联的所有结果值。我创建了 6 个 Nested Case 语句来尝试获得这些结果:

(CASE WHEN tbl_TestWells.well_result <> NULL
THEN 
(SELECT tbl_TestWells.well_result
FROM tbl_TestWells
WHERE tbl_TestWells.Well_Index = 1)
ELSE NULL END) AS  Well_1_Graded_Result,

问题是它们仅按照设置的方式返回 NULL 值。如果我取出案例,则会收到错误消息“子查询返回超过 1 个值。当子查询遵循 =、!=、<、<= 、>、>= 或子查询时,这是不允许的被用作表达式。”

编辑:完整的代码是视图的一部分,所以我将其简化为相关内容

SELECT DISTINCT
--Other Columns being selected

(CASE WHEN tbl_TestWells.well_result <> NULL
THEN 
(SELECT tbl_TestWells.well_result
FROM tbl_TestWells
WHERE tbl_TestWells.Well_Index = 1)
ELSE NULL END) AS  Well_1_Graded_Result,

--The other five well results are set up in the exact same format as above, just replacing 1 with the corresponding number

FROM tbl_TestCartridges

JOIN tbl_Tests ON
tbl_Tests.test_uid = tbl_TestCartridges.test_uid

JOIN tbl_Programs ON
tbl_Programs.program_uid = tbl_Tests.program_uid

JOIN tbl_Cartridges ON
tbl_Cartridges.system_uid = tbl_Programs.system_uid

JOIN tbl_TestWells ON
tbl_TestWells.test_cartridge_uid = tbl_TestCartridges.test_cartridge_uid

JOIN tbl_TestSamples ON
tbl_TestSamples.test_well_uid = tbl_TestWells.test_well_uid 

ORDER BY tbl_TestCartridges.barcode

; 

I'm trying to return multiple values in a single column which are linked to a position number (1 through 6), but only those values which are of a certain position in each case. Basically I have 6 columns I want to create, 1 for each position, and return all of the result values associated with that position number. I created 6 Nested Case statements to try to get these results:

(CASE WHEN tbl_TestWells.well_result <> NULL
THEN 
(SELECT tbl_TestWells.well_result
FROM tbl_TestWells
WHERE tbl_TestWells.Well_Index = 1)
ELSE NULL END) AS  Well_1_Graded_Result,

The problem is that they only return NULL values in the way it's set up. If I take out the case then I get the Error Message "subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."

EDIT: The full code is part a view so I've simplified it to what's pertinent

SELECT DISTINCT
--Other Columns being selected

(CASE WHEN tbl_TestWells.well_result <> NULL
THEN 
(SELECT tbl_TestWells.well_result
FROM tbl_TestWells
WHERE tbl_TestWells.Well_Index = 1)
ELSE NULL END) AS  Well_1_Graded_Result,

--The other five well results are set up in the exact same format as above, just replacing 1 with the corresponding number

FROM tbl_TestCartridges

JOIN tbl_Tests ON
tbl_Tests.test_uid = tbl_TestCartridges.test_uid

JOIN tbl_Programs ON
tbl_Programs.program_uid = tbl_Tests.program_uid

JOIN tbl_Cartridges ON
tbl_Cartridges.system_uid = tbl_Programs.system_uid

JOIN tbl_TestWells ON
tbl_TestWells.test_cartridge_uid = tbl_TestCartridges.test_cartridge_uid

JOIN tbl_TestSamples ON
tbl_TestSamples.test_well_uid = tbl_TestWells.test_well_uid 

ORDER BY tbl_TestCartridges.barcode

; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诠释孤独 2024-12-03 17:45:19

CASE 语句不能用于导致出现 6 行来代替 1 行。您需要将“1 行或多行”逻辑构建到查询的 JOIN 中,而不是 CASE 语句中。

包括完整的查询和架构,以及对您想要实现的目标的解释。

A CASE statement can't be used to cause 6 rows to appear in place of one row. You need to build your "1 or many rows" logic into the JOINs of your query, not the CASE statement.

Include your full query and schema, and an explanation of what you're trying to achieve.

扭转时空 2024-12-03 17:45:19

检查 NULL 值时不能使用逻辑运算符。 NULL代表未知,那么1=NULL吗?编号为1《》无效的?不,因为在这种情况下 NULL可能为 1 - 我们不知道,它是未知的。

相反,您需要使用 IS NULLIS NOT NULL 进行 NULL 检查和比较。

所以,它看起来像这样:

(CASE WHEN tbl_TestWells.well_result IS NOT NULL THEN ...

You can't use logical operators when checking for NULL values. NULL represents unknown, so is 1 = NULL? No. Is 1 <> NULL? No, because NULL could be 1 in this case - we don't know, its unknown.

Instead, you need to use IS NULL and IS NOT NULL to do NULL checks and comparisons.

So, it would look like this:

(CASE WHEN tbl_TestWells.well_result IS NOT NULL THEN ...

情泪▽动烟 2024-12-03 17:45:19

我猜您有几 (6) 行 tbl_TestWells.Well_Index = 1, 2, 3... 并且您想将这些结果收集在一行中?

如果是,您需要类似的东西:

SELECT
    MIN( CASE WHEN tbl_TestWells.well_result IS NOT NULL
                   AND tbl_TestWells.Well_Index = 1
              THEN tbl_TestWells.well_result
         END
       ) AS  Well_1_Graded_Result
  , MIN( CASE WHEN tbl_TestWells.well_result IS NOT NULL
                  AND tbl_TestWells.Well_Index = 2
              THEN tbl_TestWells.well_result
         END 
       ) AS  Well_2_Graded_Result
  , ...
FROM 
    ...
GROUP BY
         ?                         --- what do these (6) rows have in common?
ORDER BY
         tbl_TestCartridges.barcode

I guess that you have several (6) rows with tbl_TestWells.Well_Index = 1, 2, 3... and you want to gather these results in one row?

If yes, you need something like:

SELECT
    MIN( CASE WHEN tbl_TestWells.well_result IS NOT NULL
                   AND tbl_TestWells.Well_Index = 1
              THEN tbl_TestWells.well_result
         END
       ) AS  Well_1_Graded_Result
  , MIN( CASE WHEN tbl_TestWells.well_result IS NOT NULL
                  AND tbl_TestWells.Well_Index = 2
              THEN tbl_TestWells.well_result
         END 
       ) AS  Well_2_Graded_Result
  , ...
FROM 
    ...
GROUP BY
         ?                         --- what do these (6) rows have in common?
ORDER BY
         tbl_TestCartridges.barcode
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文