count() corresponding to max() of different values satisfying some condition

不问归期 提交于 2020-01-02 05:13:52

问题


I have the following tables:

user_group

usergrp_id      bigint     Primary Key
usergrp_name    text  

user

user_id             bigint     Primary Key
user_name           text
user_usergrp_id     bigint
user_loc_id         bigint

user_usergrp_id has its corresponding id from the user_group table user_loc_id has its corresponding id(branch_id) from the branch table.

branch

branch_id      bigint     Primary Key
branch_name    text
branch_type    smallint

branch_type By default is set as 1. Although it may contain any value in between 1 and 4.

user_projects

proj_id          bigint     Primary Key
proj_name        text
proj_branch_id   smallint

proj_branch_id has its corresponding id(branch_id) from the branch table.

user_approval

appr_id           bigint     Primary Key
appr_prjt_id      bigint
appr_status       smallint
appr_approval_by  bigint

appr_approval_by has its corresponding id(user_id) from the user table
appr_status may contain different status values like 10,20,30... for a single appr_prjt_id

user_group

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

user

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

branch

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

user_projects

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

Condition: The output must take the MAX() value of appr_status for each appr_prjt_id and count it.

I.e., in the above table appr_prjt_id=1 has 3 different status: 10, 20, 30. Its count must only be shown for status corresponding to 30 in the output (not in the statuses 10 and 20), corresponding to a user group in a particular branch_name. Similarly for each of the other id's in the field appr_prjt_id

SQL Fiddle

Desired Output:

                           10   |    20  |  30

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

How can I do that?

SQL Fiddle


回答1:


SQL Fiddle

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

The classic solution, that works in most DBMSs is to use a case:

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

But Postgresql has the boolean type and it has a cast to integer (boolean::integer) resulting in 0 or 1 which makes for less verbose code.

In this case it is also possible to do a count in instead of a sum:

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

I indeed prefer the count but I have the impression that it is harder to understand. The trick is to know that count counts anything not null and that a (true or null) is true and a (false or null) is null so it will count whenever the condition is true.



来源:https://stackoverflow.com/questions/15676574/count-corresponding-to-max-of-different-values-satisfying-some-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!