sql - Postgresql pivot? Crosstab? -
i have table (which result of query) in postgres has set of rows (the result of complicated summation of data) looks following: (the column names names of each day, , value of each column double precision.)
sun mon tues wed thurs fri
1.24 1.11 4.51 3.21 2.21 1.01
i need have data selected 1 row results below:
day amount
sun 1.24
mon 1.11
tues 4.51
wed 3.21
thurs 2.21
fri 1.01
i'm having difficulty getting started, need change column names values , pivot result. tried experimenting crosstab i'm not entirely sure need. advice or suggestions me going in right direction appreciated.
modifying @jack douglas's first answer:
select unnest(array['sun', 'mon', 'tue', 'wed', 'thu', 'fri']) day, unnest(array[sun, mon, tue, wed, thu, fri]) amount t;
a little less costly according 9.0 query planner:
seq scan on t (cost=0.00..11.62 rows=360 width=192)
versus
subquery scan on z (cost=0.00..12.16 rows=360 width=68) -> seq scan on t (cost=0.00..11.26 rows=360 width=192)
Comments
Post a Comment