How to find duplicate record for same date - sql

I have a room inventory table each room has one record for each day,
but some rooms have double record for a day. I want to query to pull out those id.
Inventory_table => id, roomid, inv_date....

The following may get you a list of the rooms with duplicates.
select id, roomid, inv_date, count(room)
from room_inventory
group by id, roomid, inv_date
having count(room) > 1;

Select Id, count(*) from inventory_table group by roomid, inv_date having count(*)>1

select empno,count() from emp group by empno having count()>1;
its with reapted values
select * from emp where rowid not iN(select min(rowid) from emp group by deptno)

Related

Oracle query: how do I limit the returned records to only those having a count > 1 but show full results?

I need to show all the users who have more than one ID but not return the users who do. I tried group by having but I need to list the IDs and not just count them so could not get that to work for me. I ended up with using a the code below but it returns all the records.
select id,fname,lname,ssn,dob
count(id) over partition by fname,lname,ssn,dob) as cnt
from TABLE
order by cnt desc;
Use a subquery:
select id, fname, lname, ssn, dob
from (select id, fname, lname, ssn, dob,
count(id) over (partition by fname, lname, ssn, dob) as cnt
from TABLE
) t
where cnt >= 2
order by cnt;
WITH CTE (FNAME, LNAME, TALLY) AS
(
SELECT FNAME, LNAME, COUNT(ID) AS TALLY
FROM TABLE
HAVING COUNT(ID) > 1
)
SELECT T.ID, C.FNAME,C.LNAME FROM CTE C
JOIN TABLE T
ON C.FNAME = T.FNAME
AND C.LNAME = T.LNAME

single row based on min of one column with a join

I am trying to get the certain columns from a row associated with min(date) by joining with another table.
I am using this in Hive.
My query is:
With temp1 as
(Select employee_id as emp_id, min(date) as min_date from employee where employee_id in (select employee_id from employee_sal) group by employee_id)
Select
employee_sal.dept_code,
Temp1.emp_id,
employee_sal.dept_name,
employee_sal.paid_loc,
employee_sal.country_paid,
min_date
from employee_sal inner join temp1 on employee_sal.employee_id = emp1.employee_id
But I need the dept_name, paid_loc, country_paid based on the min(date) based on employee table like this:
employee_sal.dept_code, Temp1.emp_id, temp1.dept_name, temp1.paid_loc, temp1.country_paid, min_date
These should be the values associated with the min(date) from employee.
With temp1 as
(Select employee_id as emp_id, min(date) as min_date from employee where employee_id in (select employee_id from employee_sal) group by employee_id)
Select
employee_sal.dept_code,
Temp1.emp_id,
employee_sal.dept_name,
employee_sal.paid_loc,
employee_sal.country_paid,
min_date
from employee_sal inner join temp1 on employee_sal.employee_id = emp1.employee_id And employee_sal.min_date = emp1.min_date

SQL oracle: Need to display a query.

so i have 3 tables linked together named office, employee, and dependent.
office: Oid (PK), officeName
employee: EID(PK), Fname, Lname, JobTitle, Salary, DOH, Gender, DOB, OID(FK1), Supervisor(FK2)
Dependent: DID(PK), Fname, Lname, Gender, EID(FK1)
Here is the link to the picture of the tables:
http://classweb2.mccombs.utexas.edu/mis325/class/hw/hw12a.jpg
I need to display concatenated name and EID of 5 employees with the largest number of dependents, if there is a tie for the five largest, then I need to display all of the tying employees.
I am confused on how to begin. please help :)
thank you in advance
Just break down the problem:
How many dependents an EID has:
SELECT EID, COUNT(*) AS C
FROM Dependent
GROUP BY EID
Add a rank
SELECT EID, C, RANK() OVER (ORDER BY C DESC)
FROM (
SELECT EID, COUNT(*) AS C
FROM Dependent
GROUP BY EID
) S
We want the first 5
SELECT EID
FROM (
SELECT EID, C, RANK() OVER (ORDER BY C DESC) AS R
FROM (
SELECT EID, COUNT(*) AS C
FROM Dependent
GROUP BY EID
) S
) S2
WHERE R <= 5
Now ask for what you want:
SELECT * -- or whatever
FROM Employee
WHERE EID IN (
SELECT EID
FROM (
SELECT EID, C, RANK() OVER (ORDER BY C DESC) AS R
FROM (
SELECT EID, COUNT(*) AS C
FROM Dependent
GROUP BY EID
) S
) S2
WHERE R <=5
) S3
I suggest you run each step and make sure it gives you the expected results.
Ummm I would try something like this:
Select TOP 5
a.FNAME,
a.LNAME,
a.EID,
Count(b.EID) as Dependents
FROM employee a
LEFT JOIN dependent b on a.EID = b.EID
group by 1,2,3 order by Dependents desc

SELECT Employees with the top three salaries for the last two years

