1

我对 postgresql 很陌生,想从我们的表中生成一些摘要数据

我们有一个简单的留言板表名称messages,其中包含一个元素ctg_uid。每个ctg_uid对应表中的一个类别名称categories

以下是分类select * from categories ORDER by ctg_uid ASC;

 ctg_uid |    ctg_category    | ctg_creator_uid 
---------+--------------------+-----------------
       1 | general            |               1
       2 | faults             |               1
       3 | computing          |               1
       4 | teaching           |               2
       5 | QIS-FEEDBACK       |               3
       6 | QIS-PHYS-FEEDBACK  |               3
       7 | SOP-?-CHANGE       |               3
       8 | agenda items       |               7
      10 | Acq & Process      |               2
      12 | physics-jobs       |               3
      13 | Tech meeting items |              12
      16 | incident-forms     |               3
      17 | ERRORS             |               3
      19 | Files              |              10
      21 | QIS-CAR            |               3
      22 | doses              |               4
      24 | admin              |               3
      25 | audit              |               3
      26 | For Sale           |               4
      31 | URGENT-REPORTS     |               4
      34 | dt-jobs            |               3
      35 | JOBS               |               3
      36 | IN-PATIENTS        |               4
      37 | Ordering           |               4
      38 | dep-meetings       |               4
      39 | reporting          |               4

我想做的是对我们的所有消息messages计算每个类别的频率

我可以按类别进行 SELECT count(msg_ctg_uid) FROM messages where msg_ctg_uid='13';

然而,有可能在一个班轮中做到这一点吗?

下面给出了每条消息的类别和 ctg_uid SELECT ctg_category, msg_ctg_uid FROM messages INNER JOIN categories ON (ctg_uid = msg_ctg_uid);

SELECT ctg_category, count(msg_ctg_uid) FROM messages INNER JOIN categories ON (ctg_uid = msg_ctg_uid);

给我错误ERROR: column "categories.ctg_category" must appear in the GROUP BY clause or be used in an aggregate function

如何汇总每个类别的频率?

4

1 回答 1

1

您缺少 group by 子句:

SELECT ctg_category, count(msg_ctg_uid) 
FROM messages INNER JOIN categories ON (ctg_uid = msg_ctg_uid);
GROUP BY ctg_category

这意味着你想要每ctg_category

于 2013-07-17T15:11:34.197 回答