sql >> Database teknologi >  >> RDS >> Oracle

Oracle SQL pivotforespørgsel

Oracle 11g og derover

Fra og med Oracle 11g kan du nu bruge PIVOT operatør for at opnå dette resultat:

create table tq84_pivot (
  month number,
  value number
);

insert into tq84_pivot values(1, 100);
insert into tq84_pivot values(2, 200);
insert into tq84_pivot values(3, 300);
insert into tq84_pivot values(4, 400);
insert into tq84_pivot values(5, 500);
insert into tq84_pivot values(6, 600);
--
insert into tq84_pivot values(1, 400);
insert into tq84_pivot values(2, 350);
insert into tq84_pivot values(4, 150);

select 
  *
from
  tq84_pivot
pivot (
   sum (value) as sum_value for
     (month) in (1 as month_jan,
                 2 as month_feb,
                 3 as month_mar,
                 4 as month_apr,
                 5 as month_mai,
                 6 as month_jun,
                 7 as month_jul,
                 8 as month_aug,
                 9 as month_sep,
                10 as month_oct,
                11 as month_nov,
                12 as month_dec)
);


  1. Nye funktioner i SQL Server 2017 (Database Engine)

  2. MySQL ATAN() Funktion – Returner buetangensen for en værdi (eller værdier)

  3. En oversigt over JSON-kapaciteter i PostgreSQL

  4. Hvordan tester man en Oracle Stored Procedure med RefCursor returtype?