Get data starting from the middle of the table - sql

I have a question. I want to get data from the middle of the table. But the problem is I can't start from the middle. I have read that rownum can't be used for this. But i dont know how to do this with another method. Anyone have any clue how to do this? Thank you.
Below is an example.
select count(1) --9857
from time_day_dm
where rownum between 5000 and 9857
The output would be 0.
What i expect would be 4857.

rownum won't help here (as you already know).
What you might do, is to use a couple of analytic functions (count, to find number of rows in the table; row_number, to sort them).
Here's an example based on Scott's emp table. This is its contents:
SQL> select empno, ename, job, sal
2 from emp
3 order by ename;
EMPNO ENAME JOB SAL
---------- ---------- --------- ----------
7876 ADAMS CLERK 1100
7499 ALLEN SALESMAN 1600
7698 BLAKE MANAGER 2850
7782 CLARK MANAGER 2450
7902 FORD ANALYST 3000
7900 JAMES CLERK 950
7566 JONES MANAGER 2975
7839 KING PRESIDENT 5000
7654 MARTIN SALESMAN 1250
7934 MILLER CLERK 1300
7788 SCOTT ANALYST 3000
7369 SMITH CLERK 800
7844 TURNER SALESMAN 1500
7521 WARD SALESMAN 1250
14 rows selected.
Query you might be interested in looks like this:
SQL> with temp as
2 (select empno, ename, job, sal,
3 row_number() over (order by ename) rn,
4 count(*) over () cnt
5 from emp
6 )
7 select *
8 from temp
9 where rn between cnt/2 and cnt;
EMPNO ENAME JOB SAL RN CNT
---------- ---------- --------- ---------- ---------- ----------
7566 JONES MANAGER 2975 7 14
7839 KING PRESIDENT 5000 8 14
7654 MARTIN SALESMAN 1250 9 14
7934 MILLER CLERK 1300 10 14
7788 SCOTT ANALYST 3000 11 14
7369 SMITH CLERK 800 12 14
7844 TURNER SALESMAN 1500 13 14
7521 WARD SALESMAN 1250 14 14
8 rows selected.
SQL>

Related

Creating table with defined variable

I work in SQL Developer by Oracle. I have a simple query to creating new table based on condition where. I want to generate new table with defined variable like below. My code isn't working. How can I define variable and put its directly to query without any entry box?
DEFINE STARTDATE DATE:="TO_DATE('2021-06-01','YYYY-MM-DD')"
CREATE TABLE RESULTS AS
SELECT
*
FROM TABLE_1
WHERE DATA=ADD_MONTHS(:STARTDATE,2);
DEFINE creates a substitution variable.
:STARTDATE uses a bind variable, it is not a substitution variable.
&startdate would be a substution variable.
You want something like:
DEFINE STARTDATE = "DATE '2021-06-01'"
CREATE TABLE RESULTS AS
SELECT *
FROM TABLE_1
WHERE DATA=ADD_MONTHS(&STARTDATE,2);
That's not how define variables works. It is just a text replacement, not a pl/sql variable. try this:
DEFINE STARTDATE =TO_DATE('2021-06-01','YYYY-MM-DD')
In SQL*Plus, it is a substitution variable you'd use (BTW, define syntax you used is wrong).
For example:
SQL> select * From emp order by hiredate;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17.12.80 800 20
7499 ALLEN SALESMAN 7698 20.02.81 1600 300 30
7521 WARD SALESMAN 7698 22.02.81 1250 500 30
7566 JONES MANAGER 7839 02.04.81 2975 20
7698 BLAKE MANAGER 7839 01.05.81 2850 30
7782 CLARK MANAGER 7839 09.06.81 2450 10
7844 TURNER SALESMAN 7698 08.09.81 1500 0 30
7654 MARTIN SALESMAN 7698 28.09.81 1250 1400 30
7839 KING PRESIDENT 17.11.81 5000 10
7900 JAMES CLERK 7698 03.12.81 950 30
7902 FORD ANALYST 7566 03.12.81 3000 20
7934 MILLER CLERK 7782 23.01.82 1300 10
7788 SCOTT ANALYST 7566 09.12.82 3000 20
7876 ADAMS CLERK 7788 12.01.83 1100 20
14 rows selected.
Create a table out of it:
SQL> DEFINE STARTDATE = "1980-12-01"
SQL> create table a1 as select * from emp where trunc(hiredate, 'mm') =
2 trunc(add_months(to_date('&startdate', 'yyyy-mm-dd'), 2));
old 2: trunc(add_months(to_date('&startdate', 'yyyy-mm-dd'), 2))
new 2: trunc(add_months(to_date('1980-12-01', 'yyyy-mm-dd'), 2))
Table created.
SQL> select * from a1;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20.02.81 1600 300 30
7521 WARD SALESMAN 7698 22.02.81 1250 500 30
SQL>

