Please help. I have a problem that I want to solve on postgresql where I have multiple rows and I want to join them into one row using pivot table.
Here is an examble of the table I want to pivot
------------------------------------------------------------------
|uId|fnme | mnme | lnme| location | rate | hours| date |
------------------------------------------------------------------
| 1 | An | Pee | Mre | Group Estate| 10.05 | 6 |2022-04-25 |
| 1 | An | Pee | Mre | Group Estate| 10.05 | 12 |2022-04-27 |
| 2 | Ali | null| Art | Hellensville| 14.00 | 8 |2022-04-29 |
| 2 | Ali | null| Art | Elin Holding| 14.00 | 5 |2022-04-16 |
| 2 | Ali | null| Art |Hellensville | 14.00 | 8 |2022-04-09 |
| 1 | An | Pee | Mre | Eaglesview | 10.05 | 8 |2022-04-09 |
| 1 | An | Pee | Mre | Eaglesview | 10.05 | 7.5 |2022-04-08 |
------------------------------------------------------------------
Below is the desired result:
------------------------------------------------------------------------------------------------------------------------------------------------
|uId|fnme | mnme | lnme| location1 | rate1 | hours1| location2 | rate2 | hours2|location3 | rate3 | hours3| location4 | rate4 | hours4|
------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | An | Pee | Mre | Group Estate | 10.05 | 6 | Group Estate| 10.05 | 12 | Eaglesview | 10.05 | 8 | Eaglesview | 10.05 | 7.5 |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 2 | Ali | null | Art | Hellensville| 14.00 | 8 | Elin Holding| 14.00 | 5 | Hellensville| 14.00 | 8 | null | null | null |
-----------------------------------------------------------------------------------------------------------------------------------------------
Note that the number of rows will determine number of columns that the output pivot table will be like. Meaning the columns will be dynamically created
After a lot of researching I found something closer to my question so I made a work around
[Dynamic Pivot Needed with Row_Number()][1]
(
select uId as ci,location,rate,hours,firstname as fn, middlename as mn, lastname as ln,
row_number() over(partition by id order by "createdAt") as rn
from timesheetview where "paymentStatus" = false
)
select distinct ci as uId,fn as firstname,mn as middlename, ln as lastname,
(select ct.location from cte ct where ct.ci=cte.ci and ct.rn=1) location1,
(select CAST (ct.rate AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=1) payment_rate1,
(select CAST (ct.hours AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=1) payment_normal_hours1,
(select ct.location from cte ct where ct.ci=cte.ci and ct.rn=2) location2,
(select CAST (ct.rate AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=2) payment_rate2,
(select CAST (ct.hours AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=2) payment_normal_hours2,
(select ct.location from cte ct where ct.ci=cte.ci and ct.rn=3) location3,
(select CAST (ct.rate AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=3) payment_rate3,
(select CAST (ct.hours AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=3) payment_normal_hours3,
(select ct.location from cte ct where ct.ci=cte.ci and ct.rn=4) location4,
(select CAST (ct.rate AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=4) payment_rate4,
(select CAST (ct.hours AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=4) payment_normal_hours4,
(select ct.location from cte ct where ct.ci=cte.ci and ct.rn=5) location5,
(select CAST (ct.rate AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=5) payment_rate5,
(select CAST (ct.hours AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=5) payment_normal_hours5,
(select ct.location from cte ct where ct.ci=cte.ci and ct.rn=6) location6,
(select CAST (ct.rate AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=6) payment_rate6,
(select CAST (ct.hours AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=6) payment_normal_hours6,
(select ct.location from cte ct where ct.ci=cte.ci and ct.rn=7) location7,
(select CAST (ct.rate AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=7) payment_rate7,
(select CAST (ct.hours AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=7) payment_normal_hours7,
(select ct.location from cte ct where ct.ci=cte.ci and ct.rn=8) location8,
(select CAST (ct.rate AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=8) payment_rate8,
(select CAST (ct.hours AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=8) payment_normal_hours8,
(select ct.location from cte ct where ct.ci=cte.ci and ct.rn=9) location9,
(select CAST (ct.rate AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=9) payment_rate9,
(select CAST (ct.hours AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=9) payment_normal_hours9,
(select ct.location from cte ct where ct.ci=cte.ci and ct.rn=10) location10,
(select CAST (ct.rate AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=10) payment_rate10,
(select CAST (ct.hours AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=10) payment_normal_hours10,
(select ct.location from cte ct where ct.ci=cte.ci and ct.rn=11) location11,
(select CAST (ct.rate AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=11) payment_rate11,
(select CAST (ct.hours AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=11) payment_normal_hours11,
(select ct.location from cte ct where ct.ci=cte.ci and ct.rn=12) location12,
(select CAST (ct.rate AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=12) payment_rate12,
(select CAST (ct.hours AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=12) payment_normal_hours12,
(select ct.location from cte ct where ct.ci=cte.ci and ct.rn=13) location13,
(select CAST (ct.rate AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=13) payment_rate13,
(select CAST (ct.hours AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=13) payment_normal_hours13,
(select ct.location from cte ct where ct.ci=cte.ci and ct.rn=14) location14,
(select CAST (ct.rate AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=14) payment_rate14,
(select CAST (ct.hours AS DOUBLE PRECISION) from cte ct where ct.ci=cte.ci and ct.rn=14) payment_normal_hours14
from cte```
I wanted something maybe cleaner but this solves my problem. I would have prefered where columns are dynamically generated especially when more data is involved
[1]: https://stackoverflow.com/questions/29569801/dynamic-pivot-needed-with-row-number
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.