sql swap [message #228440] |
Tue, 03 April 2007 01:39 |
pavuluri
Messages: 247 Registered: January 2007
|
Senior Member |
|
|
how can swap 4 and 7 row in a table ?
Thanks,
Srinivas
|
|
|
|
Re: sql swap [message #228451 is a reply to message #228450] |
Tue, 03 April 2007 02:07 |
pavuluri
Messages: 247 Registered: January 2007
|
Senior Member |
|
|
asume that
i have emp table.
i want 7th row displayed as 5th row
and 5th row displayed as 7th row
thanks
srinivas
|
|
|
Re: sql swap [message #228453 is a reply to message #228451] |
Tue, 03 April 2007 02:08 |
|
Michel Cadot
Messages: 68718 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Once again there is no order, so there is no 5th or 7th row unless you yourself order your rows.
So use "order by".
Regards
Michel
(add first sentence)
[Updated on: Tue, 03 April 2007 02:09] Report message to a moderator
|
|
|
Re: sql swap [message #228463 is a reply to message #228453] |
Tue, 03 April 2007 02:37 |
pavuluri
Messages: 247 Registered: January 2007
|
Senior Member |
|
|
thanks
working this query
select * from emp order by case when rownum=5 then 7
when rownum=7 then 5
else rownum end
thanks
srinivas
|
|
|
|
Re: sql swap [message #228468 is a reply to message #228465] |
Tue, 03 April 2007 02:50 |
Frank
Messages: 7901 Registered: March 2000
|
Senior Member |
|
|
We have been here time and time again.
Rows are returned in a more or less random order that is not predictable (at least not by 99.999% of us mortals) if you do not use an order by.
So, there is no point in swapping rows 4 and 7, because your results may change if you run the same query tomorrow.
|
|
|
|
|
|
Re: sql swap [message #228506 is a reply to message #228502] |
Tue, 03 April 2007 04:30 |
|
Maaher
Messages: 7065 Registered: December 2001
|
Senior Member |
|
|
The same principle applies: You sort, for the second query (reverse order) you reverse the sort you used for the first query. That way, your 'first' record. will become your 'last' record.
MHE
[Updated on: Tue, 03 April 2007 04:31] Report message to a moderator
|
|
|
|
|
|
Re: sql swap [message #228532 is a reply to message #228520] |
Tue, 03 April 2007 05:24 |
pavuluri
Messages: 247 Registered: January 2007
|
Senior Member |
|
|
Hi guys
see this
select e.*,rownum from emp e order by
case when rownum=(select max(rownum) from emp) then 1
when rownum=1 then (select max(rownum) from emp)
else rownum end
Thanks,
srinivas
|
|
|
Re: sql swap [message #228533 is a reply to message #228532] |
Tue, 03 April 2007 05:29 |
|
Michel Cadot
Messages: 68718 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
And you get an undeterministic result.
There is no difference with something like (without order):
with cnt as (select count(*) cnt from emp)
select e.*, decode(rownum,1,cnt,cnt,1,rownum) "ROWNUM"
from emp e, cnt
/
Regards
Michel
|
|
|
Re: sql swap [message #228544 is a reply to message #228532] |
Tue, 03 April 2007 06:00 |
Frank
Messages: 7901 Registered: March 2000
|
Senior Member |
|
|
Ok, Step one: You DO believe us when we say that Oracle spits out rows in random order if you don't add an explicit order by on one of your columns?
Step two: What is the use of switching two rows if these rows are already in random order?
To accomplish your result (alternative to Michel's way):
SQL> select ename
2 , empno
3 , case when rownum = 1 then no_of_records
4 when rownum = no_of_records then 1
5 else rownum
6 end my_rownum
7 from (select ename
8 , empno
9 , count(*) over () no_of_records
10 from emp
11 );
ENAME EMPNO MY_ROWNUM
---------- ---------- ----------
SMITH 7369 14
ALLEN 7499 2
WARD 7521 3
JONES 7566 4
MARTIN 7654 5
BLAKE 7698 6
CLARK 7782 7
SCOTT 7788 8
KING 7839 9
TURNER 7844 10
ADAMS 7876 11
JAMES 7900 12
FORD 7902 13
MILLER 7934 1
[Updated on: Tue, 03 April 2007 06:01] Report message to a moderator
|
|
|
Re: sql swap [message #228545 is a reply to message #228544] |
Tue, 03 April 2007 06:07 |
pavuluri
Messages: 247 Registered: January 2007
|
Senior Member |
|
|
u r query wrong
see this differnee
SELECT e.*
, ROWNUM
FROM emp e
ORDER BY CASE
WHEN ROWNUM = (SELECT MAX (ROWNUM)
FROM emp)
THEN 1
WHEN ROWNUM = 1
THEN (SELECT MAX (ROWNUM)
FROM emp)
ELSE ROWNUM
END
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ROWNUM
7369 SMITH CLERK 7902 12/17/1980 800 20 1
7499 ALLEN SALESMAN 7698 2/20/1981 1600 300 30 2
7521 WARD SALESMAN 7698 2/22/1981 1250 500 30 3
7566 JONES MANAGER 7839 4/2/1981 2975 20 4
7654 MARTIN SALESMAN 7698 9/28/1981 1250 1400 30 5
7698 BLAKE MANAGER 7839 5/1/1981 2850 30 6
7782 CLARK MANAGER 7839 6/9/1981 2450 10 7
7788 SCOTT ANALYST 7566 4/19/1987 3000 20 8
7839 KING PRESIDENT 11/17/1981 5000 10 9
7844 TURNER SALESMAN 7698 9/8/1981 1500 0 30 10
7876 ADAMS CLERK 7788 5/23/1987 1100 20 11
7900 JAMES CLERK 7698 12/3/1981 950 30 12
7902 FORD ANALYST 7566 12/3/1981 3000 20 13
7934 MILLER CLERK 7782 1/23/1982 1300 10 14
[Mod-edit]Added code tags.
[Updated on: Tue, 03 April 2007 06:16] by Moderator Report message to a moderator
|
|
|
|
Re: sql swap [message #228576 is a reply to message #228544] |
Tue, 03 April 2007 07:24 |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
Step one: You DO believe us when we say that Oracle spits out rows in random order if you don't add an explicit order by on one of your columns?
No, I don't think the OP does really believe us. He sees the data coming back in exactly the same order each time does a query on his little demo tables,and assumes that watever we are talking about doesn't apply.
It's the only conclusion I can reach from the willful refusal to listen displayed on this thread.
|
|
|
|
Re: sql swap [message #228590 is a reply to message #228545] |
Tue, 03 April 2007 07:48 |
joy_division
Messages: 4963 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
pavuluri wrote on Tue, 03 April 2007 07:07 | u r query wrong
see this differnee
|
You really need to go away for a long time and read the Concepts manual. You have no concept of a relational database.
|
|
|
Re: sql swap [message #228609 is a reply to message #228590] |
Tue, 03 April 2007 08:16 |
pavuluri
Messages: 247 Registered: January 2007
|
Senior Member |
|
|
SQL> select ename
2 , empno
3 , case when rownum = 1 then no_of_records
4 when rownum = no_of_records then 1
5 else rownum
6 end my_rownum
7 from (select ename
8 , empno
9 , count(*) over () no_of_records
10 from emp
11 );
ENAME EMPNO MY_ROWNUM
---------- ---------- ----------
SMITH 7369 14
ALLEN 7499 2
WARD 7521 3
JONES 7566 4
MARTIN 7654 5
BLAKE 7698 6
CLARK 7782 7
SCOTT 7788 8
KING 7839 9
TURNER 7844 10
ADAMS 7876 11
JAMES 7900 12
FORD 7902 13
MILLER 7934 1
...................................................
SELECT e.*
, ROWNUM
FROM emp e
ORDER BY CASE
WHEN ROWNUM = (SELECT MAX (ROWNUM)
FROM emp)
THEN 1
WHEN ROWNUM = 1
THEN (SELECT MAX (ROWNUM)
FROM emp)
ELSE ROWNUM
END
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ROWNUM
7369 SMITH CLERK 7902 12/17/1980 800 20 1
7499 ALLEN SALESMAN 7698 2/20/1981 1600 300 30 2
7521 WARD SALESMAN 7698 2/22/1981 1250 500 30 3
7566 JONES MANAGER 7839 4/2/1981 2975 20 4
7654 MARTIN SALESMAN 7698 9/28/1981 1250 1400 30 5
7698 BLAKE MANAGER 7839 5/1/1981 2850 30 6
7782 CLARK MANAGER 7839 6/9/1981 2450 10 7
7788 SCOTT ANALYST 7566 4/19/1987 3000 20 8
7839 KING PRESIDENT 11/17/1981 5000 10 9
7844 TURNER SALESMAN 7698 9/8/1981 1500 0 30 10
7876 ADAMS CLERK 7788 5/23/1987 1100 20 11
7900 JAMES CLERK 7698 12/3/1981 950 30 12
7902 FORD ANALYST 7566 12/3/1981 3000 20 13
7934 MILLER CLERK 7782 1/23/1982 1300 10 14
.............................................
You have no concept of a relational database.
thanks
srinivas
|
|
|
Re: sql swap [message #228611 is a reply to message #228609] |
Tue, 03 April 2007 08:19 |
Frank
Messages: 7901 Registered: March 2000
|
Senior Member |
|
|
Could you please explain what your nice construct does?
What if you remove that and just do a select *, rownum from emp?
|
|
|
Re: sql swap [message #228613 is a reply to message #228609] |
Tue, 03 April 2007 08:21 |
pavuluri
Messages: 247 Registered: January 2007
|
Senior Member |
|
|
sorry
see diff
SQL> select ename
2 , empno
3 , case when rownum = 1 then no_of_records
4 when rownum = no_of_records then 1
5 else rownum
6 end my_rownum
7 from (select ename
8 , empno
9 , count(*) over () no_of_records
10 from emp
11 );
ENAME EMPNO MY_ROWNUM
---------- ---------- ----------
SMITH 7369 14
ALLEN 7499 2
WARD 7521 3
JONES 7566 4
MARTIN 7654 5
BLAKE 7698 6
CLARK 7782 7
SCOTT 7788 8
KING 7839 9
TURNER 7844 10
ADAMS 7876 11
JAMES 7900 12
FORD 7902 13
MILLER 7934 1
...........................
my query
and result
select e.*,rownum from emp e order by case when rownum=(select max(rownum) from emp) then 1
when rownum=1 then (select max(rownum) from emp)
else rownum end
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ROWNUM
7934 MILLER CLERK 7782 1/23/1982 1300 10 14
7499 ALLEN SALESMAN 7698 2/20/1981 1600 300 30 2
7521 WARD SALESMAN 7698 2/22/1981 1250 500 30 3
7566 JONES MANAGER 7839 4/2/1981 2975 20 4
7654 MARTIN SALESMAN 7698 9/28/1981 1250 1400 30 5
7698 BLAKE MANAGER 7839 5/1/1981 2850 30 6
7782 CLARK MANAGER 7839 6/9/1981 2450 10 7
7788 SCOTT ANALYST 7566 4/19/1987 3000 20 8
7839 KING PRESIDENT 11/17/1981 5000 10 9
7844 TURNER SALESMAN 7698 9/8/1981 1500 0 30 10
7876 ADAMS CLERK 7788 5/23/1987 1100 20 11
7900 JAMES CLERK 7698 12/3/1981 950 30 12
7902 FORD ANALYST 7566 12/3/1981 3000 20 13
7369 SMITH CLERK 7902 12/17/1980 800 20 1
see difference. no difference ok
i have no concept of a relational database
|
|
|
Re: sql swap [message #228615 is a reply to message #228613] |
Tue, 03 April 2007 08:27 |
pavuluri
Messages: 247 Registered: January 2007
|
Senior Member |
|
|
hi
frank
u r not swaping only row num is swaping, see ur query result.
see my query result row is swaping.
Thanks,
srinivas
|
|
|
Re: sql swap [message #228617 is a reply to message #228615] |
Tue, 03 April 2007 08:39 |
|
Maaher
Messages: 7065 Registered: December 2001
|
Senior Member |
|
|
- would you please ADD CODE TAGS! Every time you place code, be it a select or a result set. use these tags: [CODE][/CODE] to preserve formatting.
- Add an ORDER BY in your query and "it will be swapped".
I'm tired of repeating myself but: add an order by! But don't take our word for it.
MHE
[Updated on: Wed, 04 April 2007 01:09] Report message to a moderator
|
|
|
Re: sql swap [message #228618 is a reply to message #228615] |
Tue, 03 April 2007 08:40 |
Frank
Messages: 7901 Registered: March 2000
|
Senior Member |
|
|
SQL> select e.*,rownum from emp e order by case when rownum=(select max(rownum) from emp) then 1
2 when rownum=1 then (select max(rownum) from emp)
3 else rownum end;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ROWNUM
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7934 MILLER CLERK 7782 23-JAN-82 1300 10 14
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 2
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 3
7566 JONES MANAGER 7839 02-APR-81 2975 20 4
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 5
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 6
7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20 8
7839 KING PRESIDENT 17-NOV-81 5000 10 9
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 10
7876 ADAMS CLERK 7788 12-JAN-83 1100 20 11
7900 JAMES CLERK 7698 03-DEC-81 950 30 12
7902 FORD ANALYST 7566 03-DEC-81 3000 20 13
7369 SMITH CLERK 7902 17-DEC-80 800 20 1
Now we get further into time and someone changes something toi your table:SQL> create index my_index on emp(empno desc, ename, job, mgr, hiredate, sal, comm, deptno);
Index created.
SQL> analyze table emp compute statistics for table for all indexes;
Table analyzed.
SQL> select e.*,rownum from emp e order by case when rownum=(select max(rownum) from emp) then 1
2 when rownum=1 then (select max(rownum) from emp)
3 else rownum end;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ROWNUM
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20 14
7902 FORD ANALYST 7566 03-DEC-81 3000 20 2
7900 JAMES CLERK 7698 03-DEC-81 950 30 3
7876 ADAMS CLERK 7788 12-JAN-83 1100 20 4
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 5
7839 KING PRESIDENT 17-NOV-81 5000 10 6
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20 7
7782 CLARK MANAGER 7839 09-JUN-81 2450 10 8
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 9
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 10
7566 JONES MANAGER 7839 02-APR-81 2975 20 11
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 12
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 13
7934 MILLER CLERK 7782 23-JAN-82 1300 10 1
14 rows selected.
This is what we mean. You HAVE to use an order by to guarantee the order of rows returned.
|
|
|
|
|
Re: sql swap [message #228675 is a reply to message #228615] |
Tue, 03 April 2007 11:48 |
joy_division
Messages: 4963 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
pavuluri wrote on Tue, 03 April 2007 09:27 | hi
frank
u r not swaping only row num is swaping, see ur query result.
|
What friggin' language is this? Does your resume and professional letters use words like this?
|
|
|
|