簡體   English   中英

在 PostgreSQL 中使用 COUNT 與 JOIN 和 GROUP BY

[英]Using COUNT with JOIN and GROUP BY in PostgreSQL

我有幾張桌子。 第一個名為category ,這里是:

id|template_id|name   |entry_count|is_base_template|can_rename|can_delete|section|userId|parentCategoryId|
--|-----------|-------|-----------|----------------|----------|----------|-------|------|----------------|
 1|           |Notes  |          0|true            |true      |true      |A      |      |                |
 2|           |ToDo   |          0|true            |true      |true      |A      |      |                |
 3|          1|Notes  |          0|false           |true      |true      |A      |     1|                |
 4|          2|ToDo   |          0|false           |true      |true      |A      |     1|                |
 5|           |Must Do|          0|false           |          |          |A      |      |               4|

第二個表稱為entry

id|body                                         |title               |createdOn          |startDate|finishDate|isArchived|isLocked|isTrashed|
--|---------------------------------------------|--------------------|-------------------|---------|----------|----------|--------|---------|
 1|im a root entry                              |root entry          |2020-06-11 22:40:01|         |          |          |        |         |
 2|I must do it                                 |must do title       |2020-06-11 22:42:16|         |          |          |        |         |
 3|i was asked to do it right now and right here|one more must do    |2020-06-11 17:44:22|         |          |          |        |         |
 4|5 subcat body                                |5 subcat title      |2020-06-11 17:46:47|         |          |          |        |         |
 5|5 subcat body try 2                          |5 subcat title try 2|2020-06-11 17:51:26|         |          |          |        |         |
 6|5 subcat body try 3                          |5 subcat title try 3|2020-06-11 17:53:17|         |          |          |        |         |
 7|4 subcat body try 1                          |4 subcat title try 1|2020-06-11 17:54:34|         |          |          |        |         |

還有關系表(category_entries_entry):

categoryId|entryId|
----------|-------|
         4|      1|
         5|      5|
         5|      6|
         4|      7|

一個類別可以擁有孩子,如果 parentCategoryId 不是 NULL 那么我們正在處理它的孩子。 例如,第五類(id = 5)實際上是第四類的子類。 目前的要求之一是孩子不能有自己的孩子。

我需要的是計算每個類別的條目數,而不管一個類別有多少子類別。

如果我這樣做:

SELECT category.id as cat_id , COUNT(*) as entries_in_cat
FROM category
LEFT JOIN category_entries_entry
ON category.id = category_entries_entry."categoryId"
LEFT JOIN entry
ON entry.id =  category_entries_entry."entryId"
WHERE category.is_base_template = false
GROUP BY category.id;

這是我得到的:

cat_id|entries_in_cat|
------|--------------|
     5|             2|
     4|             2|
     3|             1|

問題是 id = 5 的類別是一個子類別,這意味着它應該添加到 id = 4 的類別中,因為這是她的 parentCategory id。 所以這個特殊情況的表格應該是這樣的:

cat_id|entries_in_cat|
------|--------------|
     4|             4|
     3|             1|

因此,問題是:我如何實現這一目標?

如果我理解正確,您希望加入categoryrelations並聚合父 ID(如果可用):

SELECT COALESCE(c.parent_id, c.id) as cat_id , COUNT(*) as entries_in_cat
FROM category c JOIN
     relations r
     ON c.id = r.category_id
WHERE NOT c.is_base_template
GROUP BY cat_id;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM