PL/SQL-表和条件都不同时如何在单个查询中实现多条count语句

Posted

技术标签:

【中文标题】PL/SQL-表和条件都不同时如何在单个查询中实现多条count语句【英文标题】:PL/SQL-How to implement multiple count statements in a single query when tables and conditions are all different 【发布时间】:2016-06-24 17:08:03 【问题描述】:

我正在开发使用 PL/SQL 数据库的开放文本内容服务器工具。我想要做的是通过 6 个不同的查询获取计数数据,这些查询也有不同的条件和不同的表。我试图结合所有这 6 个计数查询,但没有运气。下面列出了这 6 个查询:

一个月内创建的文档:

select count (dataid) from Dtree where 
Createdate >= %1 and createdate <= %2 and subtype = 144

用户总数:

select count(a.id) from Kuaf a, kuaf b where 
a.deleted =0 and a.type =0 and b.id = a.groupid

每月登录的唯一用户数(计数):

Select count (distinct (performerID))
from dauditnew where auditid=23 and auditdate >= %1 and auditdate <= %2

一个月内创建的用户数(计数):

Select Count(dataid) FROM DAUDITNEW where AUDITID = 1 
AND AUDITSTR LIKE 'Create' and subtype=142 AND 
auditdate >= %1 and auditdate <= %2

删除的用户数(计数):

SELECT count(a.userid) from dauditnew a WHERE
a.auditstr = 'Delete' AND 
a.AuditDate >= %1 AND 
a.AuditDate <= %2 AND 
a.UserID in (Select ID from KUAF where Deleted = 1 and Type=0)

启动的工作流程:

Select count(*) from Wworkaudit WWA where WWA.workaudit_status=1 AND 
WWA.workaudit_date >= %1 and WWA.workaudit_date <= %2

这里 %1,%2 表示用户输入。由于这 6 个查询都有非常不同的条件,因此将它们组合起来对我来说似乎是一项艰巨的任务。请帮帮我。

谢谢。

【问题讨论】:

请定义“组合它们”。输出应该是什么样的?在单独的行上显示六个计数,并用描述来识别它们?或者将它们显示在六列中,并带有描述性的列名?还是什么? 通过组合我的意思是我想要一个查询来完成所有这 6 个查询的工作。我想用描述性的列名在 6 个不同的列中显示计数。谢谢 【参考方案1】:
SELECT (
         select count (dataid)
         from   Dtree
         where  Createdate BETWEEN :start_date and :end_date
         and    subtype = 144
       ) AS Docs_Per_Month,
       (
         select count(a.id)
         from   Kuaf a INNER JOIN kuaf b ON (b.id = a.groupid)
         where  a.deleted = 0
         and    a.type    = 0
       ) AS Total_No_of_Users,
       (
         Select count( distinct performerID )
         from   dauditnew
         where  auditid = 23
         and    auditdate BETWEEN :start_date and :end_date
       ) AS Unique_Users_in_Month,
       (
         Select Count(dataid)
         FROM   DAUDITNEW
         where  AUDITID  = 1 
         AND    AUDITSTR = 'Create'
         and    subtype  = 142
         AND    auditdate BETWEEN :start_date and :end_date
       ) AS Users_Created_in_Month,
       (
         SELECT count(a.userid)
         from   dauditnew a
         WHERE  a.auditstr = 'Delete'
         AND    a.auditdate BETWEEN :start_date and :end_date
         AND    a.UserID in (Select ID from KUAF where Deleted = 1 and Type=0)
       ) AS Users_Deleted,
       (
         Select count(*)
         from   Wworkaudit
         where  workaudit_status = 1
         AND    workaudit_date BETWEEN :start_date and :end_date
       ) AS Workflows_Initiated
FROM   DUAL;

【讨论】:

感谢 MT0 ??...您刚刚为我解决了一个非常头疼的问题。非常感谢??? 您好 MT0,是否可以按列而不是按行获取这些数据? 获取每个查询,然后使用UNION ALL 像这样SELECT 'Docs_per_month' AS name, count(dataid) AS frequency FROM dtree ... UNION ALL SELECT 'Total_No_Of_Users', COUNT(a.id) FROM Kauf a ... UNION ALL ... 连接它们【参考方案2】:

使用 UNION ALL 语句

例如。 从 a...where... 中选择 count (a.x) 联合所有 select count (b.z) from b...where... 联合所有 从 c... 中选择 count (c.y)... 等等

注意:必须使用UNION ALL,因为如果使用常规UNION,将不会显示重复的结果

【讨论】:

以上是关于PL/SQL-表和条件都不同时如何在单个查询中实现多条count语句的主要内容,如果未能解决你的问题,请参考以下文章

我们如何在 Oracle SQL 或 PL/SQL 中实现 Standard Normal CDF?

正则表达式:如何在 PL/SQL 中实现负向后查找

在 PL/SQL 中实现应用程序

在 pl/sql 过程中实现水平碎片表

PL/SQL 和 Oracle Forms Builder

有人可以帮我在 PL/SQL Oracle 中实现以下触发器吗?