delete all employees who are working with employee(employee name given by user)

Delete all the who are working with that employee, except that Employee.(Prompt for the ENAME).
the employees will have same department number.can anyone help me out.
delete from emptest e where e.deptno IN
(select f.deptno from emptest f where e.deptno=f.deptno AND
e.empno<>f.empno AND f.ename='&ename' );
Sample data:
SQL> set ver off
SQL> undefine par_ename
SQL> select * from employee order by deptno, ename;
DEPTNO ENAME JOB SAL
---------- ---------- --------- ----------
10 CLARK MANAGER 2450 --> department 10: if KING is a parameter,
10 KING PRESIDENT 5001 --> then CLARK and MILLER will be
10 MILLER CLERK 1300 --> deleted, but KING won't
20 ADAMS CLERK 1100
20 FORD ANALYST 3000
20 JONES MANAGER 2975
20 SCOTT ANALYST 3000
20 SMITH CLERK 1000
30 ALLEN SALESMAN 1600
30 BLAKE MANAGER 2850
30 JAMES CLERK 950
30 MARTIN SALESMAN 1250
30 TURNER SALESMAN 1500
30 WARD SALESMAN 1250
14 rows selected.
Query:
SQL> delete from employee e
2 where e.deptno = (select a.deptno
3 from employee a
4 where a.ename = '&&par_ename'
5 )
6 and e.ename <> '&&par_ename';
Enter value for par_ename: KING
2 rows deleted.
Result:
SQL> select * from employee order by deptno, ename;
DEPTNO ENAME JOB SAL
---------- ---------- --------- ----------
10 KING PRESIDENT 5001 --> only KING remains in dept. 10
20 ADAMS CLERK 1100
20 FORD ANALYST 3000
20 JONES MANAGER 2975
20 SCOTT ANALYST 3000
20 SMITH CLERK 1000
30 ALLEN SALESMAN 1600
30 BLAKE MANAGER 2850
30 JAMES CLERK 950
30 MARTIN SALESMAN 1250
30 TURNER SALESMAN 1500
30 WARD SALESMAN 1250
12 rows selected.
SQL>

Why versions_xid value gets reset after some time in Oracle?

While working with row versions query it has been observed the versions_xid value gets removed after some time.
For example:
SQL> select versions_xid, empno, ename, sal from scott.emp
versions between scn minvalue and maxvalue
order by sal;
VERSIONS_XID EMPNO ENAME SAL
---------------- ---------- ---------- ----------
7369 SMITH 800
7900 JAMES 950
7876 ADAMS 1100
7521 WARD 1250
7654 MARTIN 1250
7934 MILLER 1300
7839 KING 1400
7788 SCOTT 1400
7844 TURNER 1500
01002100D6050000 7788 SCOTT 1500
7499 ALLEN 1600
7566 JONES 1800
0800130073070000 7788 SCOTT 1800
06001500DB070000 7788 SCOTT 1900
04000C00B9060000 7788 SCOTT 2000
7782 CLARK 2450
7698 BLAKE 2850
7902 FORD 3000
18 rows selected.
SQL>
After some time when i execute the same query the row versions are gone!
SQL> select versions_xid, empno, ename, sal from scott.emp
versions between scn minvalue and maxvalue
order by sal; 2 3
VERSIONS_XID EMPNO ENAME SAL
---------------- ---------- ---------- ----------
7369 SMITH 800
7900 JAMES 950
7876 ADAMS 1100
7521 WARD 1250
7654 MARTIN 1250
7934 MILLER 1300
7839 KING 1400
7844 TURNER 1500
7499 ALLEN 1600
7566 JONES 1800
7788 SCOTT 2000
7782 CLARK 2450
7698 BLAKE 2850
7902 FORD 3000
14 rows selected.
SQL>
Can someone please let me know whats happening? Is it something to do with undo_retention? Because, reading through various blogs and Oracle docs didn't give a direct answer or relation.

