PostgreSQL无法进行自我联接

时间:2019-06-27 20:27:18

标签: sql postgresql

我有一张桌子:

| acctg_cath_id | parent | description |
| 1             | 20     | Bills       |
| 9             | 20     | Invoices    |
| 20            |        | Expenses    |
| 88            | 30     |
| 89            | 30     |
| 30            |        |

我想创建一个自我联接,以便将我的物品归为一个父母。

尝试过此方法,但不起作用:

SELECT
    accounting.categories.acctg_cath_id,
    accounting.categories.parent

FROM accounting.categories a1, accounting.categories a2

WHERE a1.acctg_cath_id=a2.parent

我收到错误消息:invalid reference to FROM-clause entry for table "categories"

当我尝试:

a.accounting.categories.acctg_cath_id
b.accounting.categories.acctg_cath_id

我收到错误消息:cross-database references are not implemented: a.accounting.categories.acctg_cath_id


所需的输出:

  • 费用(家长20)
    • 账单(儿童1)
    • 发票(9岁儿童)

我在这里做什么错了?

4 个答案:

答案 0 :(得分:1)

似乎您只想对行进行排序:

import cv2
import numpy as np
import pandas as pd

img = cv2.imread('image.png', cv2.IMREAD_COLOR)

grayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

thresholded = cv2.threshold(grayscale, 0, 255, cv2.THRESH_OTSU)

cv2.imwrite("image.png", thresholded)

bbox = cv2.boundingRect(thresholded)

x, y, w, h = bbox

print(bbox)

foreground = img[y:y+h, x:x+w]

cv2.imwrite("foreground.png", foreground)

结果:

+---------------+--------+-------------+
| acctg_cath_id | parent | description |
+---------------+--------+-------------+
| 20            |        | Expenses    |
| 1             | 20     | Bills       |
| 9             | 20     | Invoices    |
| 30            |        |             |
| 88            | 30     |             |
| 89            | 30     |             |
+---------------+--------+-------------+

答案 1 :(得分:0)

您的语法正在执行交叉联接: FROM accounting.categories a1, accounting.categories a2

尝试以下操作:

SELECT
    a2.acctg_cath_id,
    a2.parent
FROM accounting.categories a1
    JOIN accounting.categories a2 ON (a1.acctg_cath_id = a2.parent)
;

检查DBFiddle

答案 2 :(得分:0)

您不需要分组,只需自我加入即可。

select 
  c.acctg_cath_id parentid, c.description parent,
  cc.acctg_cath_id childid, cc.description child
from (
  select distinct parent
  from categories
) p inner join categories c
on p.parent = c.acctg_cath_id
inner join categories cc on cc.parent = p.parent
where p.parent = 20

如果希望所有的父母和所有的孩子都可以删除WHERE子句。
请参见demo
结果:

> parentid | parent   | childid | child   
> -------: | :------- | ------: | :-------
>       20 | Expences |       1 | Bills   
>       20 | Expences |       9 | Invoices

答案 3 :(得分:0)

您不需要自我加入。您不需要聚合。您只需要一个Match子句:

group by