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.
| 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 2008I 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 Identifierthere 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 valueID: 1Key: "Identifier"Value: "0001"DeviceID (FK): 1ID: 2Key: "Identifier"Value: "0001"DeviceID (FK): 6ID: 3Key: "Identifier"Value: "0002"DeviceID (FK): 4ID: 4Key: "Identifier"Value: "0002"DeviceID (FK): 8ID: 5Key: "Identifier"Value: "0062"DeviceID (FK): 54so 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 |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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,salarysuppose you wanna find is the empids with same salarythen this can be done by first(select empid,salary from employee a inner join job b on a.empid=b.empidgroup 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.empidgroup 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@sqlmasterhttp://www.intelcs.com/SQL_Server_Consultant.aspx |
 |
|
|
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,salarysuppose you wanna find is the empids with same salarythen this can be done by first(select empid,salary from employee a inner join job b on a.empid=b.empidgroup 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.empidgroup 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@sqlmasterhttp://www.intelcs.com/SQL_Server_Consultant.aspx
sorry didnt understand how this will give employees with same salary?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 Identifierthere 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 valueID: 1Key: "Identifier"Value: "0001"DeviceID (FK): 1ID: 2Key: "Identifier"Value: "0001"DeviceID (FK): 6ID: 3Key: "Identifier"Value: "0002"DeviceID (FK): 4ID: 4Key: "Identifier"Value: "0002"DeviceID (FK): 8ID: 5Key: "Identifier"Value: "0062"DeviceID (FK): 54so 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 likeSELECT t.*FROM tbl t inner join (select key,value from tbl group by key,value having count(*) >1)t1on t1.key = t.keyand t1.value = t.value ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|