i have table as
Dept
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
and another table as
Emp
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 12/17/1980 800 NULL 20
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 NULL 20
7654 MARTIN SALESMAN 7698 9/28/1981 1250 1400 30
7698 BLAKE MANAGER 7839 5/1/1981 2850 NULL 30
7782 CLARK MANAGER 7839 6/9/1981 2450 NULL 10
7788 SCOTT ANALYST 7566 12/9/1982 3000 NULL 20
7839 KING PRESIDENT NULL 11/17/1981 5000 NULL 10
7844 TURNER SALESMAN 7698 9/8/1981 1500 0 30
7876 ADAMS CLERK 7788 1/12/1983 1100 NULL 20
7900 JAMES CLERK 7698 12/3/1981 950 NULL 30
7902 FORD ANALYST 7566 12/3/1981 3000 NULL 20
7934 MILLER CLERK 7782 1/23/1982 1300 NULL 10
My question : List ALL the department names and their employee count.
Count should include only those employees whose hire date is greater than 1981
Result: should be like this
DNAME EMPCOUNT
ACCOUNTING 1
OPERATIONS 0
RESEARCH 2
SALES 0
please help
SELECT DNAME,
count(emp.deptno) AS EMPCOUNT
FROM DEPT
LEFT JOIN emp ON DEPT.DEPTNO = emp.DEPTNO
WHERE YEAR(hiredate)>1981
GROUP BY emp.deptno
Related
I am having trouble with this question I can't seem to get the count correct
on each department and only select the highest one as well as excluding
"DALLAS"
THIS IS THE QUESTION
"Write a SQL statement to display the name and location of all departments
(except the departments located in Dallas) with the highest number of
employees.
You cannot use join operations in your SQL statement (e.g., … FROM department,
employee WHERE …, department INNER JOIN employee ON …)."
DEPARTMENT_ID DEPARTMENT_NAME LOCATION
------------- -------------------- --------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 IT DALLAS
50 EXECUTIVE NEW YORK
60 MARKETING CHICAGO
6 rows selected
EMPLOYEE_ID EMPLOYEE_NAME JOB_TITLE SUPERVISOR_ID HIRE_DATE SALARY COMMISSION DEPARTMENT_ID
----------- -------------------- -------------------------------------------------- ------------- --------- ---------- ---------- -------------
7839 KING PRESIDENT 20-NOV-01 5000 50
7596 JOST VICE PRESIDENT 7839 04-MAY-01 4500 50
7603 CLARK VICE PRESIDENT 7839 12-JUN-01 4000 50
7566 JONES CHIEF ACCOUNTANT 7596 05-APR-01 3000 10
7886 STEEL PUBLIC ACCOUNTANT 7566 08-MAR-03 2500 10
7610 WILSON BUSINESS ANALYST 7596 03-DEC-01 3000 20
7999 WOLFE TEST ANALYST 7610 15-FEB-02 2500 20
7944 LEE REPORTING ANALYST 7610 04-SEP-06 2400 20
7900 FISHER SALES EXECUTIVE 7603 06-DEC-01 3000 500 30
7921 JACKSON SALES REPRESENTATIVE 7900 25-FEB-05 2500 400 30
7952 LANCASTER SALES CONSULTANT 7900 06-DEC-06 2000 150 30
7910 SMITH DATABASE ADMINISTRATOR 7596 20-DEC-01 2900 40
7788 SCOTT PROGRAMMER 7910 15-JAN-03 2500 40
7876 ADAMS PROGRAMMER 7910 15-JAN-03 2000 40
7934 MILLER PROGRAMMER 7876 25-JAN-02 1000 40
8000 BREWSTER TBA 22-AUG-13 2500
8100 PHILLIPS TBA 7839 21-AUG-13 2800
7400 SMITH VICE PRESIDENT 7839 16-FEB-01 4300 50
7700 ANDRUS PUBLIC ACCOUNTANT 7566 18-FEB-02 2500 10
7601 SAMPSON PROGRAMMER 7910 09-JAN-01 2500 40
7588 DODSON TEST ANALYST 7610 02-AUG-08 2500 20
7888 SANDY SALES CONSULTANT 7900 05-AUG-04 2500 30
22 rows selected
SELECT DEPARTMENT_NAME,
location,
count(*)
FROM DEPARTMENT
WHERE department_id IN ( SELECT department_id
FROM department
WHERE UPPER(location) <> 'DALLAS'
)
group by department_NAME, location
ORDER BY location;
DEPARTMENT_NAME LOCATION COUNT(*)
-------------------- -------------------- ----------
MARKETING CHICAGO 1
SALES CHICAGO 1
ACCOUNTING NEW YORK 1
EXECUTIVE NEW YORK 1
you can try using sub-queries if you are limited in not using joins
SELECT *
FROM (SELECT d.department_name,
d.location,
(SELECT COUNT(employee_id)
FROM employee e
WHERE e.department_id = d.department_id) no_employees
FROM department d
WHERE d.location <> 'DALLAS'
) t
WHERE no_employees = (SELECT COUNT(employee_id)
FROM employee
WHERE department_id IN (SELECT DISTINCT department_id
FROM department
WHERE location <> 'DALLAS')
GROUP BY department_id
ORDER BY 1 DESC
LIMIT 1)
Result
department_name location no_employees
SALES CHICAGO 4
EXECUTIVE NEW YORK 4
Am trying to the find the department with maximum count and then retrieve the corresponding name and location without using joins
SELECT (SELECT DEPARTMENT_NAME, location
FROM DEPARTMENT
WHERE department_id = q.department_id) ,
q.ct countofdept
FROM
(SELECT count(*) ct, department_id
FROM EMPLOYEE
WHERE department_id in ( SELECT department_id
FROM department
WHERE UPPER(location) <> 'DALLAS'
)
GROUP BY department_id
ORDER BY ct desc
LIMIT 1) q
I have a table with values as follows(MySql):
SQL> SELECT deptno, ename FROM emp ORDER BY deptno, ename;
DEPTNO ENAME
------ ----------
10 CLARK
10 KING
10 MILLER
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH
30 ALLEN
30 BLAKE
30 JAMES
30 MARTIN
30 TURNER
30 WARD
14 rows selected.
but I need them in the following less convenient format:
DEPTNO ENAME
------ -----------------------------------------
10 CLARK, KING, MILLER
20 ADAMS, FORD, JONES, SCOTT, SMITH
30 ALLEN, BLAKE, JAMES, MARTIN, TURNER, WARD
Kindly help me to achiever above result/output.
Using group_concat
SELECT deptno, group_concat(ename order by ename) as ename
FROM emp
group by deptno
ORDER BY deptno;
I am trying to find an average of past records in the database based on a specific time frame (between 9 and 3 months ago) if there is no value recorded for a recent sale. the reason for this is recent sales on our website sometimes do not immediately collect commissions so i am needing to go back to historic records to find out what a commission rate estimate might be.
Commission rate is calculated as:
total_commission / gross_sales
It is only necessary to find out what an estimate would be if a recent sale has no "total_commission" recorded
here is what i have tried so far but i think this is wrong:
SELECT
cs.*
,SUM(cs2.gross_sales)
,SUM(cs2.total_commission)
FROM
(SELECT
sale_id
, date
, customer_code
, customer_country
, gross_sales
, total_commission
FROM customer_sale cs ) cs
LEFT JOIN customer_sale cs2
ON cs2.customer_code = cs.customer_code
AND cs2.customer_country = cs.customer_country
AND cs2.date > cs.date - interval 9 month
AND cs2.date < cs.date - interval 3 month
GROUP BY cs.sale_id
so that data would be structured as follows:
sale_id date customer_code customer_country gross_sales total_commission
1 2013-12-01 cust1 united states 10000 1500
2 2013-12-01 cust2 france 20000 3000
3 2013-12-01 cust3 united states 15000 2250
4 2013-12-01 cust4 france 14000 2100
5 2013-12-01 cust5 united states 13000 1950
6 2013-12-01 cust6 france 12000 1800
7 2014-04-02 cust1 united states 10000
8 2014-04-02 cust2 france 20000
9 2014-04-02 cust3 united states 15000
10 2014-04-02 cust4 france 14000
11 2014-04-02 cust5 united states 13000
12 2014-04-02 cust6 france 12000
so I would need to output results from the query similar to this: (based on sales between 9 and 3 months ago from the same customer_code in the same customer_country)
sale_id date customer_code customer_country gross_sales total_commission gross_sales_past total_commission_past
1 2013-12-01 cust1 united states 10000 1500
2 2013-12-01 cust2 france 20000 3000
3 2013-12-01 cust3 united states 15000 2250
4 2013-12-01 cust4 france 14000 2100
5 2013-12-01 cust5 united states 13000 1950
6 2013-12-01 cust6 france 12000 1800
7 2014-04-02 cust1 united states 10000 10000 1500
8 2014-04-02 cust2 france 20000 20000 3000
9 2014-04-02 cust3 united states 15000 15000 2250
10 2014-04-02 cust4 france 14000 14000 2100
11 2014-04-02 cust5 united states 13000 13000 1950
12 2014-04-02 cust6 france 12000 12000 1800
Your query looks mostly right, but I think your outer query needs to be GROUP BY cs.sale_id (assuming that sale_id is unique in the customer_sale table, and assuming that the date column is datatype DATE, DATETIME, or TIMESTAMP).
And I think you want to include a join predicate so that you match only match "past" rows to those rows where you don't have a total commission, e.g.
AND cs.total_commission IS NULL
And I don't think you really need an inline view.
Here's what I came up with:
SELECT cs.sale_id
, cs.date
, cs.customer_code
, cs.customer_country
, cs.gross_sales
, cs.total_commission
, SUM(ps.gross_sales) AS gross_sales_past
, SUM(ps.total_commission) AS total_commission_past
FROM customer_sale cs
LEFT
JOIN customer_sale ps
ON ps.customer_code = cs.customer_code
AND ps.customer_country = cs.customer_country
AND ps.date > cs.date - INTERVAL 9 MONTH
AND ps.date < cs.date - INTERVAL 3 MONTH
AND cs.total_commission IS NULL
GROUP
BY cs.sale_id
Appropriate indexes will likely improve performance of the query. Likely, the EXPLAIN output will show "Using temporary; Using filesort", and that can be expensive for large sets.
MySQL will likely be able to make use of a covering index for the JOIN:
... ON customer_sale (customer_code,customer_country,date,gross_sales,total_commission).
I have this table (named EMP) in MySQL:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ----- ------- ---- -------- -------- ------- ------
7369 SMITH CLERK 7902 13-JUN-83 800.00 20
7499 ALLEN SALESMAN 7698 15-AUG-83 1,600.00 300.00 30
7521 WARD SALESMAN 7698 26-MAR-84 1,250.00 500.00 30
7566 JONES MANAGER 7839 31-OCT-83 2,975.00 20
7654 MARTIN SALESMAN 7698 05-DEC-83 1,250.00 1,400.00 30
7698 BLAKE MANAGER 7839 11-JUL-84 2,850.00 30
7782 CLARK MANAGER 7839 14-MAY-84 2,450.00 10
7788 SCOTT ANALYST 7566 05-MAR-84 3,000.00 20
7839 KING PRESIDENT 09-JUL-84 5,000.00 10
7844 TURNER SALESMAN 7698 04-JUN-84 1,500.00 .00 30
7876 ADAMS CLERK 7788 04-JUN-84 1,100.00 20
7900 JAMES CLERK 7698 23-JUL-84 950.00 30
7902 FORD ANALYST 7566 05-DEC-83 3,000.00 20
7934 MILLER CLERK 7782 21-NOV-83 1,300.00 10
How can I write a Select query that returns the year (HIREDATE column) when there are most number of employees (rows) registered?
What about
SELECT YEAR(hiredate) FROM emp
GROUP BY YEAR(hiredate)
ORDER BY COUNT(*) DESC
LIMIT 1
?
What about:
SELECT COUNT(1), YEAR(HIREDATE) FROM EMP GROUP BY YEAR(HIREDATE);
TRY
SELECT count(*) as total FROM emp GROUP BY hiredate ORDER BY total DESC LIMIT 1;
Try this:
select year(hiredate), count(*) from emp
group by year(hiredate) order by 2 desc limit 1
Any help is greatly appreciated.
I have a table hospital:
Nurse + Year + No.Patients
A001 |2000 | 23
A001 |2001 | 30
A001 |2002 | 35
B001 |2000 | 12
B001 |2001 | 15
B001 |2002 | 45
C001 |2000 | 50
C002 |2001 | 59
C003 |2002 | 69
etc
What I am trying to do is work out which nurse
had the greatest increase of patients for the years 2000 - 2002.
Clearly B001 did as her patients increased from 12 to 45 and increase of 33
and what I am trying to produce is the result B001 | 33.
This is what I have so far:
select a.nurse,a.nopats from hospital as a
join
( select nurse,max(nopats)-min(nopats) as growth
from hospital where year between 2000 and 2002 group by nurse ) as s1
on a.nurse = s1.nurse and a.nopats = s1.growth
where year between 2000 and 2002;
but all I get returned is an empty set.
I think I need an overall max(nopats) after the join.
Any help here would be great.
Thanks!
Try this:
SELECT nurse, (max(nopats) - min(nopats)) AS growth
FROM hospital
WHERE year BETWEEN 2000 AND 2002
GROUP BY nurse
ORDER BY growth DESC
LIMIT 1;
Result: B001 | 33 due to LIMIT 1; just leave it away if you want more results.
SELECT nurse, MAX(nopats) - MIN(nopats) AS Growth
FROM hospital
WHERE year BETWEEN 2000 AND 2002
GROUP BY nurse
ORDER BY Growth
That should do it. Let me know if thats what you needed.