AFAIK, to group by item
you have to order by item
:
Select * from crosstab($$Select item, year, value from sec_sample
Where cik=320193
AND period='Annual'
AND Sector='Agro'
Order by item, year, preorder
$$,
$$VALUES ('2011'::int), ('2012')$$)
AS value("item" varchar(255), "2011" numeric(20,2), "2012" numeric(20,2));
But you can pivot without using crosstab
, like this:
select
s.item,
sum(case when s.year = 2011 then s.value else 0 end) as "2011",
sum(case when s.year = 2012 then s.value else 0 end) as "2012"
from sec_sample as s
group by s.item
order by min(s.preorder)
I think it's easier to modify this type of query