简体   繁体   English

SQL CASE子查询

[英]SQL CASE sub-query

I have two tables, the following query to retrieve the os memory when max instance memory is unlimited and the error message: 我有两个表,以下查询可在最大实例内存不受限制时检索操作系统内存,并显示错误消息:

--osmemory                          --instancememory
servername  osmemory                servername  instancename  instancememory
----------  --------                ----------  ------------  --------------
srva        4096                    srva        srva\insta    2048
srvb        6144                    srvb        srvb\instb    2147483647

select i.ServerName
     , i.instancename
     , case i.instance memory LIKE '2147483647' 
         then (select o.osmemory from o.osmemory
               join i.instance on o.servername = i.servername)
...

Subquery returned more than 1 value. 子查询返回的值超过1。 This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。

I've been trying (and i'm still trying) various scripts, but with no luck. 我一直在尝试(而且我还在尝试)各种脚本,但是没有运气。

Can somebody help? 有人可以帮忙吗? Thanks 谢谢

The error is obvious. 错误很明显。 You are using a subquery in a context where one column and up to one row is allowed. 您在允许一列最多一行的上下文中使用子查询。 This is called a "scalar subquery". 这称为“标量子查询”。 One method to fix this uses an aggregation function: 解决此问题的一种方法是使用聚合函数:

select i.ServerName, i.instancename,
       (case i.instance memory LIKE '2147483647' 
            then (select max(o.osmemory) from o.osmemory join i.instance on o.servername = i.servername)
       . . . 

However, that might not be your intent. 但是,这可能不是您的意图。

EDTI: EDTI:

If I had to speculate, your problem is the join in the subquery. 如果让我猜测,你的问题是join在子查询。 You probably just want a correlated subquery. 您可能只需要一个相关的子查询。 This is a guess, but: 这是一个猜测,但是:

select i.ServerName, i.instancename,
       (case i.instance memory LIKE '2147483647' 
            then (select o.osmemory from o.osmemory where o.servername = i.servername)
       . . . 

还是使用Max(o.osmemory)或Max(o.osmemory)不管结果是否始终为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM