id subject points Rank
joe maths 70 1
Mike maths 60 2
Sarah maths 40 3
mike English 80 1
Sarah English 65 2
joe English 55 3
Sarah Chemistry 80 1
Mike Chemistry 60 2
joe Chemistry 43 3
I was able to query this in mysql but i want to store the rank column in the table (grades) using the ALTER command
i tried
ALTER TABLE grades
ADD COLUMN Rank
GENERATE ALWAYS AS
DENSE_RANK() OVER(PARTITION BY subjects ORDER BY points DESC)
ORDER BY id,
Rank
but did not work
Hope i could find some help out there
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
Suppose I have a table like this:
name position zipcode
-------------------------
Gary Gm 12345
Rob VP 54321
John Manager 1234
After 20 minutes some rows were added in the source and now the table looks like
name position zipcode
------------------------
Gary Gm 12345
Rob VP 54321
John Manager 1234
Chris Director 5478
Kane VP 9999
So Kane and Chris were added after 20 minutes in the source, and there is no date time field to identify on which day the rows were added, I just wanted to know if there are any functions to identify the newly added rows in the table in SQL Server 2014?
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 create an Employee Utilization report in SSRS (Visual Studio 2010, SQL Server 2012)
We have employees who work for multiple program managers. I need a report that shows program managers, the employees who work for them, and the utilization of each employee by each manager they work for. This will allow us to identify which employees do not have their utilization distributed correctly.
The dataset query returns data such as:
EmpID Emp Mgr Util Total Util
1234 Doe, John Lundy, Sal 100 100
2345 Ward, Joe Lundy, Sal 40 110
3456 Kline, Rob Smith, Bob 100 100
4567 Abbott, Fred Smith Bob 100 100
2345 Ward, Joe Smith, Bob 70 110
The results, when grouped by manager, should look like this (with the plus indicating the expand toggle):
Mgr Emp UtilMgr Util Total Util
Lundy, Sal
+Doe, John 100
+Ward, Joe 110
Smith, Bob
+Kline, Rob 100
+Abbott, Fred 100
+Ward, Joe 110
When an employee is expanded, the detail will show the employee utilization broken down by the different program managers they do work for:
Mgr Emp UtilMgr Util Total Util
Lundy, Sal
-Doe, John 100
Lundy, Sal 100
-Ward, Joe 110
Lundy, Sal 40
Smith, Bob 70
Smith, Bob
+Kline, Rob 100
+Abbott, Fred 100
+Ward, Joe 110
I have tried all kinds of grouping and property combinations (parents, children, display detail) in the report, but cannot get the results to be grouped the way I want it. I tried adding another manager field (aliased as UtilMgr) to the query, but still couldn't get the report correct.
I'm looking for the best way to handle this. It seems like the solution is to have the Program Manager as a parent to the Employee, and then have a second group where the Employee is a parent to the Manager, but I'm not sure how to implement that.
Do I need to do something different with the query? Do I need to use a subreport or a second matrix, nested in the first? Is there some way to create overlapping parent/child groups?
I have only been working with SSRS for about 2 weeks, so I hope all the terminology I've used is clear and correct. Thanks in advance for any help!
Can you change your query?
In order to do what you want, your dataset needs to look like this:
EmpID Emp Mgr UtilMgr Util Total Util
1234 Doe, John Lundy, Sal Lundy, Sal 100 100
2345 Ward, Joe Lundy, Sal Lundy, Sal 40 110
2345 Ward, Joe Lundy, Sal Smith, Bob 70 110
3456 Kline, Rob Smith, Bob Smith, Bob 100 100
4567 Abbott, Fred Smith, Bob Smith, Bob 100 100
2345 Ward, Joe Smith, Bob Smith, Bob 70 110
2345 Ward, Joe Smith, Bob Lundy, Sal 40 110