Postgres SQL:如何在此 SQL 中执行 GROUP BY

发布于 2024-10-05 05:15:17 字数 4364 浏览 0 评论 0原文

我们有以下 SQL,可以从审计表 family_audit 和 family_address_audit 生成报告。

(SELECT fam.familyserialno, fam.familyname, addr.housenumber, addr.address1,
        COALESCE(fam.operation, addr.operation) op,
        COALESCE(fam.username, addr.username) usr,
        COALESCE(fam.adddate, addr.adddate) add_date,
        (SELECT r.userrolename 
           FROM userrole_master r, user_master u 
          WHERE u.loginid=COALESCE(fam.username, addr.username) AND r.userrole_id=u.userrole_id) rolename
  FROM family_audit fam
       INNER JOIN family_address_audit addr USING(txid, family_id)
WHERE (fam.node_id = 8) AND (fam.familyserialno >=1 and fam.familyserialno <=5)
)
UNION
(SELECT fam.familyserialno, fam.familyname,
        addr.housenumber, addr.address1,
        COALESCE(fam.operation, addr.operation) op,
        COALESCE(fam.username, addr.username) usr,
        COALESCE(fam.adddate, addr.adddate) add_date,
        (SELECT r.userrolename 
           FROM userrole_master r, user_master u 
          WHERE u.loginid=COALESCE(fam.username, addr.username) AND r.userrole_id=u.userrole_id) rolename
   FROM family_audit fam
        FULL OUTER JOIN family_address_audit addr USING(txid, family_id)
  WHERE ((fam.familyserialno IS NULL and addr.family_id in 
             (select family_id 
                from family 
               where familyserialno >=1 and familyserialno <=5 and node_id =8)) 
         OR ((addr.family_id IS NULL) and ((fam.familyserialno >=1 and fam.familyserialno <=5) and fam.node_id=8) ) )
)
ORDER BY add_date DESC;

“上述”SQL 的典型输出如下

 familyserialno | familyname | housenumber |  address1  | op |  usr  |        add_date         | rolename
----------------+------------+-------------+------------+----+-------+-------------------------+----------
              3 | thirda     |             |            | U  | admin | 2010-11-24 15:29:34.312 | admin
                |            | 34324       | sdfdsfdsf  | U  | admin | 2010-11-24 15:28:42.314 | admin
              3 | third      |             |            | I  | admin | 2010-11-24 15:28:34.576 | admin
                |            | 333         | sdfdsf     | U  | admin | 2010-11-24 15:18:57.129 | admin
              2 | aSecond    |             |            | I  | admin | 2010-11-24 14:58:15.477 | admin
              1 | anjanFirst | anjanFirst  | anjanFirst | I  | admin | 2010-11-24 14:12:35.477 | admin

========================

我想按 fam.familyserialno 对输出进行分组。所以,我将上面的SQL修改如下;但是,这会导致错误(最后)

SELECT * from 
((SELECT fam.familyserialno, fam.familyname, addr.housenumber, addr.address1,
        COALESCE(fam.operation, addr.operation) op,
        COALESCE(fam.username, addr.username) usr,
        COALESCE(fam.adddate, addr.adddate) add_date,
        (SELECT r.userrolename 
           FROM userrole_master r, user_master u 
          WHERE u.loginid=COALESCE(fam.username, addr.username) AND r.userrole_id=u.userrole_id) rolename
  FROM family_audit fam
       INNER JOIN family_address_audit addr USING(txid, family_id)
WHERE (fam.node_id = 8) AND (fam.familyserialno >=1 and fam.familyserialno <=5)
)
UNION
(SELECT fam.familyserialno, fam.familyname,
        addr.housenumber, addr.address1,
        COALESCE(fam.operation, addr.operation) op,
        COALESCE(fam.username, addr.username) usr,
        COALESCE(fam.adddate, addr.adddate) add_date,
        (SELECT r.userrolename 
           FROM userrole_master r, user_master u 
          WHERE u.loginid=COALESCE(fam.username, addr.username) AND r.userrole_id=u.userrole_id) rolename
   FROM family_audit fam
        FULL OUTER JOIN family_address_audit addr USING(txid, family_id)
  WHERE ((fam.familyserialno IS NULL and addr.family_id in 
             (select family_id 
                from family 
               where familyserialno >=1 and familyserialno <=5 and node_id =8)) 
         OR ((addr.family_id IS NULL) and ((fam.familyserialno >=1 and fam.familyserialno <=5) and fam.node_id=8) ) )
))
GROUP BY 1 
ORDER BY 7 DESC;

