I am trying to create a report that basically pulls all data from a sql table. The problem is this table was used for registration purposes and there are cases where the same employee registered for two different events and so there are multiple entries for them in the table. I really don't need to know which events they registered for, just that they did register. Is there any way to modify my query so that only one record per employee is selected? I tried to use DISTINCT but that still pulled every single record.
- Can you show us the table structure?Tim Biegeleisen– Tim Biegeleisen2016-08-25 16:13:03 +00:00Commented Aug 25, 2016 at 16:13
- this is the query I am running now it excludes true and false answers to disclaimers and the eventID they signed up for SELECT EmpID, ENAME, Department, ContactNumber, Email, CASE UPR00100_VW.EMPLOYMENTTYPE WHEN 1 THEN 'Full Time Regular' WHEN 2 THEN 'Full Time Temp' WHEN 3 THEN 'Part Time Regular' WHEN 4 THEN 'Part Time Temp' WHEN 5 THEN 'Intern' WHEN 6 THEN 'Other' END AS Employmenttype FROM [EventDisclaimers] Left outer JOIN dbo.UPR00100_VW ON dbo.EventDisclaimers.EmpID = dbo.UPR00100_VW.EMPLOYID order by EmpID ascKAL077– KAL0772016-08-25 16:39:22 +00:00Commented Aug 25, 2016 at 16:39
4 Answers
The explanation for what you are seeing most likely is that the records you are getting back are not duplicates. For example, if you used SELECT *, then you would be getting back all columns, and even though the employee name, id, etc. columns could be identical for multiple records, the other columns might be distinct.
Without knowing your table structure, I cannot give an exact answer. But here are two ways to deal with this. First, you can use SELECT DISTINCT with only columns corresponding to the employee, e.g.
SELECT DISTINCT firstName, lastName, id FROM yourTable This has the disadvantage that it throws away information from all the other columns. If you want to retain those other columns, then a second option would be to group records by employee, and then use aggregate functions on the other columns, e.g.
SELECT firstName, lastName, id, MIN(signupTime) FROM yourTable GROUP BY firstName, lastName, id In this query, I assumed there is a timestamp column called signupTime, which recorded the time for each employee signup (including possible duplicate signups). This query would retain the first signup time, along with the employee id and name.
Comments
you should grouping them based on fullname, employeeid or whatever... . I have mentioned a simple sample to show you how to use group by command in SQL queries :
SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name; I hope it works for you.