ORACLE - 从两个表中进行分组计数
ORACLE - Count from two tables with group by
我有两个 table(table1 和 table2)共享一个列(day_code 编号)。
我想从最小值 day_code 获取每个 table 的记录数,然后按结果分组 day_code。
Table 1(记录数day_code)
20160703 - 5
20160704 - 4
Table 2(day_code的记录数)
20160703 - 5
20160704 - 4
我需要这样的东西:
----------------------------------------------------
DAY_CODE | TABLE 1 | TABLE 2 |
20160703 | 5 | 5 |
20160704 | 4 | 4 |
我正在使用该查询:
SELECT *
FROM
(
SELECT day_code, COUNT(day_code) AS TB1 FROM TABLE1 GROUP BY day_code
UNION ALL
SELECT day_code, COUNT(day_code) AS TB2 FROM TABLE2 GROUP BY day_code
) s
where day_code between 20160703 and 20160704
我得到了这个:
DAY_CODE | TB1
20160703 | 5
20160704 | 4
20160703 | 5
20160704 | 4
你能帮帮我吗?
提前感谢您的建议,
LR
尝试:
SELECT coalesce( t1.day_code, t2.day_code) As daycode,
nvl( cnt1, 0 ) cnt1,
nvl( cnt2, 0 ) cnt2
FROM (
SELECT day_code, count(*) cnt1
FROM tab1
GROUP BY day_code
) t1
FULL OUTER JOIN (
SELECT day_code, count(*) cnt2
FROM tab2
GROUP BY day_code
) t2
ON t1.day_code = t2.day_code
ORDER BY 1
这是一个使用数据透视表的解决方案。我创建了更多数据以显示对空值的正确处理。
with table1 (day_code, ct) as (
select 20160703, 5 from dual union all
select 20160704, 4 from dual union all
select 20160705, 7 from dual
),
table2 (day_code, ct) as (
select 20160703, 5 from dual union all
select 20160704, 8 from dual
)
select *
from (select day_code, ct, 1 as t from table1
union all
select day_code, ct, 2 as t from table2
)
pivot (min(ct) for t in (1 as table1, 2 as table2))
order by day_code;
输出:
DAY_CODE TABLE1 TABLE2
---------- ---------- ----------
20160703 5 5
20160704 4 8
20160705 7
我有两个 table(table1 和 table2)共享一个列(day_code 编号)。
我想从最小值 day_code 获取每个 table 的记录数,然后按结果分组 day_code。
Table 1(记录数day_code)
20160703 - 5
20160704 - 4
Table 2(day_code的记录数)
20160703 - 5
20160704 - 4
我需要这样的东西:
----------------------------------------------------
DAY_CODE | TABLE 1 | TABLE 2 |
20160703 | 5 | 5 |
20160704 | 4 | 4 |
我正在使用该查询:
SELECT *
FROM
(
SELECT day_code, COUNT(day_code) AS TB1 FROM TABLE1 GROUP BY day_code
UNION ALL
SELECT day_code, COUNT(day_code) AS TB2 FROM TABLE2 GROUP BY day_code
) s
where day_code between 20160703 and 20160704
我得到了这个:
DAY_CODE | TB1
20160703 | 5
20160704 | 4
20160703 | 5
20160704 | 4
你能帮帮我吗?
提前感谢您的建议, LR
尝试:
SELECT coalesce( t1.day_code, t2.day_code) As daycode,
nvl( cnt1, 0 ) cnt1,
nvl( cnt2, 0 ) cnt2
FROM (
SELECT day_code, count(*) cnt1
FROM tab1
GROUP BY day_code
) t1
FULL OUTER JOIN (
SELECT day_code, count(*) cnt2
FROM tab2
GROUP BY day_code
) t2
ON t1.day_code = t2.day_code
ORDER BY 1
这是一个使用数据透视表的解决方案。我创建了更多数据以显示对空值的正确处理。
with table1 (day_code, ct) as (
select 20160703, 5 from dual union all
select 20160704, 4 from dual union all
select 20160705, 7 from dual
),
table2 (day_code, ct) as (
select 20160703, 5 from dual union all
select 20160704, 8 from dual
)
select *
from (select day_code, ct, 1 as t from table1
union all
select day_code, ct, 2 as t from table2
)
pivot (min(ct) for t in (1 as table1, 2 as table2))
order by day_code;
输出:
DAY_CODE TABLE1 TABLE2
---------- ---------- ----------
20160703 5 5
20160704 4 8
20160705 7