A problem with getting the correct data - sql

I have created a query that looks like this but I havent found a way to group the departments together. Thats the part I need help with. I want to get Department number in one colum, Employee is another and then the person that would be their colleague in another. Any help would be great.
This is what I want it to look like
DEPARTMENT EMPLOYEE COLLEAGUE
10 CLARK KING
10 CLARK MILLER
10 KING CLARK
10 KING MILLER
10 MILLER CLARK
10 MILLER KING
20 ADAMS FORD
20 ADAMS JONES
20 ADAMS SCOTT
20 ADAMS SMITH
And here is what I got so far but is not correct.Kings colleague is not King its Clark and Miller. Clarks colleague is not Clark its king and Miller.
SQL> select ename AS Employee, deptno AS Department, ename AS Colleague from emp order by deptno asc;
EMPLOYEE DEPARTMENT COLLEAGUE
---------- ---------- ----------
KING 10 KING
CLARK 10 CLARK
MILLER 10 MILLER
ADAMS 20 ADAMS
SCOTT 20 SCOTT
SMITH 20 SMITH
FORD 20 FORD
JONES 20 JONES
WARD 30 WARD
JAMES 30 JAMES
ALLEN 30 ALLEN
EMPLOYEE DEPARTMENT COLLEAGUE
---------- ---------- ----------
MARTIN 30 MARTIN
BLAKE 30 BLAKE
TURNER 30 TURNER
14 rows selected.
Heres the dept table
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
Heres the emp table
SQL> select ename, deptno, job from emp;
ENAME DEPTNO JOB
---------- ---------- ---------
KING 10 PRESIDENT
BLAKE 30 MANAGER
CLARK 10 MANAGER
JONES 20 MANAGER
MARTIN 30 SALESMAN
ALLEN 30 SALESMAN
TURNER 30 SALESMAN
JAMES 30 CLERK
WARD 30 SALESMAN
FORD 20 ANALYST
SMITH 20 CLERK
ENAME DEPTNO JOB
---------- ---------- ---------
SCOTT 20 ANALYST
ADAMS 20 CLERK
MILLER 10 CLERK
14 rows selected.

It sounds like you want something like
SQL> ed
Wrote file afiedt.buf
1 select deptno department,
2 e1.ename employee,
3 e2.ename colleague
4 from emp e1 join emp e2 using (deptno)
5 where e1.empno != e2.empno
6* order by deptno
SQL> /
DEPARTMENT EMPLOYEE COLLEAGUE
---------- ---------- ----------
10 MILLER CLARK
10 KING CLARK
10 MILLER KING
10 CLARK MILLER
10 KING MILLER
10 CLARK KING
<<additional results removed>>

Select distinct d.dname, t1.employee, t2.employee college
From emp t1
inner join dept d on d.deptno = t1.deptno
Inner join emp t2 on t2.deptno = t1.deptno and t1.employee <> t2.employee

Related

Get data starting from the middle of the table

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>

tree structure oracle sql [duplicate]

This question already has answers here:
SQL Tree Structure
(4 answers)
Closed 6 months ago.
I would like to show employees hierarchy in tree structure.
I have this table.
Employee
-------------
EMP_ID (Number)
JOB_ID (Number)
Manager_ID (Number)
Effective_from (Date)
Effective_to (Date)
EMP_ID represents the Manager employee has. I would like to show the whole tree structure of Managers.
Table for reference.
EMP_FIRST_NAME JOB_DESCRIPTION MANAGER EFFECTIVE_F EFFECTIVE_T
--------------- -------------------- ---------- ----------- -----------
Tomm General Manager 01-jan-2000 01-jan-3000
Mohammed Senior Accountant Tomm 01-jan-2000 01-jan-3000
Ali Accountant Tomm 01-jan-2000 01-jan-3000
Basel Accountant Mohammed 01-jan-2000 01-jan-3000
This is a classic hierarchical query; demonstrated on Scott's EMP table:
SQL> select * From employee;
EMP_ID EMPLOYEE_N JOB MANAGER_ID
---------- ---------- --------- ----------
7369 SMITH CLERK 7902
7499 ALLEN SALESMAN 7698
7521 WARD SALESMAN 7698
7566 JONES MANAGER 7839
7654 MARTIN SALESMAN 7698
7698 BLAKE MANAGER 7839
7782 CLARK MANAGER 7839
7788 SCOTT ANALYST 7566
7839 KING PRESIDENT
7844 TURNER SALESMAN 7698
7876 ADAMS CLERK 7788
7900 JAMES CLERK 7698
7902 FORD ANALYST 7566
7934 MILLER CLERK 7782
14 rows selected.
You'd then
SQL> select lpad(employee_name, 2 * (level + 1), ' ') name
2 from employee e
3 start with manager_id is null
4 connect by prior emp_id = manager_id;
NAME
--------------------------------------------------------------------------------
KING
JONES
SCOTT
ADAMS
FORD
SMITH
BLAKE
ALLEN
WARD
MARTIN
TURNER
JAMES
CLARK
MILLER
14 rows selected.
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>

