简体   繁体   English

如何基于多行多列动态创建postgresql数据透视表

[英]How to dynamically create a postgresql pivot table based on multiple rows and columns

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.我有一个问题,我想在 postgresql 上解决我有多行的问题,我想使用数据透视表将它们加入一行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM