如何获得价值一行在PostgreSQL?(How to get value in one row i

2019-10-16 12:22发布

我有postgreSQL9.3数据库表名emp_leave像

|emp_name|department|ann_leave|med_leave|cas_leave|org_ann_lv|org_med_lv|org_cas_lv|
| Tame   |    IT    |         |         |   3     |          |          |   25     |
| Tame   |    IT    |     4   |         |         |      20  |          |          |
| Tame   |    IT    |         |      3  |         |          |      30  |          |


 I want the query result like 
|emp_name|department|ann_leave|med_leave|cas_leave|org_ann_lv|org_med_lv|org_cas_lv|
| Tame   |    IT    |    4    |    3    |   3     |    20    |    30    |   25     |

Answer 1:

你要聚集:

select el.emp_name, el.department, 
       max(el.ann_leave),
       . . . ,
       max(el.org_cas_lv)
from emp_leave el
group by el.emp_name, el.department;

这是假设空白为null



Answer 2:

为了消除空值,并转换成单排

select
emp_name,
department,
min(ann_leave),
min(med_leave),
min(cas_leave),
min(org_ann_lv),
min(org_med_lv),
min(org_cas_lv)
from emp_leave
group by
emp_name,
department


文章来源: How to get value in one row in postgreSQL?