SQL query to display a parent table's record followed by all its child table's record

I need to write down a single SQL statement that displays the following output by using tables EMP and DEPT.
Output:
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
6 rows selected.
40 OPERATIONS BOSTON
no rows selected
So I need this specific output from a query where the dept information is shown first then all its respective records from the child table.
From my point of view, that task isn't as simple as it looks. OK, my SQL*Plus capabilities probably aren't as good as they used to / should be. The main problem is how to make EMP table's headings repeat for each department (and no, I don't know how to do that).
The closest result I managed to produce is this:
SQL> col deptno format 999999
SQL> col dname format a10
SQL> col loc format a10
SQL> set linesize 30
SQL> set recsep off
SQL> break on deptno on dname on loc
SQL>
SQL> select d.deptno, d.dname, d.loc, e.empno, e.ename
2 from dept d left join emp e on e.deptno = d.deptno
3 order by d.deptno;
DEPTNO DNAME LOC
------- ---------- ----------
EMPNO ENAME
---------- ----------
10 ACCOUNTING NEW YORK
7839 KING
7782 CLARK
7934 MILLER
20 RESEARCH DALLAS
7902 FORD
7369 SMITH
7566 JONES
30 SALES CHICAGO
7900 JAMES
7844 TURNER
7654 MARTIN
7521 WARD
7499 ALLEN
7698 BLAKE
40 OPERATIONS BOSTON
13 rows selected.
Once you, Enferno (or anyone else) manages to get exactly what's been asked for, I'd really like to see the final code.

Select Distinct by 4 columns and return all columns in table

I need to return select distinct for first 4 column together,and return all others column remaining.For Example if i Have:
DEPTNO LOC JOB SAL ENAME
---------- ------------- --------- ---------- ----------
10 NEW YORK CLERK 1300 MILLER
10 NEW YORK MANAGER 2450 CLARK
10 NEW YORK CLERK 1300 KING
20 DALLAS ANALYST 3000 FORD
20 DALLAS ANALYST 3000 SCOTT
20 DALLAS CLERK 800 SMITH
20 DALLAS CLERK 1100 ADAMS
20 DALLAS MANAGER 2975 JONES
30 CHICAGO CLERK 950 JAMES
30 CHICAGO MANAGER 2850 BLAKE
30 CHICAGO SALESMAN 1250 MARTIN
30 CHICAGO SALESMAN 1250 WARD
30 CHICAGO SALESMAN 1500 TURNER
30 CHICAGO SALESMAN 1600 ALLEN
I need to return:
DEPTNO LOC JOB SAL ENAME
---------- ------------- --------- ---------- ----------
10 NEW YORK CLERK 1300 MILLER
10 NEW YORK MANAGER 1300 CLARK
20 DALLAS ANALYST 3000 SCOTT
20 DALLAS CLERK 800 SMITH
20 DALLAS CLERK 1100 ADAMS
20 DALLAS MANAGER 2975 JONES
30 CHICAGO CLERK 950 JAMES
30 CHICAGO MANAGER 2850 BLAKE
30 CHICAGO SALESMAN 1250 MARTIN
30 CHICAGO SALESMAN 1500 TURNER
30 CHICAGO SALESMAN 1600 ALLEN
In other words if first 4 column are unique(togheter) i return whole row.
I try with select distinct but i get only column that i write in command:
select distinct DEPTNO,LOC,JOB,SAL from my_table
will not return ENAME.
Than I try to use inner join, but than I get all columns from table.
Thanx
You haven't specified how you want it to choose between multiple ENAMEs. In this example, it returns the first one in alphabetical order:
SELECT DISTINCT
deptno
,loc
,job
,sal
,MIN(ename)
OVER (PARTITION BY deptno, loc, job, sal)
AS ename
FROM my_table;
you also can try this way
Example :
create table ttt
(
DEPTNO int ,
LOC varchar(20),
JOB varchar(20),
SAL int,
ENAME varchar(20)
)
--
insert into
ttt
values(10,'NEW YORK','CLERK',1300,'MILLER'),
(10,'NEW YORK','MANAGER',2450,'CLARK'),
(10,'NEW YORK','MANAGER',2450,'KING')
--
SELECT
DISTINCT
DEPTNO,
LOC,
JOB,
SAL,
MAX(ENAME) from ttt
GROUP BY
DEPTNO,
LOC,
JOB,
SAL
Here is the SQL Fiddle for this
Click To See Demo