我将使用什么类型的 sql 查询来打开以下内容;
| ID | SERIAL | LCN | INITLCN |
|------|----------|-------|---------|
| 1 | A | A1 | |
| 2 | B | A2 | |
| 3 | C | A3 | A1 |
| 4 | D | A4 | A2 |
| 5 | E | A5 | A1 |
|------|----------|-------|---------|
变成与此类似的结果;
| ID | COUNT |
|------|---------|
| 1 | 2 |
| 2 | 1 |
|------|---------|
使用我的低 SQL 技能,我设法编写了以下查询,但是它非常慢;
select
a.id,
count (b.id) as parent
from assets a
left join assets b
ON (a.lcn = b.initlcn)
group by a.id
order by a.id;