Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Kueri Oracle Pivot memberikan kolom dengan tanda kutip di sekitar nama kolom. Apa?

Anda dapat memberikan alias ke kolom baru di pivot pernyataan IN klausa.(NB:Ini berbeda dari standar di mana klausa IN() yang tidak mengizinkan alias.)

with testdata as
(
    select 'Fred' First_Name, 10 Items from dual
    union
    select 'John' First_Name, 5  Items from dual
    union 
    select 'Jane' First_Name, 12 Items from dual
    union
    select 'Fred' First_Name, 15 Items from dual
)
select * from testdata
pivot (
      sum(Items) 
      for First_Name
      in ('Fred' as fred,'John' as john,'Jane' as jane)
      )

dan juga untuk klausa agregat Anda yang diperlukan jika Anda memiliki banyak klausa..

with testdata as
(
    select 'Fred' First_Name, 10 Items from dual
    union
    select 'John' First_Name, 5  Items from dual
    union 
    select 'Jane' First_Name, 12 Items from dual
    union
    select 'Fred' First_Name, 15 Items from dual
)
select * from testdata
pivot (
    sum(Items) itmsum,
    count(Items) itmcnt
    for First_Name
    in ('Fred' as fred,'John' as john,'Jane' as jane)
   )

kembali

FRED_ITMSUM FRED_ITMCNT JOHN_ITMSUM JOHN_ITMCNT JANE_ITMSUM JANE_ITMCNT
----------- ----------- ----------- ----------- ----------- -----------
         25           2           5           1          12           1

Tentu saja Anda kemudian dapat beralih ke lingkaran penuh dan menggunakan aliasing oracle standar dan mengganti namanya menjadi apa pun yang Anda suka termasuk memasukkan kembali tanda kutip..

with testdata as
(
    select 'Fred' First_Name, 10 Items from dual
    union
    select 'John' First_Name, 5  Items from dual
    union 
    select 'Jane' First_Name, 12 Items from dual
    union
    select 'Fred' First_Name, 15 Items from dual
)
select FRED_ITMSUM "Fred's Sum", FRED_ITMCNT "Fred's Count"
     , JOHN_ITMSUM "John's Sum", JOHN_ITMCNT "John's Count"
     , JANE_ITMSUM "Janes's Sum", JANE_ITMCNT "Janes's Count"
from testdata
pivot (
    sum(Items) itmsum,
    count(Items) itmcnt
    for First_Name
    in ('Fred' as fred,'John' as john,'Jane' as jane)
   )

memberikan

Fred's Sum Fred's Count John's Sum John's Count Janes's Sum Janes's Count
---------- ------------ ---------- ------------ ----------- -------------
        25            2          5            1          12             1



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Metode Pengaturan Parameter NLS dan Prioritasnya (Oracle Database)

  2. Daftar Semua Fungsi di Oracle Database

  3. Kembalikan nomor dari pernyataan Oracle Select setelah tanggal parsing

  4. Menentukan apakah Field Data di Oracle bertipe Number

  5. Apa itu Oracle SQL &PL/SQL? Semua Yang Perlu Diketahui Pemula