Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 21st, 2012, 10:00 PM
miguelosaurio miguelosaurio is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 7 miguelosaurio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 39 sec
Reputation Power: 0
How to display the result from 2 queries?

Hello, first of all thanks for opening this thread and trying to help me, I am trying to display the results from 2 queries, one is suposed to display the count of the employees, per department, who win over the average of the entire company and the other one is suposed to display the count of the employees, per department, who win under the average of the entire company.

I used a UNION ALL, but all it does is merge the results from the ones that win over and under the average into one row, is there a way to separate them?

I tried assigning names to each salary using AS but it only displays the first I put in.

If anyone could please help me I would apretiate it very much, thanks in advance, I will paste the code bellow.


sql Code:
Original - sql Code
  1. (
  2. SELECT DE.DEPARTMENT_NAME, COUNT (EM.EMPLOYEE_ID) AS MAYORES
  3. FROM DEPARTMENTS DE, EMPLOYEES EM
  4. WHERE DE.DEPARTMENT_ID = EM.DEPARTMENT_ID
  5. AND EM.SALARY > (SELECT AVG(EM.SALARY) FROM EMPLOYEES EM)
  6. GROUP BY DE.DEPARTMENT_NAME
  7. )
  8. UNION ALL
  9. (
  10. SELECT DE.DEPARTMENT_NAME, COUNT (EM.EMPLOYEE_ID) AS MENORES
  11. FROM DEPARTMENTS DE, EMPLOYEES EM
  12. WHERE DE.DEPARTMENT_ID = EM.DEPARTMENT_ID
  13. AND EM.SALARY < (SELECT AVG(EM.SALARY) FROM EMPLOYEES EM)
  14. GROUP BY DE.DEPARTMENT_NAME
  15. )
  16. ORDER BY DEPARTMENT_NAME;

Reply With Quote
  #2  
Old October 26th, 2012, 02:05 PM
LKBrwn_DBA's Avatar
LKBrwn_DBA LKBrwn_DBA is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 751 LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 2 h 56 m 5 sec
Reputation Power: 348
Wink

Quote:
Originally Posted by miguelosaurio
. . . Etc ...
If anyone could please help me I would apretiate it very much, thanks in advance, I will paste the code bellow.

Two ways:
1)
sql Code:
Original - sql Code
  1. SELECT   'Mayores:' emp_type, de.department_name, COUNT (em.employee_id) AS num_emp
  2.     FROM departments de, employees em
  3.    WHERE de.department_id = em.department_id
  4.      AND em.salary > (SELECT AVG (em.salary)
  5.                         FROM employees em)
  6. GROUP BY de.department_name
  7. UNION ALL
  8. SELECT   'Menores:', de.department_name, COUNT (em.employee_id)
  9.     FROM departments de, employees em
  10.    WHERE de.department_id = em.department_id
  11.      AND em.salary < (SELECT AVG (em.salary)
  12.                         FROM employees em)
  13. GROUP BY de.department_name
  14. ORDER BY department_name;

2)
sql Code:
Original - sql Code
  1.  
  2. SELECT de.department_name
  3.      , SUM (CASE WHEN em.salary > s.avg_sal THEN 1 ELSE 0 END) AS mayores
  4.      , SUM (CASE WHEN em.salary < s.avg_sal THEN 1 ELSE 0 END) AS menores
  5.     FROM departments de
  6.        , employees em
  7.        , (SELECT AVG (em.salary) avg_sal
  8.             FROM employees em) s
  9.    WHERE de.department_id = em.department_id
  10. GROUP BY de.department_name
  11. ORDER BY department_name;

Comments on this post
miguelosaurio agrees!
__________________

Last edited by LKBrwn_DBA : October 26th, 2012 at 02:21 PM.

Reply With Quote
  #3  
Old October 26th, 2012, 02:17 PM
LKBrwn_DBA's Avatar
LKBrwn_DBA LKBrwn_DBA is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 751 LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 2 h 56 m 5 sec
Reputation Power: 348
Cool

And yet another:
sql Code:
Original - sql Code
  1. SELECT   department_name, MAX (mayores) mayores, MAX (menores) menores
  2.     FROM (SELECT   de.department_name, COUNT (em.employee_id) AS mayores, 0 AS menores
  3.               FROM departments de, employees em
  4.              WHERE de.department_id = em.department_id
  5.                AND em.salary > (SELECT AVG (em.salary)
  6.                                   FROM employees em)
  7.           GROUP BY de.department_name
  8.           UNION ALL
  9.           SELECT   de.department_name, 0, COUNT (em.employee_id)
  10.               FROM departments de, employees em
  11.              WHERE de.department_id = em.department_id
  12.                AND em.salary < (SELECT AVG (em.salary)
  13.                                   FROM employees em)
  14.           GROUP BY de.department_name)
  15. GROUP BY department_name
  16. ORDER BY department_name;

Comments on this post
miguelosaurio agrees!

Last edited by LKBrwn_DBA : October 26th, 2012 at 02:20 PM.

Reply With Quote
  #4  
Old October 28th, 2012, 04:55 PM
miguelosaurio miguelosaurio is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 7 miguelosaurio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 39 sec
Reputation Power: 0
Thanks to the both of you all three solutions worked wonderfully and it actually helped me to solve another problem that I was encountering that I had no clue how to solve.

Thanks again.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > How to display the result from 2 queries?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap