簡體   English   中英

如何從PostgresSQL中的一行中獲取列數據?

[英]How to get column data from a row in postgresSQL?

我有數據庫表名稱工資
| Employee | | Period | | Attributes | | Amount |
| Zakir | | Oct'18 | | Basic | | 20000 |
| Zakir | | Oct'18 | | H.Rent | | 10000 |
| Zakir | | Oct'18 | | Convene | | 2000 |
| Zakir | | Oct'18 | | Medical | | 2000 |
| Tame | | Oct'18 | | Basic | | 30000 |
| Tame | | Oct'18 | | H.Rent | | 15000 |
| Tame | | Oct'18 | | Convene | | 2500 |
| Tame | | Oct'18 | | Medical | | 2500 |

我想要

| Employee | | Period | | Basic | | H.Rent| |Convene| |Medical| |Gross|
| Zakir | | Oct'18 | | 20000 | | 10000 | | 2000 | | 2000 | |34000|
| Tame | | Oct'18 | | 30000 | | 15000 | | 2500 | | 2500 | |50000|

用例何時

select employee ,period,
sum(case when attribute='basic' then amount else 0 end) basic,
sum(case when attribute='H.Rent' then amount else 0 end H_Rent,
sum(case when attribute='Convene' then amount else 0 end) Convene,
sum(case when attribute='Medical' then amount else 0 end) Medical,
sum(case when attribute='basic' then amount else 0 end)+
sum(case when attribute='H.Rent' then amount else 0 end)+
sum(case when attribute='Convene' then amount else 0 end)+
sum(case when attribute='Medical' then amount else 0 end) as gross
from payroll group by employee ,period

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM