4

我有以下表格:

用户组

usergrp_id      bigint     Primary Key
usergrp_name    text  

用户

user_id             bigint     Primary Key
user_name           text
user_usergrp_id     bigint
user_loc_id         bigint

user_usergrp_id有来自 user_group 表 user_loc_id的对应 id 有来自分支表的对应 id(branch_id)。

分支

branch_id      bigint     Primary Key
branch_name    text
branch_type    smallint

branch_type默认设置为 1。尽管它可能包含 1 到 4 之间的任何值。

用户项目

proj_id          bigint     Primary Key
proj_name        text
proj_branch_id   smallint

proj_branch_id从分支表中具有相应的 id(branch_id)。

user_approval

appr_id           bigint     Primary Key
appr_prjt_id      bigint
appr_status       smallint
appr_approval_by  bigint

appr_approval_by具有来自用户表的相应 id(user_id)
appr_status可能包含不同的状态值,例如 10,20,30... 对于单个appr_prjt_id

用户组

usergrp_id | usergrp_name
-------------------------
    1      | Admin
    2      | Manager

用户

user_id | user_name | user_usergrp_id |user_loc_id
---------------------------------------------------
    1   | John      |      1          |     1
    2   | Harry     |      2          |     1

分支

branch_id | branch_name | branch_type
-------------------------------------
    1     |  location1  |    2
    2     |  location2  |    1
    3     |  location3  |    4
    4     |  location4  |    2
    5     |  location4  |    2

用户项目

proj_id | proj_name | proj_branch_id
------------------------------------
    1   | test1      |       1
    2   | test2      |       2
    3   | test3      |       1
    4   | test4      |       3
    5   | test5      |       1
    6   | test5      |       4

user_approval

appr_id | appr_prjt_id | appr_status | appr_approval_by
-------------------------------------------------------
    1   |    1         |     10      |     1
    2   |    1         |     20      |     1
    3   |    1         |     30      |     1
    4   |    2         |     10      |     2
    5   |    3         |     10      |     1
    6   |    3         |     20      |     2
    7   |    4         |     10      |     1
    8   |    4         |     20      |     1

条件:输出必须取for each的MAX()值并计数。appr_statusappr_prjt_id

即,在上表中appr_prjt_id=1有 3 种不同的状态:10、20、30。它的计数必须只显示在输出中对应于 30 的状态(不在状态 10 和 20 中),对应于特定的用户组branch_name。同样对于该字段中的每个其他 idappr_prjt_id

SQL小提琴

期望的输出:

                           10   |    20  |  30

         ------> Admin     0    |    1   |   1
         |
location1
         |
         ------> Manager   1    |    1   |   0

我怎样才能做到这一点?

SQL小提琴

4

1 回答 1

2

SQL小提琴

select
    branch_name, usergrp_name,
    sum((appr_status = 10)::integer) "10",
    sum((appr_status = 20)::integer) "20",
    sum((appr_status = 30)::integer) "30"
from
    (
        select distinct on (appr_prjt_id)
            appr_prjt_id, appr_approval_by, appr_status
        from user_approval
        order by 1, 3 desc
    ) ua
    inner join
    users u on ua.appr_approval_by = u.user_id
    inner join
    user_group ug on u.user_usergrp_id = ug.usergrp_id
    inner join
    branch b on u.user_loc_id = b.branch_id
group by branch_name, usergrp_name
order by usergrp_name

适用于大多数 DBMS 的经典解决方案是使用case

select
    branch_name, usergrp_name,
    sum(case appr_status when 10 then 1 else 0 end) "10",

但是 Postgresql 具有布尔类型,并且它具有转换为整数 ( boolean::integer) 的结果,从而产生 0 或 1,从而减少了冗长的代码。

在这种情况下,也可以使用 a countin 而不是 a sum

select
    branch_name, usergrp_name,
    count(appr_status = 10 or null) "10",

我确实更喜欢,count但我的印象是它更难理解。诀窍是要知道count计算任何不为空的东西,并且 a (true或 null) 是true并且 a (false或 null) 为空,因此只要条件为真,它就会计算在内。

于 2013-03-28T08:19:29.177 回答