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

时间:2009-11-11 12:15:11

标签: 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);

我收到错误:ora 01427单行子查询返回多行

请建议。

谢谢,迪帕克

4 个答案:

答案 0 :(得分:3)

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);

也许您还有另一种使用方案?像这样的东西:     选择 *     来自一个表格     CASE中的位置         然后呢         然后呢         ELSE END

然后使用CASE可能不是正确的方案。也许这有助于您朝着正确的方向前进:

Select *
From aTable
Where <Case1> and  column1 in <Subselect1>
Or <Case2> and column1 in <Subselect2>
OR Not (<Case1> Or <Case2>) and column1 in <Subselect3>

但这对于优化器来说可能是相当有用的工作......

答案 1 :(得分:3)

Case语句中的 distinct 尝试在只允许一个值时返回多个值,而SELECT语句当前只返回一行中的一个值。如果您正在尝试获取每个文件名的计数,请执行

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

答案 2 :(得分:0)

运行此查询:

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

你会看到它返回多行,导致ORA-01427。

答案 3 :(得分:0)

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

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 is null

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