I've a distinct query as like below for three columns. My requirement is that I need a count of distinct of these three columns.
select distinct empid, empname, salary from employee
This is the following query used to getting the count of the table in normal case, but I needed that distinct count, how can I make a query ?
select count(empid) from employee
select count(*) from
(
select distinct empid, empname, salary from employee
) x
if you don't want to use a sub select just use group by
Select Count(*) FROM employee GROUP BY empid, empname, salary
Related
I have employee table with emp id (emp_id) and department (dep_id) fields. An employee could be working in more than one Department. I want to write a sql query to display unique emp_ids who work in more than one department.
Pl help me to write sql query.
Thx
Answered here: SQL query for finding records where count > 1
You need to use count, group by and having like this.
select emp_id, count(dep_id)
from employee_department
group by emp_id
having count(dep_id)>1
Query
SELECT COUNT(*)
FROM
(
SELECT id_employee, COUNT(*) AS CNT
FROM Department_Employee
GROUP BY id_employee
) AS T
WHERE CNT > 1
I have a table that is called: customers.
I'm trying to get the name and the salary of the people who have the maximum salary.
So I have tried this:
SELECT name, salary AS MaxSalary
FROM CUSTOMERS
GROUP BY salary
HAVING salary = max(salary)
Unfortunately, I got this error:
Column 'CUSTOMERS.name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I know I should add the name column to the group by clause, but I get all the records of the table.
I know that I can do it by:
SELECT name, salary
FROM CUSTOMERS
WHERE salary = (SELECT MAX(salary) FROM CUSTOMERS)
But I want to achieve it by group by and having clauses.
This requirement isn't really suited for a group by and having solution. The easiest way to do so, assuming you're using a modern-insh version of MS SQL Server, is to use the rank window function:
SELECT name, salary
FROM (SELECT name, salary, RANK() OVER (ORDER BY salary DESC) rk
FROM customers) c
WHERE rk = 1
Mureinik's answer is good with rank, but if you didn't want a windowed function for whatever reason, you can just use a CTE or a subquery.
with mxs as (
select
max(salary) max_salary
from
customers
)
select
name
,salary
from
customers cst
join mxs on mxs.max_salary = cst.salary
There was no need to use group by and having clause there, you know. But if you want to use them then query should be
SELECT name, salary
FROM CUSTOMERS
GROUP BY salary
having salary = (select max(salary) from CUSTOMERS)
Say I have a subquery like
select deptNo, count(*)
from employee group by deptNo
and I want use just count(*) in my main query, how can I do that?
eg: Queries like count(*) in (select deptNo, count(*) from employee group by deptNo);
Or what is the alternative to do it??
select avg(cnt)
from (
select deptNo, count(*) as cnt
from employee group by deptNo) src
select t.count1 from
(select deptNo,count(*) as count1 from employee group by deptNo) as t
Try to have an alias name for the count in the inner query.I assume you need only the column count1 here.(I have named the column as count1)
I've retrieved a count of the duplicates and their occurrences using the below code
select empID, count(empID) AS DUPLICATEempID
from employees
group by empID
having count (empID) > 1
I now want the table to include the number of rows returned (i.e. insert the number on the table returned)
thanks in advance.
In SAS, you can do this with a subquery:
select empId, DUPLICateempID, count(*) as NumDuplicates
from (select empID, count(empID) AS DUPLICATEempID
from employees
group by empID
having count (empID) > 1
) t
When you have an aggregation function without a group by, it applies the function to the whole table and re-merges the results.
Given a list containing City, EmpName and Salary, sorted by city and EmpName, how to output each EmpName and Salary with the total Salary per City?
Here is what I have got:
select EmpName, sum(Salary) from table group by province;
But it gives me error as I have not included the EmpName in the group by clause and/or am not performing any aggregation on it. How can i achieve the desired results? Any help?
If, what you want, is the sum of the salary in the city for each employee, then you have two options. The first should work in almost any database:
select EmpName, tcity.CitySalary
from t join
(select City, sum(Salary) as CitySalary
from t
group by city
) tcity
on tcity.city = t.city
The second way is to use a window function. Notably, this doesn't work on mysql:
select EmptName, sum(salary) over (partition by city)
from t
SELECT t.City, t.EmpName, t.Salary, x.city_salary
FROM table t,
(SELECT City, SUM(Salary) as city_salary
FROM table
GROUP BY City) x
WHERE x.city = t.city;