Home » SQL & PL/SQL » SQL & PL/SQL » Oracle transpose rows into columns (Oracle 11g)
Oracle transpose rows into columns [message #610812] |
Mon, 24 March 2014 10:56 |
|
developer12
Messages: 88 Registered: July 2013
|
Member |
|
|
Hi,
I have a rates table with the table structure and insert scripts as:
CREATE TABLE RATES
(
to_currency NUMBER NOT NULL,
src NUMBER NOT NULL,
e_date DATE NOT NULL,
from_currency NUMBER NOT NULL,
rate NUMBER
);
SET DEFINE OFF;
Insert into RATES
(TO_CURRENCY, SRC, E_DATE, FROM_CURRENCY, RATE)
Values
(61, 146, TO_DATE('09/03/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 1, 0.473645525059);
Insert into RATES
(TO_CURRENCY, SRC, E_DATE, FROM_CURRENCY, RATE)
Values
(61, 146, TO_DATE('09/04/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 1, 0.473608881496);
Insert into RATES
(TO_CURRENCY, SRC, E_DATE, FROM_CURRENCY, RATE)
Values
(61, 146, TO_DATE('09/05/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 1, 0.473596668235);
Insert into RATES
(TO_CURRENCY, SRC, E_DATE, FROM_CURRENCY, RATE)
Values
(61, 146, TO_DATE('09/06/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 1, 0.473566137839);
COMMIT;
I want the results as in attached Rates.xls.
Can anyone help how I can form a query to achieve that?
-
Attachment: Rates.csv
(Size: 0.63KB, Downloaded 970 times)
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Oracle transpose rows into columns [message #610935 is a reply to message #610883] |
Wed, 26 March 2014 03:09 |
|
rayudu.trinadh@gmail.com
Messages: 2 Registered: March 2014
|
Junior Member |
|
|
Last time i had attended one interview,he simply questioned me the same.
How you will arrange rows of table data in to columns?
I had replied that using PIVOT we can do that, he asked me the syntax for the same.But it works only in 11g.
Is there any alternative way to approach the same.
for example check this:
SELECT * FROM (SELECT year(invoiceDate) as [year], left(datename(month,invoicedate),3)as [month], _
InvoiceAmount as Amount FROM Invoice) as InvoiceResult
year month amount
2011 May 100
2011 Jun 200
2011 Aug 30
2011 Sep 300
SELECT *
FROM (
SELECT
year(invoiceDate) as [year],left(datename(month,invoicedate),3)as [month],
InvoiceAmount as Amount
FROM Invoice
) as s
PIVOT
(
SUM(Amount)
FOR [month] IN ( May,Jun, Aug,Sep)
)AS pivot
result:
year May Jun Aug Sep
2011 100 200 30 300
|
|
|
|
Goto Forum:
Current Time: Tue Apr 23 10:59:01 CDT 2024
|