错误:FROM 中的子查询必须有别名 第 2 行:((选择 fam.familyserialno、fam.familyname、addr.housenumbe... ^ 提示:例如,FROM (SELECT ...) [AS] foo。

如何解决此问题?我们确实需要按 fam.familyserialno 对所有行进行分组。

非常感谢,

PS:我们使用的是 Postgres 8.4.4。

We have the below SQL that generates a report from the Audit Tables family_audit and family_address_audit.

(SELECT fam.familyserialno, fam.familyname, addr.housenumber, addr.address1,
        COALESCE(fam.operation, addr.operation) op,
        COALESCE(fam.username, addr.username) usr,
        COALESCE(fam.adddate, addr.adddate) add_date,
        (SELECT r.userrolename 
           FROM userrole_master r, user_master u 
          WHERE u.loginid=COALESCE(fam.username, addr.username) AND r.userrole_id=u.userrole_id) rolename
  FROM family_audit fam
       INNER JOIN family_address_audit addr USING(txid, family_id)
WHERE (fam.node_id = 8) AND (fam.familyserialno >=1 and fam.familyserialno <=5)
)
UNION
(SELECT fam.familyserialno, fam.familyname,
        addr.housenumber, addr.address1,
        COALESCE(fam.operation, addr.operation) op,
        COALESCE(fam.username, addr.username) usr,
        COALESCE(fam.adddate, addr.adddate) add_date,
        (SELECT r.userrolename 
           FROM userrole_master r, user_master u 
          WHERE u.loginid=COALESCE(fam.username, addr.username) AND r.userrole_id=u.userrole_id) rolename
   FROM family_audit fam
        FULL OUTER JOIN family_address_audit addr USING(txid, family_id)
  WHERE ((fam.familyserialno IS NULL and addr.family_id in 
             (select family_id 
                from family 
               where familyserialno >=1 and familyserialno <=5 and node_id =8)) 
         OR ((addr.family_id IS NULL) and ((fam.familyserialno >=1 and fam.familyserialno <=5) and fam.node_id=8) ) )
)
ORDER BY add_date DESC;

A typical output from the "above" SQL is as below

 familyserialno | familyname | housenumber |  address1  | op |  usr  |        add_date         | rolename
----------------+------------+-------------+------------+----+-------+-------------------------+----------
              3 | thirda     |             |            | U  | admin | 2010-11-24 15:29:34.312 | admin
                |            | 34324       | sdfdsfdsf  | U  | admin | 2010-11-24 15:28:42.314 | admin
              3 | third      |             |            | I  | admin | 2010-11-24 15:28:34.576 | admin
                |            | 333         | sdfdsf     | U  | admin | 2010-11-24 15:18:57.129 | admin
              2 | aSecond    |             |            | I  | admin | 2010-11-24 14:58:15.477 | admin
              1 | anjanFirst | anjanFirst  | anjanFirst | I  | admin | 2010-11-24 14:12:35.477 | admin

======================

I want to group the output by fam.familyserialno. So, I modified the above SQL as below; However, that results in an error (at the end)

SELECT * from 
((SELECT fam.familyserialno, fam.familyname, addr.housenumber, addr.address1,
        COALESCE(fam.operation, addr.operation) op,
        COALESCE(fam.username, addr.username) usr,
        COALESCE(fam.adddate, addr.adddate) add_date,
        (SELECT r.userrolename 
           FROM userrole_master r, user_master u 
          WHERE u.loginid=COALESCE(fam.username, addr.username) AND r.userrole_id=u.userrole_id) rolename
  FROM family_audit fam
       INNER JOIN family_address_audit addr USING(txid, family_id)
WHERE (fam.node_id = 8) AND (fam.familyserialno >=1 and fam.familyserialno <=5)
)
UNION
(SELECT fam.familyserialno, fam.familyname,
        addr.housenumber, addr.address1,
        COALESCE(fam.operation, addr.operation) op,
        COALESCE(fam.username, addr.username) usr,
        COALESCE(fam.adddate, addr.adddate) add_date,
        (SELECT r.userrolename 
           FROM userrole_master r, user_master u 
          WHERE u.loginid=COALESCE(fam.username, addr.username) AND r.userrole_id=u.userrole_id) rolename
   FROM family_audit fam
        FULL OUTER JOIN family_address_audit addr USING(txid, family_id)
  WHERE ((fam.familyserialno IS NULL and addr.family_id in 
             (select family_id 
                from family 
               where familyserialno >=1 and familyserialno <=5 and node_id =8)) 
         OR ((addr.family_id IS NULL) and ((fam.familyserialno >=1 and fam.familyserialno <=5) and fam.node_id=8) ) )
))
GROUP BY 1 
ORDER BY 7 DESC;

ERROR: subquery in FROM must have an alias
LINE 2: ((SELECT fam.familyserialno, fam.familyname, addr.housenumbe...
^
HINT: For example, FROM (SELECT ...) [AS] foo.

How do I fix this ? We really need all the rows to be grouped by fam.familyserialno.

Thank you very much,

P.S : We are using Postgres 8.4.4.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两人的回忆 2024-10-12 05:15:19

您需要为子查询指定别名,如下所示:

 SELECT * FROM ((SELECT ...) UNION (SELECT ...)) AS foo GROUP BY 1 ORDER BY 7 DESC;

这里 foo 是子查询的别名。

You need to alias the subquery, as in:

 SELECT * FROM ((SELECT ...) UNION (SELECT ...)) AS foo GROUP BY 1 ORDER BY 7 DESC;

Here foo is an alias for the subquery.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文