如何在没有不同的情况下获得总计数?
How to get the Total count without distinct?
这里是fiddle
我有一个 table 作为
TABLE_MAIN
+-----+----------+---------+
| id | name | phase |
+-----+----------+---------+
| 101 | Bolt | PHASE 1 |
| 102 | Nut | PHASE 1 |
| 103 | Screw | PHASE 2 |
| 104 | Hex BOLT | PHASE 2 |
| 105 | Rubber | PHASE 3 |
| 106 | Aluminum | PHASE 3 |
| 107 | Slate | PHASE 3 |
| 108 | Pen | PHASE 3 |
| 109 | Pencil | PHASE 3 |
+-----+----------+---------+
TABLE_ERROR
+-----+----------+---------+
| id | name | phase |
+-----+----------+---------+
| 101 | Bolt | PHASE 1 |
| 102 | Needle | PHASE 1 |
| 101 | Bolt | PHASE 3 |
| 102 | Needle | PHASE 3 |
| 104 | Bolt | PHASE 3 |
| 105 | Rubber | PHASE 3 |
| 105 | Plastic | PHASE 3 |
| 106 | Aluminum | PHASE 3 |
| 106 | Steel | PHASE 3 |
| 106 | Cooper | PHASE 3 |
+-----+----------+---------+
现在我试图找出每个阶段 table_error
中 PHASE 3
的 ID 在 table_main
中出现的次数。如果 ID 重复,则应将其添加到总数中。
预计
+---------+-----------------+-------+
| phase | already_present | total |
+---------+-----------------+-------+
| PHASE 1 | 2 | 8 |
| PHASE 2 | 1 | 8 |
| PHASE 3 | 5 | 8 |
+---------+-----------------+-------+
我试过了
SELECT phase, count(*) AS already_present, sum(count(*)) OVER () AS total
FROM table_main
WHERE id IN (
SELECT id
FROM table_error
WHERE phase = 'PHASE 3'
)
GROUP BY phase
但它给了我,
+---------+-----------------+-------+
| phase | already_present | total |
+---------+-----------------+-------+
| PHASE 1 | 2 | 5 |
| PHASE 2 | 1 | 5 |
| PHASE 3 | 2 | 5 |
+---------+-----------------+-------+
希望对您有所帮助
select tm.phase, count(tm.id) AS already_present, SUM(count(tm.id)) OVER() as total
from table_main tm
inner join table_error te on te.id = tm.id and te.phase = 'PHASE 3'
group by tm.phase
这里是fiddle
我有一个 table 作为
TABLE_MAIN
+-----+----------+---------+
| id | name | phase |
+-----+----------+---------+
| 101 | Bolt | PHASE 1 |
| 102 | Nut | PHASE 1 |
| 103 | Screw | PHASE 2 |
| 104 | Hex BOLT | PHASE 2 |
| 105 | Rubber | PHASE 3 |
| 106 | Aluminum | PHASE 3 |
| 107 | Slate | PHASE 3 |
| 108 | Pen | PHASE 3 |
| 109 | Pencil | PHASE 3 |
+-----+----------+---------+
TABLE_ERROR
+-----+----------+---------+
| id | name | phase |
+-----+----------+---------+
| 101 | Bolt | PHASE 1 |
| 102 | Needle | PHASE 1 |
| 101 | Bolt | PHASE 3 |
| 102 | Needle | PHASE 3 |
| 104 | Bolt | PHASE 3 |
| 105 | Rubber | PHASE 3 |
| 105 | Plastic | PHASE 3 |
| 106 | Aluminum | PHASE 3 |
| 106 | Steel | PHASE 3 |
| 106 | Cooper | PHASE 3 |
+-----+----------+---------+
现在我试图找出每个阶段 table_error
中 PHASE 3
的 ID 在 table_main
中出现的次数。如果 ID 重复,则应将其添加到总数中。
预计
+---------+-----------------+-------+
| phase | already_present | total |
+---------+-----------------+-------+
| PHASE 1 | 2 | 8 |
| PHASE 2 | 1 | 8 |
| PHASE 3 | 5 | 8 |
+---------+-----------------+-------+
我试过了
SELECT phase, count(*) AS already_present, sum(count(*)) OVER () AS total
FROM table_main
WHERE id IN (
SELECT id
FROM table_error
WHERE phase = 'PHASE 3'
)
GROUP BY phase
但它给了我,
+---------+-----------------+-------+
| phase | already_present | total |
+---------+-----------------+-------+
| PHASE 1 | 2 | 5 |
| PHASE 2 | 1 | 5 |
| PHASE 3 | 2 | 5 |
+---------+-----------------+-------+
希望对您有所帮助
select tm.phase, count(tm.id) AS already_present, SUM(count(tm.id)) OVER() as total
from table_main tm
inner join table_error te on te.id = tm.id and te.phase = 'PHASE 3'
group by tm.phase