Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在oracle中使用case语句返回多个值_Sql_Oracle - Fatal编程技术网

Sql 如何在oracle中使用case语句返回多个值

Sql 如何在oracle中使用case语句返回多个值,sql,oracle,Sql,Oracle,我想从oracle中的查询返回多个值。例如: select count(*) from tablename a where asofdate='10-nov-2009' and a.FILENAME in (case when 1 = 1 then (select distinct filename from tablename where asofdate='10-nov-2009' and isin is null) else null end);

我想从oracle中的查询返回多个值。例如:

select count(*)
from tablename a 
where asofdate='10-nov-2009'
and a.FILENAME in (case 
    when 1 = 1 then (select distinct filename from tablename 
        where asofdate='10-nov-2009' and isin is null) 
    else null
end);
我得到错误:ora 01427单行子查询返回多行

请给我一些建议

谢谢,Deepak运行此查询:

select distinct filename from tablename 
where asofdate='10-nov-2009' and isin is null
您将看到,它返回多个导致ORA-01427的行。

运行此查询:

select distinct filename from tablename 
where asofdate='10-nov-2009' and isin is null

您将看到它返回多个行,这导致ORA-01427。

CASE语句不能返回多个值,它是一个处理一个值的函数

您的声明不需要它,此声明应适用于:

select count(*) 
from tablename a 
where asofdate='10-nov-2009' 
and a.FILENAME in (select distinct filename 
    from tablename 
    where asofdate='10-nov-2009'
    and isin is null);
也许您还考虑了另一个使用场景?大概是这样的: 挑选* 来自aTable 万一在哪里 什么时候 什么时候 另一端

那么用例可能不是正确的场景。也许这有助于你朝着正确的方向前进:

Select *
From aTable
Where <Case1> and  column1 in <Subselect1>
Or <Case2> and column1 in <Subselect2>
OR Not (<Case1> Or <Case2>) and column1 in <Subselect3>
选择*
来自aTable
第1列在哪里
或中的第1列
或不(或)和中的第1列

但是对于优化器来说,这可能是相当大的工作…

一个CASE语句不能返回多个值,它是一个处理一个值的函数

您的声明不需要它,此声明应适用于:

select count(*) 
from tablename a 
where asofdate='10-nov-2009' 
and a.FILENAME in (select distinct filename 
    from tablename 
    where asofdate='10-nov-2009'
    and isin is null);
也许您还考虑了另一个使用场景?大概是这样的: 挑选* 来自aTable 万一在哪里 什么时候 什么时候 另一端

那么用例可能不是正确的场景。也许这有助于你朝着正确的方向前进:

Select *
From aTable
Where <Case1> and  column1 in <Subselect1>
Or <Case2> and column1 in <Subselect2>
OR Not (<Case1> Or <Case2>) and column1 in <Subselect3>
选择*
来自aTable
第1列在哪里
或中的第1列
或不(或)和中的第1列

但是对于优化器来说,这可能是一个相当大的工作…

您Case语句中的distinct试图返回多个值,而您的SELECT语句当前只返回一行中的一个值。如果要获取每个文件名的计数,请执行以下操作

SELECT FileName, Count(*)
FROM tablename
WHERE asofdate='10-nov-2009' and isin is null
GROUP BY FileName

Case语句中的distinct试图返回多个值,但只允许返回一个值,SELECT语句当前只返回一行中的一个值。如果要获取每个文件名的计数,请执行以下操作

SELECT FileName, Count(*)
FROM tablename
WHERE asofdate='10-nov-2009' and isin is null
GROUP BY FileName

据我所知,你正在寻找类似的东西:

select a.filename, count(*)
from tablename a
where a.asofdate = '10-nov-2009' 
and exists (
    select *
    from tablename b
    where b.isin is null
    and a.asofdate = '10-nov-2009' 
    and a.filename = b.filename
)
group by a.filename
这将查找一天的文件名计数,其中至少有一行
isin为null


如果您编辑您的问题并添加对您要查找的内容的解释,您可能会得到更好的答案。

据我所知,您正在查找以下内容:

select a.filename, count(*)
from tablename a
where a.asofdate = '10-nov-2009' 
and exists (
    select *
    from tablename b
    where b.isin is null
    and a.asofdate = '10-nov-2009' 
    and a.filename = b.filename
)
group by a.filename
这将查找一天的文件名计数,其中至少有一行
isin为null


如果你编辑你的问题并添加对你要找的内容的解释,你可能会得到更好的答案。

@CodeByMoonlight:做得很好!谢谢:)格式良好且缩进的SQL总是有帮助的。@CodeByMoonlight:做得很好!谢谢:)格式良好且缩进的SQL总是有帮助的。谢谢。是的,你是对的。我将case语句用于两种情况,例如,如果todays_data=Monthend_date使用文件名,而lb_%则使用returns_%等文件名。表中有1个以上的文件名。再次感谢汉克斯熨斗。是的,你是对的。我将case语句用于两种情况,例如,如果todays_data=Monthend_date使用文件名,而lb_%则使用returns_%等文件名。表中有1个以上的文件名。再次感谢