I'm converting a huge keystore table to columns, with the following query. However, I need to remove the sparse columns and have only one row per id
. How?
I can't use coalesce
, Postgres 8.4. I can't install any packages.
Query:
(select id
, case when udfname = 'ptid' then udfvalue end as "ptid"
, case when udfname = 'col1' then udfvalue end as "col1"
, case when udfname = 'col2' then udfvalue end as "col2"
, case when udfname = 'col3' then udfvalue end as "col3"
, case when udfname = 'col4' then udfvalue end as "col4"
, case when udfname = 'xref_col5' then udfvalue end as "xref_col5"
from uglykeystoretable where sampleid = 656556) -- restricting to one id so it won't clog.
;
This substring(array_to_string())
hack makes it work, however there must be a cleaner solution. Let me know.
select
substring ( array_to_string(array_agg(sampleid), ',') , 1, strpos( array_to_string(array_agg(sampleid), ','), ',')-1 ) as sampleid
,array_to_string(array_agg(pid), ',') as pid
,array_to_string(array_agg(dob), ',') as dob
,array_to_string(array_agg(seq_res), ', ') as seq_res
,array_to_string(array_agg(sx), ',') as sx
,array_to_string(array_agg(wrkspc), ',') as wrkspc
,array_to_string(array_agg(xref_isq_sid), ',') as xref_isq_sid
,array_to_string(array_agg(dt_coll), ',') as dt_coll
,array_to_string(array_agg(lcus), ',') as lcus
,array_to_string(array_agg(spcs), ',') as spcs
,nullif(array_to_string(array_agg(fname), ''),'') as fname
,nullif(array_to_string(array_agg(lname), ''),'') as lname
from
(select sampleid
, (case when udfname = 'pid' then udfvalue end) as "pid"
, (case when udfname = 'dob' then udfvalue end) as "dob"
, case when udfname = 'Sequencing Resolution' then udfvalue end as "seq_res"
, case when udfname = 'sx' then udfvalue end as "sx"
, case when udfname = 'wrkspc' then udfvalue end as "wrkspc"
, case when udfname = 'xref_iseq_sid' then udfvalue end as "xref_isq_sid"
, case when udfname = 'dt_coll' then udfvalue end as "dt_coll"
, case when udfname = 'lcus' then udfvalue end as "lcus"
, case when udfname = 'spcs' then udfvalue end as "spcs"
, case when udfname = 'lname' then udfvalue end as "lname"
, case when udfname = 'fname' then udfvalue end as "fname"
from ugly_keystore_table
--where sid >( SELECT MAX(sid)-1000 FROM MyEntity)
) as sample_details
group by sid
;