组合不同表的两个查询的SQL
我对联接表有以下查询:组合不同表的两个查询的SQL,sql,oracle,Sql,Oracle,我对联接表有以下查询: select scholarship_id as scholId, count(scholarship_id) as incompleteCount from applicant_scholarship group by scholarship_id 及 我希望将这两个查询结合起来,给我一个带有scholId、incompleteCount和completeCount的表。有人能帮忙吗 在我的解决方案中使用了以下内容: SELECT scholId,
select scholarship_id as scholId,
count(scholarship_id) as incompleteCount
from applicant_scholarship
group by scholarship_id
及
我希望将这两个查询结合起来,给我一个带有scholId、incompleteCount和completeCount的表。有人能帮忙吗
在我的解决方案中使用了以下内容:
SELECT scholId,
SUM (completeCount) AS completeCount,
SUM (incompleteCount) AS incompleteCount,
SUM (completeCount) + SUM (incompleteCount) AS totalCount
FROM ( SELECT scholarship_id AS scholId,
COUNT (scholarship_id) AS incompleteCount,
NULL AS completeCount
FROM applicant_scholarship
GROUP BY scholarship_id
UNION
SELECT scholarship_id AS scholId, NULL, COUNT (scholarship_id)
FROM applicant_comp_schol
GROUP BY scholarship_id)
GROUP BY scholId
您可能还应该有一个包含完整记录列表的表,这将是您的基础(以确保捕获“零”)。假设您这样做了,您可以将两个查询作为子查询保留,并对其进行连接:
select
s.scholarship_id
,nvl(inc.num_records, 0) incompleteCount
,nvl(cpl.num_records, 0) completeCount
from
scholarships s
left join (
select
scholarship_id
,count(scholarship_id) num_records
from applicant_scholarship
group by scholarship_id
) inc on s.scholarship_id = inc.scholarship_id
left join (
select
scholarship_id
,count(scholarship_id) num_records
from applicant_comp_schol
group by scholarship_id
) cpl on s.scholarship_id = cpl.scholarship_id
如果您没有包含所有内容的实际“奖学金”表,那么您可以构建另一个子查询,将这两个表合并在一起以获得组合的唯一奖学金id值,然后将其用作基表。假设您的每个表中都有唯一的标识符,可以使用完全外部联接来完成以下操作:
SELECT COALESCE (aps.scholarship_id, acs.scholarship_id) AS scholid,
COUNT (DISTINCT aps.applicant_scholarship_id) AS incompletecount,
COUNT (DISTINCT acs.applicant_comp_schol_id) AS incompletecount
FROM applicant_scholarship aps
FULL OUTER JOIN applicant_comp_schol acs
ON aps.scholarship_id = acs.scholarship_id
GROUP BY scholarship_id
显然,我假设
申请人奖学金id
和申请人奖学金id
存在<代码>奖学金“ID /代码>不会在这些地方工作,因为它会导致错误的计数。您可能想考虑使用“联合所有”而不是“联合”,联合将在其自身上删除重复行。在这个特定的SQL中,必须使用联合标识,不会有任何重复行,但联合所有仍然更好。但我认为OP希望每个奖学金id都有一行,包括不完全计数和完全计数。@jarlh你是对的,这就是我想要的,尽管union all没有为每个奖学金id提供一行,包括不完全计数和完全计数。在这种情况下,按照AVK的说法,将其用作视图或子查询,然后按scol\u id分组。即,从视图中选择scholid,sum(completeCount)+sum(completeCount)作为totalCount
select
s.scholarship_id
,nvl(inc.num_records, 0) incompleteCount
,nvl(cpl.num_records, 0) completeCount
from
scholarships s
left join (
select
scholarship_id
,count(scholarship_id) num_records
from applicant_scholarship
group by scholarship_id
) inc on s.scholarship_id = inc.scholarship_id
left join (
select
scholarship_id
,count(scholarship_id) num_records
from applicant_comp_schol
group by scholarship_id
) cpl on s.scholarship_id = cpl.scholarship_id
SELECT COALESCE (aps.scholarship_id, acs.scholarship_id) AS scholid,
COUNT (DISTINCT aps.applicant_scholarship_id) AS incompletecount,
COUNT (DISTINCT acs.applicant_comp_schol_id) AS incompletecount
FROM applicant_scholarship aps
FULL OUTER JOIN applicant_comp_schol acs
ON aps.scholarship_id = acs.scholarship_id
GROUP BY scholarship_id