how i can display data like displayed below from two tables [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to display data from two tables, employee and department. It must display department detail then all employees related to that department. This is how I want to display:
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
EMPNO ENAME
---------- ----------
7782 CLARK
7839 KING
7934 MILLER
20 RESEARCH DALLAS
EMPNO ENAME
---------- ----------
7369 SMITH
7566 JONES
7788 SCOTT
7876 ADAMS
7902 FORD
30 SALES CHICAGO
EMPNO ENAME
---------- ----------
7499 ALLEN
7521 WARD
7654 MARTIN
7698 BLAKE
7844 TURNER
7900 JAMES
APPENDIX
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
SQL is designed to retrieve set based raw data, not present it in that kind of format. The best you can do is get the data in the right order -
SELECT
DEPTNO,
DEPT.LOC,
EMPNO,
ENAME
FROM EMP INNER JOIN DEPT
ON EMP.DEPTNO=DEPT.KEY-FIELD
ORDER BY 1,3
.. and then use some report tool to break it up into sections.
(and I hope that's dummy test data, not real salary info :)

COUNT is incorrect when grouping?

I am trying to display the employee number of each employee who manages other employees with the number of people he or she manages with the below table called emp.
empno ename job mgr hiredate sal comm deptno
----- ------ ---------- ---------- ---------- ------- ------- ------
7369 Smith Clerk 7902 1980-12-17 800 20
7499 Allen Salesman 7698 1981-02-20 1600 300 30
7521 Ward Salesman 7698 1981-02-22 1250 500 30
7566 Jones Manager 7839 1981-04-02 2975 20
7654 Martin Salesman 7698 1981-09-28 1250 1400 30
7698 Blake Manager 7839 1981-05-01 2850 30
7782 Clark Manager 7839 1981-06-09 2450 10
7788 Scott Analyst 7566 1982-12-09 3000 20
7839 King President 1981-11-17 5000 10
7844 Turner Salesman 7698 1981-09-08 1500 0 30
7876 Adams Clerk 7788 1983-01-12 1100 20
7900 James Clerk 7698 1983-12-03 950 30
7902 Ford Analyst 7566 1983-12-13 3000 20
7934 Miller Clerk 7782 1982-01-23 1300
Any idea of how I can go about doing this?
I have tried
select empno,count(mgr) from emp group by empno,mgr;
but this returns:
empno count(mgr)
---------- ----------
7369 1
7499 1
7521 1
7566 1
7654 1
7698 1
7782 1
7788 1
7839 0
7844 1
7876 1
7900 1
7902 1
7934 1
Thanks so much for your help.
select count(*) from employee_table group by mgr
I would actualy group by mgr, then you'd have a group per manager and can just do a count to see how many persons that manager manages. Then, you could do a self join on the table to get that manager's info. Something like:
SELECT E1.Mgr, E2.ename, Count(*) as Number FROM Employees E1
INNER JOIN Employees E2 ON E1.mgr = E2.empno
GROUP BY E1.Mgr
Though I haven't tested this.
You can try these that do not use join:
select mgr, count(eno)
from employee
group by mgr
or
select name, e1.mgr, count(e1.eno)
from employee e1
group by rollup (e1.mgr, name)