Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 way to find duplicates with x fields

Author  Topic 

tech_1
Posting Yak Master

129 Posts

Posted - 2011-10-17 : 11:39:39
Hi.
I believe to know how to get a hold of duplicates.

my scenario is that there will be 2 tables joined together (inner).
using SQL Server 2008

I want to find the duplicates, for a report done from a client end, of records with my where clause criteria.

But because I will be wanting to display the field values, it means that there will be around 8 or more (it will be more overtime) fields that I will have to place in my SELECT statement.

I also understand that I have to reflect the fields on my SELECT statement to my group by clause.

is there a better way to do this or find duplicates?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-17 : 12:33:12
there's better way by using a derived table. but to give more detailed suggestion, can you post some sample data to indicate how duplicates are existing?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tech_1
Posting Yak Master

129 Posts

Posted - 2011-10-18 : 05:40:43
Sure except, i dont know how it will really be like. but an example of what I believe:

Objective: find devices with the same Identifier

there is a table where "Attributes" are stored. These attributes contain a form of a key/value presence. Example: Key would be "Identifier" and value would be...the value


ID: 1
Key: "Identifier"
Value: "0001"
DeviceID (FK): 1


ID: 2
Key: "Identifier"
Value: "0001"
DeviceID (FK): 6

ID: 3
Key: "Identifier"
Value: "0002"
DeviceID (FK): 4

ID: 4
Key: "Identifier"
Value: "0002"
DeviceID (FK): 8

ID: 5
Key: "Identifier"
Value: "0062"
DeviceID (FK): 54



so as you can see, there are duplicates there for the key "Identifier" and the value of 0002 and 0001. I want to get these results

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-18 : 05:42:38
so what should be output you want out of the above data?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sqlmaster555112
Starting Member

13 Posts

Posted - 2011-10-21 : 09:47:27
Suppose you have two tables one named employee table and other named job table in employee you have fields as empid ,name ,address,[phone no]
in job table you have empid,jobid,salary
suppose you wanna find is the empids with same salary
then this can be done by first
(select empid,salary from employee a inner join job b on a.empid=b.empid
group by empid,salary)

this query will provide us with unique combinations of empid and salary Right.

Now what we want is to find the empids that are present in the employee table but not in the above table So

(Select a.empid from employee a inner join
(select empid,salary from employee a inner join job b on a.empid=b.empid
group by empid,salary) as b on a.salary=b.salary
)

If you try to put this into your data this may solve the issue if not please let me know....Thanks


@sqlmaster

http://www.intelcs.com/SQL_Server_Consultant.aspx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-21 : 09:56:10
quote:
Originally posted by sqlmaster555112

Suppose you have two tables one named employee table and other named job table in employee you have fields as empid ,name ,address,[phone no]
in job table you have empid,jobid,salary
suppose you wanna find is the empids with same salary
then this can be done by first
(select empid,salary from employee a inner join job b on a.empid=b.empid
group by empid,salary)

this query will provide us with unique combinations of empid and salary Right.

Now what we want is to find the empids that are present in the employee table but not in the above table So

(Select a.empid from employee a inner join
(select empid,salary from employee a inner join job b on a.empid=b.empid
group by empid,salary) as b on a.salary=b.salary
)

If you try to put this into your data this may solve the issue if not please let me know....Thanks


@sqlmaster

http://www.intelcs.com/SQL_Server_Consultant.aspx


sorry didnt understand how this will give employees with same salary?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-21 : 09:58:09
quote:
Originally posted by tech_1

Sure except, i dont know how it will really be like. but an example of what I believe:

Objective: find devices with the same Identifier

there is a table where "Attributes" are stored. These attributes contain a form of a key/value presence. Example: Key would be "Identifier" and value would be...the value


ID: 1
Key: "Identifier"
Value: "0001"
DeviceID (FK): 1


ID: 2
Key: "Identifier"
Value: "0001"
DeviceID (FK): 6

ID: 3
Key: "Identifier"
Value: "0002"
DeviceID (FK): 4

ID: 4
Key: "Identifier"
Value: "0002"
DeviceID (FK): 8

ID: 5
Key: "Identifier"
Value: "0062"
DeviceID (FK): 54



so as you can see, there are duplicates there for the key "Identifier" and the value of 0002 and 0001. I want to get these results




seems like

SELECT t.*
FROM tbl t
inner join (select key,value
from tbl
group by key,value
having count(*) >1)t1
on t1.key = t.key
and t1.value = t.value


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -