1

I have a query wherein I have week, avg_pts, avg_fgm, avg_fga, pct, avg_fg3m as columns. Is there a way wherein I could make weeks 1 to 7 into a column? And the current columns into rows?

enter image description here

Expected Output:
Stats   Week1   Week2   Week3   Week4   Week5   Week6   Week7
Avg_pts 113.00    ...  ...  ..  ..
avg_fgm 42.00    ....   ...   ...
avg_fga 88.00    ....   ...    ...
pct     0.51     ..   ....   ....
avg_fg3m 11.00   ....   ...    ....
Albina
  • 1,901
  • 3
  • 7
  • 19
  • this link will help you https://stackoverflow.com/questions/13944417/mysql-convert-column-to-row-pivot-table#:~:text=What%20you%20need%20to%20do,a%20CASE%20for%20the%20pivot.&text=See%20SQL%20Fiddle%20with%20Demo.&text=Save%20this%20answer.,-Show%20activity%20on – abhishek kumar Dec 09 '22 at 09:17
  • Your best option: **Do not do it in SQL**. The term for what you want is *pivot* and while it can done in SQL it is always a messy process and (imho) best performed by your apps presentation manager. The main problem being the query needs to know the exact rows and columns and any change requires rewriting the query. Look at it this way: SQL is a data server and your app is, at least partially, a data formater. Let SQL serve the data, which it is very good at. Let your app do the formatting, which SQL is very poor at. – Belayer Dec 09 '22 at 19:16
  • I think you can create a **crosstab query**. More info is here: https://stackoverflow.com/questions/3002499/postgresql-crosstab-query – Albina Dec 11 '22 at 10:08

0 Answers0