How to get value in one row in postgreSQL?

寵の児 提交于 2019-12-24 07:05:07

问题


I have database table name emp_leave in postgreSQL9.3 like

|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     |

回答1:


You want aggregation :

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;

This assumes blank space as null.




回答2:


To eliminate null value and convert into single row

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


来源:https://stackoverflow.com/questions/53539959/how-to-get-value-in-one-row-in-postgresql

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