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

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 :)

Related

what is query to display only column of target table using left joins

EMP table:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7839 KING PRESIDENT 17-NOV-81 5000 10
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
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
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10
emp_target table:
TOTAL_NO_OF_EMPLOYEES TOTAL_SAL HIGHEST_SAL LOWEST_SAL AVERGAE_SAL DEPTNO
--------------------- ---------- ----------- ---------- ----------- ----------
6 9400 2850 950 1567 30
5 10875 3000 800 2175 20
3 8750 5000 1300 2917 10
I want to display only:
TOTAL_NO_OF_EMPLOYEES TOTAL_SAL HIGHEST_SAL LOWEST_SAL AVERGAE_SAL DEPTNO
--------------------- ---------- ----------- ---------- ----------- ----------
Output i tried:
Query :
select count(empno) as no_of_employees,sum(sal) as toatl_sal,
max(sal) as highest_Sal,round(avg(sal)) as average_sal, min(sal) as lowest_sal
from emp_target right join emp ON emp_target.deptno = emp_target.deptno ;
Output:
NO_OF_EMPLOYEES TOATL_SAL HIGHEST_SAL AVERAGE_SAL LOWEST_SAL
--------------- ---------- ----------- ----------- ----------
42 87075 5000 2073 800

select and display employess who have the same deptno and mgr as the prompted ename by the user [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Display the employees who have the same DEPTNO and MGR of a given employee(Prompt), excluding that employee.
can anyone help me with this one and tell me how i exclude the employee that is given by the user.
select ename,deptno,mgr from emp where
deptno=(select deptno from emp where ename='&&ename')
AND
mgr=(select mgr from emp where ename='&ename');
For example:
SQL> select deptno, empno, ename, mgr
2 from emp
3 order by deptno, mgr;
DEPTNO EMPNO ENAME MGR
---------- ---------- ---------- ----------
10 7934 MILLER 7782
10 7782 CLARK 7839
10 7839 KING
20 7788 SCOTT 7566
20 7902 FORD 7566
20 7876 ADAMS 7788
20 7566 JONES 7839
20 7369 SMITH 7902
30 7521 WARD 7698 --> we'll watch these
30 7499 ALLEN 7698 -->
30 7844 TURNER 7698 -->
30 7900 JAMES 7698 -->
30 7654 MARTIN 7698 --> employees
30 7698 BLAKE 7839
14 rows selected.
Query: lines #3 - 6 select those with the same DEPTNO and MGR, while line #7 excludes that very employee (identified by PAR_EMPNO).
SQL> select e.*
2 from emp e
3 where (e.deptno, e.mgr) = (select a.deptno, a.mgr
4 from emp a
5 where a.empno = &&par_empno
6 )
7 and e.empno <> &&par_empno;
Enter value for par_empno: 7521
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ---------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 02/20/1981 1600 300 30
7654 MARTIN SALESMAN 7698 09/28/1981 1250 1400 30
7844 TURNER SALESMAN 7698 09/08/1981 1500 0 30
7900 JAMES CLERK 7698 12/03/1981 950 30
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.

In what order distinct clause with one column(say) displays the output in oracle? [duplicate]

This question already has answers here:
Default row ordering for select query in oracle
(8 answers)
Closed 9 years ago.
EMP table
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 Smith Clerk 7902 12/17/1980 800 30
7499 Allen Salesman 7698 2/20/1981 1600 300 30
7521 Ward Salesman 7698 2/22/1981 1250 500 30
7566 Jones Manager 7839 4/2/1981 2975 20
7654 Martin Salesman 7698 9/28/1981 1250 1400 30
7698 Blake Manager 7839 5/1/1981 2850 30
7782 Clark Manager 7839 6/9/1981 2450 10
7788 Scott Analyst 7566 12/9/1982 3000 20
7839 King President 11/17/1981 5000 10
7844 Turner Salesman 7698 9/8/1981 1500 30
7876 Adams Clerk 7788 1/12/1983 1100 20
7900 James Clerk 7698 12/3/1981 950 30
7902 Ford Analyst 7566 12/3/1981 3000 20
7934 Miller Clerk 7782 1/23/1982 1300 10
Suppose we executed the following query on the above table:
select distinct ename from emp;
Output
ENAME
Ward
Turner
Adams
Allen
Martin
Blake
Clark
Scott
Ford
King
Miller
Jones
Smith
James
Now anyone Please explain me why Ward is displayed as output in the 1st row and not in the order of the table output format??
Because you have to specify an order
select distinct ename
from emp
order by ename
If you don't then the DB will grab the records in the name of performance and output it unordered.
Without ordering how you can expect ordered result.Use order by clause.

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)