sql >> Databasteknik >  >> RDS >> Oracle

Oracle Konvertera rader till kolumner

Om alla möjligheter att ta sig från rader till kolumner i Oracle kan du läsa här:

http://www.dba-oracle.com/t_converting_rows_columns.htm

Jag ser ingen enkel lösning för det ur databassynpunkt - skulle föreslå att formatera på applikationssidan, annars kan det se så tråkigt ut som detta:

SELECT
   to_char(w1.w_Date,'MM/DD/YYYY'), to_char(w2.w_Date,'MM/DD/YYYY'), 
   to_char(w3.w_Date,'MM/DD/YYYY'), to_char(w4.w_Date,'MM/DD/YYYY')
FROM 
 (select * from weather where w_date = trunc(sysdate)) w1,
 (select * from weather where w_date = trunc(sysdate) + 1) w2,
 (select * from weather where w_date = trunc(sysdate) + 2) w3,
 (select * from weather where w_date = trunc(sysdate) + 3) w4
UNION ALL
SELECT
   w1.image,  w2.image, w3.image , w4.image
FROM 
 (select * from weather where w_date = trunc(sysdate)) w1,
 (select * from weather where w_date = trunc(sysdate) + 1) w2,
 (select * from weather where w_date = trunc(sysdate) + 2) w3,
 (select * from weather where w_date = trunc(sysdate) + 3) w4
UNION ALL
SELECT
   w1.w_type,  w2.w_type, w3.w_type , w4.w_type
FROM 
 (select * from weather where w_date = trunc(sysdate)) w1,
 (select * from weather where w_date = trunc(sysdate) + 1) w2,
 (select * from weather where w_date = trunc(sysdate) + 2) w3,
 (select * from weather where w_date = trunc(sysdate) + 3) w4
UNION ALL
SELECT
   to_char(w1.high),  to_char(w2.high), to_char(w3.high) , to_char(w4.high)
FROM 
 (select * from weather where w_date = trunc(sysdate)) w1,
 (select * from weather where w_date = trunc(sysdate) + 1) w2,
 (select * from weather where w_date = trunc(sysdate) + 2) w3,
 (select * from weather where w_date = trunc(sysdate) + 3) w4
UNION ALL
SELECT
   to_char(w1.low),  to_char(w2.low), to_char(w3.low) , to_char(w4.low)
FROM 
 (select * from weather where w_date = trunc(sysdate)) w1,
 (select * from weather where w_date = trunc(sysdate) + 1) w2,
 (select * from weather where w_date = trunc(sysdate) + 2) w3,
 (select * from weather where w_date = trunc(sysdate) + 3) w4;
/



  1. Hur oracle rowid genereras internt?

  2. Använda INNER JOIN för att kombinera SQL Server-tabeller och visa dem i ASP.NET-webbformulär

  3. Python MySQL-anslutningen returnerar bytearray istället för vanligt strängvärde

  4. uppdatera värden från två tabeller med samma pcode och id