SQL多个表中的多个数据计数,使用UNION ALL。

huangapple go评论117阅读模式
英文:

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

这将返回以下结果。

SQL多个表中的多个数据计数,使用UNION ALL。

我想要获取请求计数作为total_pending_reqtotal_rejected_reqtotal_completed_req,考虑到3个不同的表。所有表都具有相同的状态代码,PENDINGCOMPLETEDREJECTED。最终结果应该如下所示。

SQL多个表中的多个数据计数,使用UNION ALL。

我想了解如何更有效地提取数据,因为我必须使用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,

SQL多个表中的多个数据计数,使用UNION ALL。

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,

SQL多个表中的多个数据计数,使用UNION ALL。

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

你可以使用以下的CASEUnion 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

或者使用SUMCase

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

huangapple
  • 本文由 发表于 2023年1月9日 13:17:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定