本文介绍了Oracle-将多个计数作为一个查询返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个查询,详细说明如下.我希望能够运行一个同时返回两个计数的SQL查询,这可能吗?

I have a couple of queries, detailed below. I'd like to be able to run one SQL query which returns both counts, is this possible?

1.

select nvl(count(rowid), 0) from tablename where OPP = 'FOO' and date = 'BAZ';

2.

select nvl(count(rowid), 0) from tablename where OPP = 'BAR' and date = 'BAZ';

到目前为止,我仅在搜索中找到MSSQL特定的解决方案.

I've only found MSSQL specific solutions in my searches so far.

推荐答案

如果在一行中需要它们:

If you need them in a single row:

SELECT
    COUNT(CASE OPP WHEN 'FOO' THEN 1 END),
    COUNT(CASE OPP WHEN 'BAR' THEN 1 END)
FROM tablename
WHERE OPP IN ('FOO', 'BAR') AND date = 'BAZ'

(无论如何,Thilo的GROUP BY方法都是更好的通用解决方案.)

(The GROUP BY approach by Thilo is a better generic solution anyway.)

修改:我已删除NVL().我已经忘记了为什么我从不使用它.

I've removed NVL(). I had forgotten why I never use it.

这篇关于Oracle-将多个计数作为一个查询返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 03:38