Subject | Re: [firebird-support] FB 2.5 Pivot/unpivot? |
---|---|
Author | Mark Rotteveel |
Post date | 2013-03-23T12:24:49Z |
On 23-3-2013 13:11, Mark Rotteveel wrote:
requirement that the combination id of columnName is unique, so the
following will work as well:
WITH pivot_step1 AS (
SELECT id,
(SELECT columnValue FROM keyvalue WHERE id = a.id AND columnName
= 'col1') AS col1,
(SELECT columnValue FROM keyvalue WHERE id = a.id AND columnName
= 'col2') AS col2
FROM (SELECT DISTINCT id FROM keyvalue) a
)
SELECT id, col1, col2
FROM pivot_step1
--
Mark Rotteveel
> On 22-3-2013 22:16, Ronan van Riet wrote:Is just realised that pivot_step2 isn't even necessary because of the
>> Hello.
>>
>> I was wondering whether FB 2.5 supports pivot/unpivot operators, like supported in Oracle?
>> If not is there perhaps a workaround?
>
> No, Firebird does not have pivot/unpivot, see
> http://tracker.firebirdsql.org/browse/CORE-1738
>
> A workaround for PIVOT would involve something like:
>
> WITH pivot_step1 AS (
> SELECT id,
> (SELECT columnValue FROM keyvalue WHERE id = a.id AND columnName
> = 'col1') AS col1,
> (SELECT columnValue FROM keyvalue WHERE id = a.id AND columnName
> = 'col2') AS col2
> FROM (SELECT DISTINCT id FROM keyvalue) a
> ),
> pivot_step2 AS (
> SELECT id, MAX(col1) AS col1, MAX(col2) AS col2
> FROM pivot_step1
> GROUP BY id
> )
> SELECT id, col1, col2
> FROM pivot_step2
requirement that the combination id of columnName is unique, so the
following will work as well:
WITH pivot_step1 AS (
SELECT id,
(SELECT columnValue FROM keyvalue WHERE id = a.id AND columnName
= 'col1') AS col1,
(SELECT columnValue FROM keyvalue WHERE id = a.id AND columnName
= 'col2') AS col2
FROM (SELECT DISTINCT id FROM keyvalue) a
)
SELECT id, col1, col2
FROM pivot_step1
--
Mark Rotteveel