简体   繁体   中英

Oracle SQL result in multiple columns

I have a SQL query of a single column that places the output in multiple rows. For instance:

Select distinct academic_year from AFF_EVENT_V
Order by academic_year;

This results in the following:

2002-2003
2003-2004
2004-2005
2005-2006
2006-2007
2007-2008
2008-2009
2009-2010
2010-2011
2011-2012
2012-2013
2013-2014
2014-2015
2015-2016
2016-2017
2017-2018

I'd like the results to appear in multiple (let's say 5) "columns". I'm looking for results something like this:

2002-2003     2003-2004     2004-2005     2005-2006     2006-2007
2007-2008     2008-2009     2009-2010     2010-2011     2011-2012
etc.

How can I structure a Query to result in this output?

One method is conditional aggregation:

select max(case when mod(seqnum, 5) = 0 then academic_year end),
       max(case when mod(seqnum, 5) = 1 then academic_year end),
       max(case when mod(seqnum, 5) = 2 then academic_year end),
       max(case when mod(seqnum, 5) = 3 then academic_year end),
       max(case when mod(seqnum, 5) = 4 then academic_year end)
from (select academic_year, row_number() over (order by academic_year) - 1 as seqnum
      from AFF_EVENT_V
      group by academic_year
     ) a
group by floor(seqnum / 5);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM