Matching EMP and DEPT
Hi all
11.2.0.1
I have departments in the DEPT table which are 10 and 20. Department 10 has 100 employees in EMP while 20 Department hasn't in the EMP, so there is no corresponding line.
How can I create a query such as all the dept with no EMP will be displayed:
EMP DEPT
==== ====
10 100
20 0
30 20
Thank you
pK
You must learn more about Outer Join
Select d.deptno
count (e.empno) emp_count
d Dept
Join emp left e
on d.deptno = e.deptno
Group
by d.deptno
Tags: Database
Similar Questions
-
Tables to be used:
1 scott.emp
2 scott.dept
For all s deptno in dept table if there is at least 1 used in the emp table output should be 1 otherwise the output must be 0.
For example, deptno in dept table 40 have no employees present in the emp table if the output should be 0.
Desired output:
REQ
----------
0
Now, if I add a line in the table emp which deptno is 40 (now for all deptnos in the dept table, there is record emp at least 1), production should be 1
REQ
----------
1
I formed query as below but do not know how to do furthur
SELECT count (empno), deptno from emp by deptno group
Hello
2742751 wrote:
Select dept Dept. d
where 0! =
(select count (*) in e emp
where e.deptno = d.deptno
);
This will not work; There is no column dept in the dept table.
Select deptno from dept d
where there are
(select count (*) in e emp
where e.deptno = d.deptno
);
Do not know how to perform tried furthur two queries
Good start. It's a good way to use a correlated EXISTS subquery. (EXISTS subqueries are almost always correlated. Why? Discuss among you).
The query above tells you what are departments have used; He said nothing on which federal departments have not used, and this is what primarily interests us. There are probably still some departments that have employees (even if you test this in any case); the real question here is: Y at - it all departments that have not used? So change IS not there and see what you get.
SELECT DeptNo
D DEPT
WHERE DOES NOT EXIST)
SELECT 1 - keep it simple
FROM emp e
WHERE e.deptno = d.deptno
)
ORDER BY deptno
;
It's okay put you in the SELECT a clause subquery EXISTS; the subquery does not return actually what you say, the subquery just returns TRUE or FALSE. You need to select for syntax, but you might as well put something that is easy to type at this location.
Here is the result of the above query:
DEPTNO
----------
40
What is the problem with that? I see two problems:
It produces the real Department deptno; We want that it displays 0 where there is a Ministry without employee. It's an easy solution: change the SELECT main clause so that it returns the literal 0 instead of deptno.
The other problem is trickier. The above query produces 1 row of output for each Department that satisfies the condition in the WHERE clause. In the table scott.emp, it happens to be just 1 row, but if there were 2 or 3, or 2300 these departments, then the above query would produce 2 or 3 or 2300 lines of output. What happens if 0 rows meet the condition in the WHERE clause? Then the query would produce 0 lines of output. It's no good: we want exactly 1 row of output, regardless of the number of rows satisfy the WHERE clause. Do you know of any which is guaranteed to produce 1 row of output, regardless of the number of lines it finds, even if it is 0 rows? An aggregate function without a GROUP BY doing the exact clause that: she produced exactly 1 row of output, no matter what, if anything, that it finds in the table, we can say:
SELECT MIN (0) AS req
D DEPT
WHERE DOES NOT EXIST)
SELECT 1
FROM emp e
WHERE e.deptno = d.deptno
)
ORDER BY deptno
;
Output:
REQ
----------
0
0 being the only value which is ever produced, no matter if we use MIN or MAX. We could even use AVG or SUM if we wanted to be cute.
However, there is still a problem: If no rows satisfy the WHERE clause, no 0 will be produced, so MIN returns null. We want the query to display 1 in this case, not NULL, so we can use NVL do display 1 instead of NULL, like this:
SELECT NVL (MIN (0)
1
) AS req
D DEPT
WHERE DOES NOT EXIST)
SELECT 1
FROM scott.emp e
WHERE e.deptno = d.deptno
)
ORDER BY deptno
;
Furthermore, I copy the scott schema emp and dept tables in my schema to test this, so I could add or remove data to test the situations as
- several services without employees
- no representation without employees
- no departments at all (emply dept table)
- no departments with emplpoyees
You don't want to edit any table provided by Oracle; You must keep the scott paintings exactly as they are, then, make copies you can change anything you want.
-
I pay to match iTunes and I can stop listening to the radio. Apple keeps prompting me to pay for music from Apple. Why? Radio without advertising is included with iTunes game! It was listed as a feature in my terms of service I renewed for 1 year in October
iTunes Radio is no longer included in iTunes game. These same terms of service as Apple status may change the features included at any time.
http://www.Apple.com/legal/Internet-services/iTunes/us/terms.html
You can try calling Apple Service customer. People have reported that they were able to cancel iTunes game and get a pro-rated refund.
-
Hi scripters
I am using the command match if the text in the selection summary ndash and mdash. Here's my step
myTxt = app.selection [0] .silence;
results myTxt "MC - AC"
var a = mytxt.match('–');
Alert (a);
I can't match really here, I get "null"? How can I match Ndash and Mdash?
Thanks in advance
concerning
a you are the
var a = mytxt.match (/ [\u2013\u2014]/);
Peter
-
WHS 2011 and by connecting a Win7 without matching username and the name of the user directory
Working with 2011 and by connecting a Win7 computer server said, are there computer autour or difficulty of connection of said work even if the user name and the directory of the user do not match? I think that's the question when connecting computers and I was wondering if there is no work around.
Hello
-Are computers in the domain environment?It is better suited for the IT Pro TechNet public. Please ask your question in the Forum on TechNet Support. You can follow the link to your question:
http://social.Microsoft.com/forums/is/whs2011/threadsHope this information helps. -
Hello
I'm trying to achieve the following objectives:
1. in table A, select rows based on the values in column 2. something like SELECT * FROM TABLE A WHERE (COLUMN2 = 'X' or Column2 IS NULL)
2 and these values selected, I want to update Column3 from Table A if TableA.column1 = TableB.column1, but only if there is exactly one game. If there are multiple matches, column 3 of the table article updated.
That's what I've tried so far.
UPDATE TABLE_A
SET
TABLE_A.COLUMN3 = (SELECT COLUMN3 OF TABLE_B
WHERE ((TABLE_B.COLUMN1 = TABLE_A.COLUMN1) AND ( TABLE_B.COLUMN1 IN (SELECT Column1 FROM TABLE_B GROUP BY COLUMN1 , HAVING COUNT (*) = 1)))
WHERE EXISTS (SELECT * FROM TABLE_A)
WHERE ((TABLE_A.COLUMN2 = 'X' OU TABLE_A.COLUMN2 = 'Y') AND (TABLE_A.COLUMN4 IS NULL OR TABLE_A.COLUMN4 = ' ')));
More details on my DB environment:
Version Info:
Oracle Database 11 g Enterprise Edition Release 11.2.0.4.0 - 64 bit Production
PL/SQL Release 11.2.0.4.0 - Production
Toad, but, depending on whether the query updated all lines. I would really appreciate if someone could tell me how to fix my request.
Thanks in advance.
Exists it predicate in the block of update will be set to true if there is at least one row in table_a where column2 is X or Y and column4 is null or a space. You need to correlate exists it with the outer query query (I'm guessing on column1) to get the result I think you want. However, who would update all rows in table_a who meets the criteria, there is a corresponding row in table_b, affecting Column3 lines form null not matched or not. (Again), I'm guessing that's not your intention. If you only want to update the lines in table_a which have a corresponding line in table_b and meet the other predicate, then I think you want something more like:
Update table_a
Set table_a.column3 = (select column3 of table_b
where table_b.column1 = table_a.column1 and
Table_B.Column1 in (select column1 from table_b
Group by column1, having count (*) = 1))
where ((table_a.column2 = 'X' ou))
table_a.Column2 = 'Y') and
(table_a.column4 is null or)
table_a.column4 = ' ')) and
table_a.Column1 in (select column1 from table_b
Group by column1, having count (*) = 1)
John
-
match, drag and drop the game
Hello
My name is Nikola and I am beginner with ACE
Here's the thing:
With the help of a tutorial I created a "game", where the user selects objects, it drags and it falls on the correct shape/color/object. Tutorial is based "1 to 1", so you can match an object and a target only.
Now I have several objects and a target on which I can drop.
Example:
TARGET: square blue
OBJECTS: Circle Blue Star, blue, etc.
I'm stuck with Instance and Movie clip names since they must be unique, and the code itself works on the principle of the names Peel - object: name, target: 'target' + object name.
Here is a code for the option "1 to 1":
var objectoriginalX:Number;
var objectoriginalY:Number;
shape_mc.buttonMode = true;
shape_mc.addEventListener (MouseEvent.MOUSE_DOWN, pickupObject);
shape_mc.addEventListener (MouseEvent.MOUSE_UP, dropObject);
shape2_mc.buttonMode = true;
shape2_mc.addEventListener (MouseEvent.MOUSE_DOWN, pickupObject);
shape2_mc.addEventListener (MouseEvent.MOUSE_UP, dropObject);
function pickupObject(event:MouseEvent):void {}
event.target.startDrag (true);
event.target.parent.addChild (event.target);
objectoriginalX = event.target.x;
objectoriginalY = event.target.y;
}
function dropObject(event:MouseEvent):void {}
event.target.stopDrag ();
var matchingTargetName:String = "target" + event.target.name;
var matchingTarget:DisplayObject = getChildByName (matchingTargetName);
If (event.target.dropTarget! = null & & event.target.dropTarget.parent == matchingTarget) {}
event.target.removeEventListener (MouseEvent.MOUSE_DOWN, pickupObject);
event.target.removeEventListener (MouseEvent.MOUSE_UP, dropObject);
event.target.buttonMode = false;
Event.Target.x = matchingTarget.x;
Event.Target.y = matchingTarget.y;
} else {}
Event.Target.x = objectoriginalX;
Event.Target.y = objectoriginalY;
}
}
I hope I was clear,
Thank you
Nikola
for your special example give all corresponding instances an identical letter to foreground:
as: bStar, bSquare, specialist for all blue shapes and gStar, gHeart etc. for all those green... you get the idea
then change your function
function dropObject(event:MouseEvent):void
{
event.target.stopDrag ();
check if the leader for two Sprites letter is the same
If (testIfMatch (event.target.name, event.target.dropTarget.parent.name))
{
event.target.removeEventListener (MouseEvent.MOUSE_DOWN, pickupObject);
event.target.removeEventListener (MouseEvent.MOUSE_UP, dropObject);
event.target.buttonMode = false;
}
on the other
{
Event.Target.x = objectoriginalX;
Event.Target.y = objectoriginalY;
}
}
function testIfMatch(_drop:String,__target:String):Boolean
{
If (_drop.substr (0, 1) is _target.substr (0, 1))
{
Returns true;
}
on the other
{
return false
}
}
-
Match iTunes and Apple's music
Hello everyone,
First post here so please bear with me. I got a glance at other posts and I find similarities, but please read below.
As I have only 128 GB on my MacBook Air and half taken upwards by the music I had iTunes game recently. The idea was to download ALL my music on the cloud and then access and/or download from the cloud on the go. After completing the game, I'm sure that my music has been downloaded (it took awhile) but I still have all my songs on my hd. So how can I / can I delete my files all having access to music in the cloud? I have Apple music in my car and accessing my entire library of 'My Music' from there, but I fear that this will not be the case if I delete my music on my Mac and it is mirrored at the game. All this makes sense?
Thanks in advance.
Hello
Check this users iTunes game tips
Make and keep a backup of your iTunes library and only flows from the Cloud.
JIm
-
I'm subscribed to iTunes game so that I can scan all my old vinyl albums. I finished this task. So now, I have a subscription to apple's music and a subscription of iTunes game. Is it necessary to have both? What features I could lose if I cancel the game?
Hello
Read this tip user iTunes game usage tips
Jim
-
Developers are developing on 12.1.3, I don't want to operate on a lower version. If 12.1.3 is not available upon delivery, when will it be?
New Oracle S/W delivery cloud. addition of WebLogic Enterprise - Oracle WebLogic Server (12.1.3.0.0), 1 file
Oracle WebLogic Server (12.1.2.0.0) V38518 - 01.zip Oracle Fusion Middleware 12 c WebLogic Server and consistency (12.1.2.0.0) 880,0 MB Oracle WebLogic Server (12.1.3.0.0) V44413 - 01.zip Coherence and oracle Fusion Middleware 12 c (12.1.3.0.0) WebLogic Server 880.3 MB -
Visible bullets in other "pages" app after match "drag-and - drop. Help please!
After you change the code from a tutoial, I've got the game part of drag n drop of my project educational app works beautifully.
The misfortune is that, if I hit the home button, which is common to all 'pages' in my app away game, the bullets that were abandoned in the correct position remain visible on the stage in all other pages.
I guess this is because the AS code removes the listener of events so once slipped to the correct position in the game, they snap there and can not be moved.
Am I wrong?
How can I solve the problem of the balls ignored being visible on other "pages".
Thanks in advance and here is the code on the page of the application.
var counter: Number = 0;
var startX:Number;
var startY: number;
peg1_mc.addEventListener (MouseEvent.MOUSE_DOWN, Pick-up);
peg1_mc.addEventListener (MouseEvent.MOUSE_UP, dropIt);
peg2_mc.addEventListener (MouseEvent.MOUSE_DOWN, Pick-up);
peg2_mc.addEventListener (MouseEvent.MOUSE_UP, dropIt);
peg3_mc.addEventListener (MouseEvent.MOUSE_DOWN, Pick-up);
peg3_mc.addEventListener (MouseEvent.MOUSE_UP, dropIt);
peg4_mc.addEventListener (MouseEvent.MOUSE_DOWN, Pick-up);
peg4_mc.addEventListener (MouseEvent.MOUSE_UP, dropIt);
peg5_mc.addEventListener (MouseEvent.MOUSE_DOWN, Pick-up);
peg5_mc.addEventListener (MouseEvent.MOUSE_UP, dropIt);
peg6_mc.addEventListener (MouseEvent.MOUSE_DOWN, Pick-up);
peg6_mc.addEventListener (MouseEvent.MOUSE_UP, dropIt);
peg7_mc.addEventListener (MouseEvent.MOUSE_DOWN, Pick-up);
peg7_mc.addEventListener (MouseEvent.MOUSE_UP, dropIt);
peg8_mc.addEventListener (MouseEvent.MOUSE_DOWN, Pick-up);
peg8_mc.addEventListener (MouseEvent.MOUSE_UP, dropIt);
peg9_mc.addEventListener (MouseEvent.MOUSE_DOWN, Pick-up);
peg9_mc.addEventListener (MouseEvent.MOUSE_UP, dropIt);
peg10_mc.addEventListener (MouseEvent.MOUSE_DOWN, Pick-up);
peg10_mc.addEventListener (MouseEvent.MOUSE_UP, dropIt);
peg11_mc.addEventListener (MouseEvent.MOUSE_DOWN, Pick-up);
peg11_mc.addEventListener (MouseEvent.MOUSE_UP, dropIt);
peg12_mc.addEventListener (MouseEvent.MOUSE_DOWN, Pick-up);
peg12_mc.addEventListener (MouseEvent.MOUSE_UP, dropIt);
function pickUp(event:MouseEvent):void {}
event.target.startDrag (true);
reply_txt. Text = "";
event.target.parent.addChild (event.target);
startX = event.target.x;
startY = event.target.y;
}
function dropIt(event:MouseEvent):void {}
event.target.stopDrag ();
var myTargetName:String = "target" + event.target.name;
var myTarget:DisplayObject = getChildByName (myTargetName);
If (event.target.dropTarget! = null & & event.target.dropTarget.parent == myTarget) {}
reply_txt. Text = "Good Job!"
event.target.removeEventListener (MouseEvent.MOUSE_DOWN, Pick-up);
event.target.removeEventListener (MouseEvent.MOUSE_UP, dropIt);
event.target.buttonMode = false;
Event.Target.x = myTarget.x;
Event.Target.y = myTarget.y;
counter ++;
} else {}
reply_txt. Text = "Try Again!";
Event.Target.x = startX;
Event.Target.y = startY;
}
if(Counter == 12) {}
reply_txt. Text = "congratulations, you're done!"
}
}
peg1_mc.buttonMode = true;
peg2_mc.buttonMode = true;
peg3_mc.buttonMode = true;
peg4_mc.buttonMode = true;
peg5_mc.buttonMode = true;
peg6_mc.buttonMode = true;
peg7_mc.buttonMode = true;
peg8_mc.buttonMode = true;
peg9_mc.buttonMode = true;
peg10_mc.buttonMode = true;
peg11_mc.buttonMode = true;
peg12_mc.buttonMode = true;
use:
home_btn.addEventListener (MouseEvent.CLICK, homeClicked);
function homeClicked(event:MouseEvent):void
{
SoundMixer.stopAll ();
{if(reparentedBalls!=null)}
for (var i: uint = 0; i<>
{if(reparentedBalls[i]!=null&&reparentedBalls[i].parent!=null)}
reparentedBalls [i].parent.removeChild (reparentedBalls [i]);
}
}
}
gotoAndStop ("home")
}
-
DEP. and emp on the same line. pls help
I want to output in two columns with the table emp and dept
Col1: DetpNo
Col2: Concatenated employee numbers separated by commas
For example
If dept contains
1 electric
2 mechanics
3 comp
Contanis table emp
John 100 1
200 Smith 2
Cobb 300 1
I need output like
Col1 Col2
1 100 300
2,200Try this
select deptno, rtrim(xmlagg(xmlelement(e,empno||',').extract('//text()')),',') from employee group by deptno /
-
Hi all
We use the Oracle 10 g R2 64 bits on 64-bit windows server 2008.
I have the following tables
CREATE TABLE dept ( DEPARTMENT_ID NUMBER primary key, DEPARTMENT_NAME VARCHAR2(100) NOT NULL, DEPARTMENT_TYPE CHAR(1) NOT NULL, /* S for section, D for DEPARTMENT*/ REFERENCE_DEPARTMENT NUMBER/* Self reference to department_id */ ); alter table dept add constraint fk_dept_id_reference_dept foreign key REFERENCE_DEPARTMENT references dept(department_id); insert into dept values (1,'ETC','D',Null); insert into dept values (2,'CSS','S',1); insert into dept values (3,'ESS','S',1); commit; create table emp ( empno number primary key, ename varchar2(1000), department_id number references dept(department_id) ); insert into emp values (1,'Jones',1); insert into emp values (2,'Smith',2); insert into emp values (3,'Scott',3); commit;
Now we need a query that selects all employees, in each Department and the name of the service
We use the following query
select count(e.empno) Total_Employees,d.department_name from emp e, dept d where e.department_id=d.department_id;
The result shows as well the section names, while we want to display a result as below
Total_Employees Department_Name 3 ETC
We do not want to display the names of section, of course we want to count employees in all sections of a Ministry, or we only want to display the name of the Department.
I hope I'm clear in the description of my problem.
Thank you.
Hello
Maahjoor wrote:
I have 14 departments and nearly 25 sections.
I can't just include every department as HE said in HIS application
- Select department_id 1, "ETC." DEPARTMENT_NAME 'd' DEPARTMENT_TYPE, Null...
I have many ministries and numerous sections.
If I include all services as IT were included in the above sentence, my request will become 2-3 hundreds of lines.
IT should use only the names of actual columns in your query, not the service name, for example ETC.
concerning
No, the length of the query is the same, regardless of the number of rows in the tables. As Etbin explained in response to #8, emp and dept subqueries are not part of the solution; they are just average Etbin of testing the solution without creating any actual table. Since you have real tables, you can start with the following subquery solution, i.e.:
WITH the_data as
(select d.department_id, d.department_name, d.department_type, d.reference_department, c.emp_count
by d Dept.
...
-
Hello
I'm having some trouble with the values defined in LOVs areas. I created a free workspace in Oracle APEX.
I created a form with two fields of LOVs, EMP and DEPT, after that I created a dynamic action to set the value on the LOV Dept field. Then always when a new EMP is selected, the LOV Dept field must define the departament of the chosen MAP.
Dynamic action has not worked with LOVs fields, always when I changed the field EMP LOV LOV DEPT field is canceled, but when I changed the DEPT LOV to SELECT LIST field, it's good work.
dynamic action properties:
event: change; *
Select type: points; *
point: EMP_LOV*.
Condition: no condition; *
Action: value; +
setType: Sql statement+.
Sql statement:+.
Select DEPTNO+.
from EMP+.
where EMP. EMPNO =: EMP_LOV+.
Assigned items: DEPT_LOV+.
Brgds,
André.
Edited by: 969316 11/03/2012 07:36
Edited by: 969316 11/03/2012 07:36Try sth like this:
First create a hidden item, let's call it P1_DEPTHIDDEN then
Create dynamic action:
Event: Change Selection Type: Item Item: P1_EMPLOYEE
Now for the first real action follow these steps:
Action: Execute PL/SQL Code BEGIN SELECT DEPTNO INTO :P1_DEPARTMENT FROM EMP WHERE EMPNO = :P1_EMPLOYEE; END; Page Items to Submit: P1_EMPLOYEE Page Items to Return: P1_DEPARTMENT_HIDDEN -- This is return value, the value you will store in your database table (not the one you see on the screen)
Now for the second True action follow these steps:
Action: Execute PL/SQL Code BEGIN SELECT DNAME INTO :P1_DEPTHIDDEN FROM DEPT, EMP WHERE DEPT.DEPTNO = EMP.DEPTNO AND EMPNO = :P1_EMPLOYEE; END; Page Items to Submit: P1_EMPLOYEE Page Items to Return: P1_DEPTHIDDEN -- This is display value, the value you actually need to see in P1_DEPARTMENT item !
Now for the third True action follow these steps:
Action: Execute JavaScript Code $x('P1_DEPARTMENT').value = $x('P1_DEPTHIDDEN').value /* Now you set P1_DEPARTMENT value to be exactly what you need */
I really hope that it works, it's probably more easy solution then this one.
Mimi
-
How can I change this request, so I can display the name and partitions in a r
How can I change this request, so I can add the ID of the table SPRIDEN
from now on gives me what I want:
in a single line, but I would like to add the id and the name that is stored in the SPRIDEN table1,543 A05 24 A01 24 BAC 24 BAE 24 A02 20 BAM 20
SELECT sortest_pidm, max(decode(rn,1,sortest_tesc_code)) tesc_code1, max(decode(rn,1,score)) score1, max(decode(rn,2,sortest_tesc_code)) tesc_code2, max(decode(rn,2,score)) score2, max(decode(rn,3,sortest_tesc_code)) tesc_code3, max(decode(rn,3,score)) score3, max(decode(rn,4,sortest_tesc_code)) tesc_code4, max(decode(rn,4,score)) score4, max(decode(rn,5,sortest_tesc_code)) tesc_code5, max(decode(rn,5,score)) score5, max(decode(rn,6,sortest_tesc_code)) tesc_code6, max(decode(rn,6,score)) score6 FROM (select sortest_pidm, sortest_tesc_code, score, row_number() over (partition by sortest_pidm order by score desc) rn FROM (select sortest_pidm, sortest_tesc_code, max(sortest_test_score) score from sortest,SPRIDEN where SPRIDEN_pidm =SORTEST_PIDM AND sortest_tesc_code in ('A01','BAE','A02','BAM','A05','BAC') and sortest_pidm is not null GROUP BY sortest_pidm, sortest_tesc_code)) GROUP BY sortest_pidm;
Hello
That depends on whether spriden_pidm is unique, and you want to get the results.
Whenever you have a problem, post a small example of data (CREATE TABLE and INSERT, relevamnt columns only instructions) for all the tables and the results desired from these data.
If you can illustrate your problem using tables commonly available (such as in the diagrams of scott or HR) so you need not display the sample data; right after the results you want.
Whatever it is, explain how you get these results from these data.
Always tell what version of Oracle you are using.Looks like you are doing something similar to the following.
Using the tables emp and dept of the scott schema, producing a line of production by Department showing the highest salary for each job, for a set given jobs:DEPTNO DNAME LOC JOB_1 SAL_1 JOB_2 SAL_2 JOB_3 SAL_3 ------ -------------- ------------- ------- ----- ------- ----- ------- ----- 20 RESEARCH DALLAS ANALYST 3000 MANAGER 2975 CLERK 1100 10 ACCOUNTING NEW YORK MANAGER 2450 CLERK 1300 30 SALES CHICAGO MANAGER 2850 CLERK 950
On each line, jobs are listed in order by the highest salary.
This seems to be similar to what you are doing. The roles played by the sortest_pidm, sortest_tesc_code and sortest_test_score in your table sortest are played by deptno, job and sal in the emp table. The roles played by the spriden_pidm, id and the name of your table spriden are played by deptno, dname and loc in the dept table.Looks like you already have something like the query below, which produces a correct output, except that it does not include the dname and loc of the dept table columns.
SELECT deptno , MAX (DECODE (rn, 1, job)) AS job_1 , MAX (DECODE (rn, 1, max_sal)) AS sal_1 , MAX (DECODE (rn, 2, job)) AS job_2 , MAX (DECODE (rn, 2, max_sal)) AS sal_2 , MAX (DECODE (rn, 3, job)) AS job_3 , MAX (DECODE (rn, 3, max_sal)) AS sal_3 FROM ( SELECT deptno , job , max_sal , ROW_NUMBER () OVER ( PARTITION BY deptno ORDER BY max_sal DESC ) AS rn FROM ( SELECT e.deptno , e.job , MAX (e.sal) AS max_sal FROM scott.emp e , scott.dept d WHERE e.deptno = d.deptno AND e.job IN ('ANALYST', 'CLERK', 'MANAGER') GROUP BY e.deptno , e.job ) ) GROUP BY deptno ;
Dept.DeptNo is unique, it won't be a dname and a loc for each deptno, so we can modify the query by replacing "deptno" with "deptno, dname, loc" throughout the query (except in the join condition, of course):
SELECT deptno, dname, loc -- Changed , MAX (DECODE (rn, 1, job)) AS job_1 , MAX (DECODE (rn, 1, max_sal)) AS sal_1 , MAX (DECODE (rn, 2, job)) AS job_2 , MAX (DECODE (rn, 2, max_sal)) AS sal_2 , MAX (DECODE (rn, 3, job)) AS job_3 , MAX (DECODE (rn, 3, max_sal)) AS sal_3 FROM ( SELECT deptno, dname, loc -- Changed , job , max_sal , ROW_NUMBER () OVER ( PARTITION BY deptno -- , dname, loc -- Changed ORDER BY max_sal DESC ) AS rn FROM ( SELECT e.deptno, d.dname, d.loc -- Changed , e.job , MAX (e.sal) AS max_sal FROM scott.emp e , scott.dept d WHERE e.deptno = d.deptno AND e.job IN ('ANALYST', 'CLERK', 'MANAGER') GROUP BY e.deptno, d.dname, d.loc -- Changed , e.job ) ) GROUP BY deptno, dname, loc -- Changed ;
In fact, you can continue to use just deptno in the analytical PARTITION BY clause. It may be slightly more efficient to just use deptno, as I did above, but it won't change the results if you use all 3, if there is only 1 danme and 1 loc by deptno.
Moreover, you don't need so many subqueries. You use the internal subquery to calculate the MAX and the outer subquery to calculate rn. Analytical functions are calculated after global fucntions so you can do both in the same auxiliary request like this:
SELECT deptno, dname, loc , MAX (DECODE (rn, 1, job)) AS job_1 , MAX (DECODE (rn, 1, max_sal)) AS sal_1 , MAX (DECODE (rn, 2, job)) AS job_2 , MAX (DECODE (rn, 2, max_sal)) AS sal_2 , MAX (DECODE (rn, 3, job)) AS job_3 , MAX (DECODE (rn, 3, max_sal)) AS sal_3 FROM ( SELECT e.deptno, d.dname, d.loc , e.job , MAX (e.sal) AS max_sal , ROW_NUMBER () OVER ( PARTITION BY e.deptno ORDER BY MAX (sal) DESC ) AS rn FROM scott.emp e , scott.dept d WHERE e.deptno = d.deptno AND e.job IN ('ANALYST', 'CLERK', 'MANAGER') GROUP BY e.deptno, d.dname, d.loc , e.job ) GROUP BY deptno, dname, loc ;
It will work in Oracle 8.1 or more. In Oracle 11, however, it is better to use the SELECT... Function PIVOT.
Maybe you are looking for
-
Addons Manager opens by itself (without be clicked; unwanted)
Often, I'll return to my office and find the Add-ons Manager is opened by itself and replaced one of my tabs. Because it replaces a site that updates frequently, the content, I have previously display is generally lost and hard to find. Very frustrat
-
Skype logging does not after the update: no connection?
Same question but Skype update itself and after that do so won't save as it says no connection... and Yes, I have a great connection... sigh... This message was transferred from its previous location to create his own new topic here; his subject and/
-
updates show only not in programs
HelloI have problems with the media center and media player on Vista Home premium. His research as corrupt files somewhwere along the line so I want to remove SP1 and reinstall it.Problem is that when I go to programs of the Control Panel, it shows n
-
Problem of lights keyboard Acer vn7 - 571 g (Nitro)
When you start Windows keyboard backlight is on. Although I previously turned off it with fn + f9. Shouldn't it do not forget to State?
-
NAT Type for PS3 (wrt610N) question
I have the wrt610N cable Internet and Modern Warefare 2 play on the PS3. Since day 1, I had a Type 2 Nat type depending on the internet connection of the PS3 test. Modern Warfare 2 includes a Nat Type indicator on the lobby screen and mine said alwa