There are tables Employees
CREATE TABLE Employees
(
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
name nvarchar(100) NOT NULL,
depID int NOT NULL,
salary money NOT NULL,
FOREIGN KEY (depID) REFERENCES Departments(id)
);
and Payments
CREATE TABLE Payments
(
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
userID int NOT NULL,
createdDate date DEFAULT GETDATE(),
sum money NOT NULL,
FOREIGN KEY (userID) REFERENCES Employees(id)
);
I need to get names of Employees with the top three salaries for the last two years.
I tried to use the query below, but it doesn't work and I got an error.
SELECT TOP 3 name
FROM Employees
WHERE id in (SELECT id, SUM(sum) as SumTotal FROM Payments
WHERE (createdDate BETWEEN '2015-09-01' AND '2013-09-01')
ORDER BY SumTotal);
Error message:
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
How to make it work?
This is one way to do it using ctes.
Demo
with pymt as
(
SELECT userid, sum(sum) as sumtotal
FROM Payments
WHERE createdDate BETWEEN '2013-09-01' AND '2015-09-01'
group by userid
)
, ename as
(
select e.name, pymt.sumtotal, row_number() over(order by pymt.sumtotal desc) as rn
from pymt join employees e
on pymt.userid = e.id
)
select name
from ename
where rn < = 3;
People are way over-complicating things. I'm pretty sure this will get you what you want:
SELECT TOP 3 employees.id, Name,
Sum([sum]) AS [TotalPayments]
FROM Employees
inner join Payments on employees.id = payments.userid
WHERE createdDate BETWEEN '2013-09-01' and '2015-09-01'
Group By employees.id, Name
Order BY TotalPayments DESC
SqlFiddle to test it
If you want just the names column, you could wrap that query with another select:
select Name from (
SELECT TOP 3 employees.id, Name,
Sum([sum]) AS [TotalPayments]
FROM Employees
inner join Payments on employees.id = payments.userid
WHERE createdDate BETWEEN '2013-09-01' and '2015-09-01'
Group By employees.id, Name
Order BY TotalPayments DESC
) q
Try this:-
SELECT userID, SUM(sum) as SumTotal INTO #temp
FROM Payments
WHERE (createdDate BETWEEN '2015-09-01' AND '2013-09-01')
group by userID
ORDER BY SumTotal
SELECT TOP 3 name
FROM Employees e join #temp
on e.id = #temp.userID
select top 3
emp.id as id,
emp.name as name,
sum(pay.sum) as total
from employees emp
join payments pay
on emp.id = pay.user_id
--where pay.createdDate BETWEEN '2015-09-01' AND '2013-09-01'
where pay.createdDate BETWEEN '2013-09-01' AND '2015-09-01'
group by emp.id, emp.name
This should work
You have to do what the error message tell you to do, put the TOP where you have your order by :
SELECT name
FROM Employees
WHERE id in (SELECT TOP 3 id, SUM(sum) as SumTotal FROM Payments
WHERE (createdDate BETWEEN '2015-09-01' AND '2013-09-01')
ORDER BY SumTotal)
[EDIT]
If we follow error message, this should be ok:
SELECT name
FROM Employees
WHERE id in ( Select x.userId From (SELECT TOP 3 userId, SUM([sum]) as SumTotal FROM Payments
WHERE (createdDate BETWEEN '2015-09-01' AND '2013-09-01')
Group By userId
ORDER BY SumTotal) x);
SELECT TOP 3 Name,
Sum(sum) AS sum
FROM Employees
WHERE createdDate BETWEEN '2015-09-01' AND '2013-09-01'
Group By Name
Order BY 2 DESC

SQL Syntax on DISTINCT Query

I have an Employee Table with their DeptCode. I want list of distinct DeptCode and their first created date in the Employee Table. This will also tell which employee was first entered for a specific dept in the Employee Table.
I used:
SELECT DISTINCT DEPTCODE,
CREATEDDATE
FROM EMPLOYEE
The Date Return is incorrect.
Any specific syntax to handle this issue.
Try:
SELECT DEPTCODE,
Min(CREATEDDATE)
FROM EMPLOYEE
GROUP BY DEPTCODE
If you want the department codes, earliest creation date, and the name of the employee, then I would recommend window functions:
select deptcode, name, createddate
from (select e.*,
row_number() over (partition by deptcode order by createddate) as seqnum
from employee e
) e
where seqnum = 1;
You can use GROUP BY and MIN to achieve this.
SELECT DEPTCODE, MIN(CREATEDDATE)
from EMPLOYEE
GROUP BY DEPTCODE
Something like this.
SELECT deptcode,
employee_name,
minddate
FROM employee
JOIN (SELECT deptcode,
Min(createddate) mindate
FROM employee
GROUP BY deptcode) temp
ON employee.deptcode = temp.deptcode
AND createddate = mindate