英文:
SQL multiple data count from multiple tables with union all
问题
我的现有SQL如下,它将仅生成total_pending_req
计数。
SELECT count(table1.employee_code) as total_requests, table1.employee_code as emp_code
FROM table1
WHERE employee_status = 'PENDING'
GROUP BY emp_code
UNION ALL
SELECT count(table2.employee_code) as total_requests, table2.employee_code as emp_code
FROM table2
WHERE employee_status = 'PENDING'
GROUP BY emp_code
UNION ALL
SELECT count(table3.employee_code) as total_requests, table3.employee_code as emp_code
FROM table3
WHERE employee_status = 'PENDING'
GROUP BY emp_code
这将返回以下结果。
我想要获取请求计数作为total_pending_req
、total_rejected_req
和total_completed_req
,考虑到3个不同的表。所有表都具有相同的状态代码,PENDING
、COMPLETED
和REJECTED
。最终结果应该如下所示。
我想了解如何更有效地提取数据,因为我必须使用UNION ALL
。我想知道是否有更好的方法来更有效地提取数据?我将感激您的帮助。
英文:
My existing SQL looks like the below, it will generate only the total_pending_req
count.
SELECT count(table1.employee_code) as total_requests, table1.employee_code as emp_code
FROM table1
WHERE employee_status = 'PENDING'
GROUP BY emp_code
UNION ALL
SELECT count(table2.employee_code) as total_requests, table2.employee_code as emp_code
FROM table2
WHERE employee_status = 'PENDING'
GROUP BY emp_code
UNION ALL
SELECT count(table3.employee_code) as total_requests, table3.employee_code as emp_code
FROM table3
WHERE employee_status = 'PENDING'
GROUP BY emp_code
This will return the result below,
I want to get the request count as total_pending_req
, total_rejected_req
and total_completed_req
considering 3 different tables. All tables have the same status codes, PENDING
, COMPLETED
and REJECTED
. The final result should be like this,
I would like to have an idea, of how to extract data more efficiently, since I have to use UNION ALL
. May I know if there is any better approach to extract data more efficiently? I would appreciate your help on this.
答案1
得分: 2
你的行出现在多个表{1,2,3}关系中,只是一个让人烦恼的干扰。让我们把它变成一个单一的关系。我们可以创建一个表或一个视图。
创建视图 combined 作为
(SELECT * FROM table1
UNION ALL
SELECT * FROM table2
UNION ALL
SELECT * FROM table3
UNION ALL
)
好!
搞定之后,GROUP BY 就变得很简单了。
SELECT 员工编码, 员工状态, COUNT(*)
FROM combined
GROUP BY 员工编码, 员工状态
如果你不喜欢 DDL,可以将其表达为 CTE。或者创建一个视图,执行 UNION ALL 的繁重工作。或者使用 MATERIALIZED VIEW,随你喜欢。
输出格式是三个员工 × 三种状态,共九行。
如果你真的需要三行,随时可以从该关系中选择以重新格式化它。
英文:
The fact that your rows appear in the
several table{1,2,3} relations is just
an annoying distraction.
Let's make it a single relation, already.
We could create a table or a view.
CREATE VIEW combined AS
(SELECT * FROM table1
UNION ALL
SELECT * FROM table2
UNION ALL
SELECT * FROM table3
UNION ALL
)
Good!
With that out of the way, it becomes a trivial GROUP BY.
SELECT employee_code, employee_status, COUNT(*)
FROM combined
GROUP BY employee_code, employee_status
You can phrase it as a
CTE
if you're averse to DDL.
Or create a VIEW that does the UNION ALL heavy lifting.
Or a MATERIALIZED VIEW, whatever.
The output format is three emps × three statuses,
or nine rows.
If you really need three rows, feel free to SELECT
from that relation to re-format it.
答案2
得分: 0
你可以使用以下的CASE
和Union All
来实现:
select
table1.employee_code as emp_code,
case when employee_status = 'PENDING' then count(table1.employee_code) else 0 end as PENDING,
case when employee_status = 'COMPLETED' then count(table1.employee_code) else 0 end as COMPLETED,
case when employee_status = 'REJECTED' then count(table1.employee_code) else 0 end as REJECTED
from table1
GROUP BY emp_code
UNION ALL
select
table2.employee_code as emp_code,
case when employee_status = 'PENDING' then count(table2.employee_code) else 0 end as PENDING,
case when employee_status = 'COMPLETED' then count(table2.employee_code) else 0 end as COMPLETED,
case when employee_status = 'REJECTED' then count(table2.employee_code) else 0 end as REJECTED
from table2
GROUP BY emp_code
UNION ALL
select
table3.employee_code as emp_code,
case when employee_status = 'PENDING' then count(table3.employee_code) else 0 end as PENDING,
case when employee_status = 'COMPLETED' then count(table3.employee_code) else 0 end as COMPLETED,
case when employee_status = 'REJECTED' then count(table3.employee_code) else 0 end as REJECTED
from table3
GROUP BY emp_code
或者使用SUM
和Case
:
SELECT
table1.employee_code as emp_code,
SUM(CASE when employee_status = 'PENDING' Then 1 Else 0 End ) as PENDING,
SUM(CASE when employee_status = 'COMPLETED' Then 1 Else 0 End ) as COMPLETED,
SUM(CASE when employee_status = 'REJECTED' Then 1 Else 0 End ) as REJECTED
from table1
GROUP BY emp_code
UNION ALL
SELECT
table2.employee_code as emp_code,
SUM(CASE when employee_status = 'PENDING' Then 1 Else 0 End ) as PENDING,
SUM(CASE when employee_status = 'COMPLETED' Then 1 Else 0 End ) as COMPLETED,
SUM(CASE when employee_status = 'REJECTED' Then 1 Else 0 End ) as REJECTED
from table2
GROUP BY emp_code
UNION ALL
SELECT
table3.employee_code as emp_code,
SUM(CASE when employee_status = 'PENDING' Then 1 Else 0 End ) as PENDING,
SUM(CASE when employee_status = 'COMPLETED' Then 1 Else 0 End ) as COMPLETED,
SUM(CASE when employee_status = 'REJECTED' Then 1 Else 0 End ) as REJECTED
from table3
GROUP BY emp_code
英文:
you can use CASE
and Union All
as follows
select
table1.employee_code as emp_code,
case employee_status = 'PENDING' then count(table1.employee_code) else 0 end as PENDING,
case employee_status = 'COMPLETED' then count(table1.employee_code) else 0 end as COMPLETED,
case employee_status = 'REJECTED' then count(table1.employee_code) else 0 end as REJECTED
from FROM table1
GROUP BY emp_code
UNION ALL
select
table2.employee_code as emp_code,
case employee_status = 'PENDING' then count(table2.employee_code) else 0 end as PENDING,
case employee_status = 'COMPLETED' then count(table2.employee_code) else 0 end as COMPLETED,
case employee_status = 'REJECTED' then count(table2.employee_code) else 0 end as REJECTED
from FROM table2
GROUP BY emp_code
UNION ALL
select
table3.employee_code as emp_code,
case employee_status = 'PENDING' then count(table3.employee_code) else 0 end as PENDING,
case employee_status = 'COMPLETED' then count(table3.employee_code) else 0 end as COMPLETED,
case employee_status = 'REJECTED' then count(table3.employee_code) else 0 end as REJECTED
from FROM table3
GROUP BY emp_code
Or using SUM
and Case
SELECT
table1.employee_code as emp_code,
SUM(CASE employee_status = 'PENDING' Then 1 Else 0 End ) as PENDING,
SUM(CASE employee_status = 'COMPLETED' Then 1 Else 0 End ) as COMPLETED,
SUM(CASE employee_status = 'REJECTED' Then 1 Else 0 End ) as REJECTED
from FROM table1
GROUP BY emp_code
UNION ALL
SELECT
table2.employee_code as emp_code,
SUM(CASE employee_status = 'PENDING' Then 1 Else 0 End ) as PENDING,
SUM(CASE employee_status = 'COMPLETED' Then 1 Else 0 End ) as COMPLETED,
SUM(CASE employee_status = 'REJECTED' Then 1 Else 0 End ) as REJECTED
from FROM table2
GROUP BY emp_code
UNION ALL
SELECT
table3.employee_code as emp_code,
SUM(CASE employee_status = 'PENDING' Then 1 Else 0 End ) as PENDING,
SUM(CASE employee_status = 'COMPLETED' Then 1 Else 0 End ) as COMPLETED,
SUM(CASE employee_status = 'REJECTED' Then 1 Else 0 End ) as REJECTED
from FROM table3
GROUP BY emp_code
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论