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