![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
All, I have a table analogous to this: SALES Column | Type | Modifiers ----------------------+-----------------------------+----------- salespersonid bigint not null dateofsale timestamp not null salesandcommish bigint[] not null For each salespersonid, I want an array of his/her sales (array element one); e.g. salespersonid monthlysales 100 | {123,45,8545,3446,3356,..... etc.} that monthlysales would contain data ordered by dateofsale.... Any ideas would be most appreciated. |
#3
| |||
| |||
|
|
Chas. wrote on 21.03.2011 21:00: All, I have a table analogous to this: SALES * * * * *Column * * * *| * * * * * *Type * * * * * * | Modifiers ----------------------+-----------------------------+----------- salespersonid * * * * * bigint * * * * * * * * * not null dateofsale * * * * * * * *timestamp * * * * *not null salesandcommish * * bigint[] * * * * * * * *not null For each salespersonid, I want an array of his/her sales (array element one); e.g. salespersonid *monthlysales 100 * * * * * * * | {123,45,8545,3446,3356,..... etc.} that monthlysales would contain data ordered by dateofsale.... Any ideas would be most appreciated. select salespersonid, * * * * array_agg(month_value) from ( * *select salespersonid, * * * * * salesandcommish[1] as month_value * *from sales * *order by dateofsale asc ) t group by salespersonid; although I'm not 100% sure if the aggregate will really honor the order by With 9.0 this is a lot easier: SELECT salespersonid, array_agg(salesandcommish[1] order by dateofsale) FROM sales GROUP BY salespersonid; |
#4
| |||
| |||
|
|
On Mar 21, 1:35*pm, Hans Castorp <REWYRLXHE... (AT) spammotel (DOT) com> wrote: Chas. wrote on 21.03.2011 21:00: All, I have a table analogous to this: SALES * * * * *Column * * * *| * * * * * *Type * * * * * * | Modifiers ----------------------+-----------------------------+----------- salespersonid * * * * * bigint * * * * * * * * * not null dateofsale * * * * * * * *timestamp * * * * * not null salesandcommish * * bigint[] * * * * * * * *not null For each salespersonid, I want an array of his/her sales (array element one); e.g. salespersonid *monthlysales 100 * * * * * * * | {123,45,8545,3446,3356,..... etc.} that monthlysales would contain data ordered by dateofsale.... Any ideas would be most appreciated. select salespersonid, * * * * array_agg(month_value) from ( * *select salespersonid, * * * * * salesandcommish[1] as month_value * *from sales * *order by dateofsale asc ) t group by salespersonid; although I'm not 100% sure if the aggregate will really honor the orderby With 9.0 this is a lot easier: SELECT salespersonid, array_agg(salesandcommish[1] order by dateofsale) FROM sales GROUP BY salespersonid; Hans, thank you. unfortunately the db in question is greenplum, not true pg, so neither works :-( I shall keep hunting. REgards, Chas. |
#5
| |||
| |||
|
|
On Mar 21, 3:12 pm, "Chas."<chas... (AT) gmail (DOT) com> wrote: On Mar 21, 1:35 pm, Hans Castorp<REWYRLXHE... (AT) spammotel (DOT) com> wrote: Chas. wrote on 21.03.2011 21:00: All, I have a table analogous to this: SALES Column | Type | Modifiers ----------------------+-----------------------------+----------- salespersonid bigint not null dateofsale timestamp not null salesandcommish bigint[] not null For each salespersonid, I want an array of his/her sales (array element one); e.g. salespersonid monthlysales 100 | {123,45,8545,3446,3356,..... etc.} that monthlysales would contain data ordered by dateofsale.... Any ideas would be most appreciated. select salespersonid, array_agg(month_value) from ( select salespersonid, salesandcommish[1] as month_value from sales order by dateofsale asc ) t group by salespersonid; although I'm not 100% sure if the aggregate will really honor the order by With 9.0 this is a lot easier: SELECT salespersonid, array_agg(salesandcommish[1] order by dateofsale) FROM sales GROUP BY salespersonid; Hans, thank you. unfortunately the db in question is greenplum, not true pg, so neither works :-( I shall keep hunting. REgards, Chas. All, I found this little ditty which does the trick on greenplum: CREATE AGGREGATE array_agg(anyelement) ( SFUNC=array_append, STYPE=anyarray, INITCOND=’{}’ ); it's from http://abdulyadi.wordpress.com/2008/...ql-version-84/ |
![]() |
| Thread Tools | |
| Display Modes | |
| |