LucDaher
LucDaher

Reputation: 157

Oracle - Transpose Row into Column

I would like to transpose the following rows outcome into columns. I was considering PIVOT but I don't believe I can achieve the desired result.

As is:

ID_NCM | DESTINATION | START      | END  
36393  | PE          | 01-01-2018 | 10-31-2018
36393  | PE          | 11-01-2018 | 12-31-9999

To be:

ID_NCM | DESTINATION | CURRENT_START | CURRENT_END | FUTURE_START | FUTURE_END
36393  | PE          | 01-01-2018    | 10-31-2018  | 11-01-2018   | 12-31-9999

Am I missing some table structure concept here whereby the desired layout is impossible to achieve with select statement?

Thanks.

Upvotes: 1

Views: 87

Answers (2)

Barbaros Özhan
Barbaros Özhan

Reputation: 65208

You may directly use grouping by ID_NCM,DESTINATION with min and max functions as :

with t(ID_NCM,DESTINATION,"START","END") as
(  
 select 36393,'PE',date'2018-01-01',date'2018-10-31' from dual union all
 select 36393,'PE',date'2018-01-11',date'9999-12-31' from dual 
)
select ID_NCM,DESTINATION,
       min("START") as CURRENT_START,min("END") as CURRENT_END,
       max("START") as FUTURE_START,max("END") as FUTURE_END
  from t
 group by ID_NCM,DESTINATION

ID_NCM  DESTINATION CURRENT_START  CURRENT_END   FUTURE_START  FUTURE_END
------  ----------- -------------  ------------  ------------  -----------
36393       PE       01.01.2018     31.10.2018    11.01.2018   31.12.9999

P.S. START and END are reserved keywords in Oracle, so wrapped them up with double-quotes.

Edit : Due to your last comment, you may make a change by adding a correlated subquery as in the following style :

with t(ID_NCM,DESTINATION,"START","END",tax_rate) as
(  
 select 36393,'PE',date'2018-01-01',date'2018-10-31',0.06 from dual union all
 select 36393,'PE',date'2018-01-11',date'9999-12-31',0.04 from dual 
)
select ID_NCM,DESTINATION,
       min("START") as CURRENT_START,min("END") as CURRENT_END,
       max("START") as FUTURE_START,max("END") as FUTURE_END,
       (select tax_rate from t where "START"=date'2018-01-01' and "END"=date'2018-10-31') 
                                                                         as current_rate,
       (select tax_rate from t where "START"=date'2018-01-11' and "END"=date'9999-12-31') 
                                                                         as future_rate
  from t
 group by ID_NCM,DESTINATION

ID_NCM  DEST   CURRENT_START  CURRENT_END  FUTURE_START  FUTURE_END CURRENT_RATE FUTURE_RATE
------  ----- -------------  ------------  ------------  ----------- ----------- -----------
36393   PE     01.01.2018     31.10.2018    11.01.2018   31.12.9999     0,06          0,04

Edited Rextester Demo

Upvotes: 1

Jaydip Rakholiya
Jaydip Rakholiya

Reputation: 802

You can try something like this -

select id_ncm,
       destination,
       max(decode(myrank, 1, start_date)) current_start,
       max(decode(myrank, 1, end_date)) current_end,
       max(decode(myrank, 2, start_date)) future_start,
       max(decode(myrank, 2, end_date)) future_end
  from (select id_ncm,
               destination,
               start_date,
               end_date,
               rank() over(partition by id_ncm order by start_date, end_date) myrank
          from your_table) v1
 group by id_ncm, destination

Upvotes: 1

Related Questions