Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I am doing a left join but the table I am joining has duplicate entries sometimes. I cannot delete these entries so I only want to pull one when there is a duplicate for example...... Table ATicket# | QTY1 | 22 | 4 3 | 5Table B Ticket# | Time1 | 5:002 | 5:002 | 5:003 | 6:00When I do a left join it should only be 3 rows and the total qty should be 11. Right now it is pulling the ticket 2 twice so the QTY is 13.
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts
Posted - 2010-07-27 : 13:42:07
Something like this.
declare @a table (Ticket# int, QTY int)insert @aselect 1 , 2union all select 2 , 4 union all select 3 , 5declare @b table (Ticket# int, [Time] varchar(10))insert @bselect 1 , '5:00'union all select 2 , '5:00'union all select 2 , '5:00'union all select 3 , '6:00'
select sum(a.QTY)from @a aleft join (select Ticket#,MAX([Time]) as [Time] from @b GROUP BY Ticket#) b on a.Ticket# = b.Ticket#