PL/SQL-How 当表和条件都不同时,在单个查询中实现多个计数语句
PL/SQL-How to implement multiple count statements in a single query when tables and conditions are all different
我正在开发使用 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 个查询都有非常不同的条件,因此将它们组合起来对我来说似乎是一项艰巨的任务。请帮帮我。
谢谢。
使用 UNION ALL 语句
例如。
select 计数 (a.x) 来自...哪里...
联合所有
select 计数 (b.z) 从 b...哪里...
联合所有
select count (c.y) 来自 c...where...
等等
注意:您必须使用 UNION ALL,因为如果您使用常规 UNION,将不会显示重复的结果
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;
我正在开发使用 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 个查询都有非常不同的条件,因此将它们组合起来对我来说似乎是一项艰巨的任务。请帮帮我。
谢谢。
使用 UNION ALL 语句
例如。 select 计数 (a.x) 来自...哪里... 联合所有 select 计数 (b.z) 从 b...哪里... 联合所有 select count (c.y) 来自 c...where... 等等
注意:您必须使用 UNION ALL,因为如果您使用常规 UNION,将不会显示重复的结果
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;