Question : SQL problem with duplicates

Hi,

I have an SQL query I'm running in Access 2007 that is giving me duplicates of records in table t, when I want table t to only show 1 record per unique value.

It looks like table bn has multiple instances of each record, but when the two are joined, I'm selecting only records where the two fields (bn.Linked Transaction number and t.Interaction Number) match - does anyone know what the issue is with my SQL? Is this a place to use DISTINCT on bn.linked transaction number?

Would someone mind helping me with the syntax of that SQL? I will have mulitple bn.service ticket numbers, but each should be linking to a different t.interaction number record via the JOIN to bn.Linked Tranaction Number

So it will be ok to have:

bn.service ticket number                   t.interaction number
1                                                         8
1                                                        12
2                                                        14
2                                                        15

...but not:

bn.service ticket number                   t.interaction number
1                                                         8
1                                                         8
2                                                        14
2                                                        14

Thanks

Bill
Code Snippet:
1:
2:
SELECT bn.[Service Ticket Number], bn.[Created Date], bn.[Changed Date], bn.[Closed Date], bn.[Created Week], bn.[Created Year], bn.[Linked Transaction Number], bn.[Customer Country], bn.[Serial Number], bn.[Product Line], bn.[Product Name], bn.[Product Number], bn.[Operating System], bn.Reason, bn.Description, bn.Status, bn.[Warranty Category], bn.[Changed by (Agent ID/Name)], bn.Field39, bn.Field40, t.[Created Time], t.[Created by (Agent ID/Name)], t.Field5, t.[Resp Serv Org]
FROM bn LEFT JOIN t ON bn.[Linked Transaction Number] = t.[Interaction Number];
Open in New Window Select All

Answer : SQL problem with duplicates


You have many columns in your SELECT clause. If you have two records with [Serive Ticket Number] = 1 and [Interaction Number] = 8.
Which record you want to pick. If you want the max values of all other columns, you can GROUP BY these two columns.
1:
2:
3:
4:
5:
6:
7:
SELECT bn.[Service Ticket Number], MAX(bn.[Created Date]), MAX(bn.[Changed Date]), MAX(bn.[Closed Date]), MAX(bn.[Created Week]), 
       MAX(bn.[Created Year]), MAX(bn.[Linked Transaction Number]), MAX(bn.[Customer Country]), MAX(bn.[Serial Number]), 
       MAX(bn.[Product Line]), MAX(bn.[Product Name]), MAX(bn.[Product Number]), MAX(bn.[Operating System]), MAX(bn.Reason), 
       MAX(bn.Description), bn.Status, MAX(bn.[Warranty Category]), MAX(bn.[Changed by (Agent ID/Name)]),MAX(bn.Field39), 
       MAX(bn.Field40), MAX(t.[Created Time]), MAX(t.[Created by (Agent ID/Name)]), MAX(t.Field5), MAX(t.[Resp Serv Org])
FROM bn LEFT JOIN t ON bn.[Linked Transaction Number] = t.[Interaction Number]
GROUP BY bn.[Service Ticket Number],t.[Interaction Number]
Open in New Window Select All
Random Solutions  
 
programming4us programming4us