Home » SQL & PL/SQL » SQL & PL/SQL » query
query [message #644068] Tue, 27 October 2015 15:09 Go to next message
chatwidravi
Messages: 3
Registered: August 2013
Location: bangalore
Junior Member
CREATE TABLE TEST (ENAME VARCHAR2(4),DEPTNO NUMBER);

INSERT INTO TEST VALUES('A',10);
INSERT INTO TEST VALUES('B',20);
INSERT INTO TEST VALUES('C',30);


Test
-----------------
ename deptno
A 10
B 20
C 30
Output of table should be like this:
ename deptno
A 30
B 20
C 10
Re: query [message #644070 is a reply to message #644068] Tue, 27 October 2015 15:15 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
College homework question? You need to show what you have tried so far. I would use a subquery factoring clause (probably two of them) to construct views, which I would then join.
Re: query [message #644071 is a reply to message #644070] Tue, 27 October 2015 15:18 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:
OraFAQ Forum Guide
How to use {code} tags and make your code easier to read


Why should output be as stated?
Re: query [message #644072 is a reply to message #644070] Tue, 27 October 2015 15:38 Go to previous messageGo to next message
chatwidravi
Messages: 3
Registered: August 2013
Location: bangalore
Junior Member
Hi,
I tried using inner join but it shows error as shown below.

SQL> WITH a AS (SELECT ename, RANK () OVER (ORDER BY ename) AS rn FROM test)
2 SELECT deptno, RANK () OVER (ORDER BY deptno DESC) AS rn
3 FROM test INNER JOIN a ON a.rn = test.rn;
FROM test INNER JOIN a ON a.rn = test.rn
*
ERROR at line 3:
ORA-00904: "TEST"."RN": invalid identifier
Re: query [message #644073 is a reply to message #644072] Tue, 27 October 2015 15:41 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
I would use simpler subqueries. For example,
orclz> select deptno,rownum join_col from (select deptno from test order by deptno desc) ;

    DEPTNO   JOIN_COL
---------- ----------
        30          1
        20          2
        10          3

orclz> select ename,rownum join_col from (select ename from test order by ename) ;

ENAM   JOIN_COL
---- ----------
A             1
B             2
C             3

orclz>
but using analytic queries might get you higher marks.
Re: query [message #644074 is a reply to message #644073] Tue, 27 October 2015 15:49 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Like:
SQL> select t.*,
  2         row_number() over (order by ename) ename_rk,
  3         row_number() over (order by deptno desc) detp_rk
  4  from test t
  5  /
ENAM     DEPTNO   ENAME_RK    DETP_RK
---- ---------- ---------- ----------
C            30          3          1
B            20          2          2
A            10          1          3

3 rows selected.

Re: query [message #644213 is a reply to message #644068] Fri, 30 October 2015 11:13 Go to previous messageGo to next message
sandeep_orafaq
Messages: 88
Registered: September 2014
Member
this requirement is not clear, just giving the expected out put does not make sense, explain what is the output in words

ENAME DEPTNO
A 10
B 30
C 20
D 50
E 40

-------------------------------------
output will be

A 50
B 40
C 30
D 20
E 10

or

A 40
B 50
C 20
D 30
E 10

I hope its clear what i mean. (explain the rule which governs the output)

Re: query [message #644218 is a reply to message #644213] Fri, 30 October 2015 11:23 Go to previous message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

If I was you I'd not expect any answer to your question from OP which posts questions, gets answers but will come back only if he has another question.

[Updated on: Fri, 30 October 2015 11:24]

Report message to a moderator

Previous Topic: Stored Procedure in migrating data
Next Topic: case statement
Goto Forum:
  


Current Time: Fri May 10 07:08:58 CDT 2024