vertica - sql getting pivoted top 5 -
i have sales table following columns:
[date], [storeid], [itemid], [amountindollars]. example: 06052012 | 1 | 111 | 2.35 06052012 | 1 | 222 | 5.00 06052012 | 2 | 333 | 9.99 ... 01052012 | 30 | 999 | 3.98
i want able select data in way:
[date] | [storeid] | totalsalesfortheday | topsellingitemid1 | topsellingitemid2 | topsellingitemid3
i trying following:
select date, storeid, sum(amountindollars), itemid sales group date, storeid, itemid
this gives itemids sold in day... how can 1. limit 3 top items 2. present 3 top items in same lines horizontally
thanks!
as requested - data sample. original table
date | storeid | itemid | amountindollars 06052012 | 1 | 111 | 2.35 06052012 | 1 | 222 | 5.00 06052012 | 1 | 444 | 9.99 06052012 | 1 | 222 | 5.00 06052012 | 1 | 444 | 9.99 06052012 | 1 | 333 | 9.99 06052012 | 1 | 444 | 9.99 06052012 | 2 | 222 | 5.00 06052012 | 2 | 444 | 9.99 06052012 | 2 | 333 | 9.99 06052012 | 2 | 444 | 9.99 05052012 | 1 | 111 | 2.35 05052012 | 1 | 222 | 5.00 05052012 | 1 | 444 | 9.99 05052012 | 1 | 222 | 5.00 05052012 | 1 | 444 | 9.99 05052012 | 1 | 333 | 9.99 05052012 | 1 | 444 | 9.99 05052012 | 2 | 222 | 5.00 05052012 | 2 | 444 | 9.99 05052012 | 2 | 333 | 9.99 05052012 | 2 | 444 | 9.99
will result in:
date | storeid |totalsales | topsellingitemid1 | topsellingitemid2 | topsellingitemid3 06052012 | 1 | 52.31 | 444 | 222 | 111 06052012 | 2 | 34.97 | 444 | 222 | 333 05052012 | 1 | 52.31 | 444 | 222 | 111 05052012 | 2 | 34.97 | 444 | 222 | 111
thanks...
i don't believe vertica supports pivot.
here's answer similar question: how do crosstab in vertica
Comments
Post a Comment