3

I have a following table structure which can't be change.

enter image description here

I'm trying to join these table and want to avoid duplicate records as well.

 select p.ProductId,p.ProductName,inv.Details from Products p
inner join Inventory inv on(p.ProductId = inv.ProductId)

here is SqlFiddle.

7
  • 1
    Duplicated records can be removed with DISTINCT, but your rows aren't duplicated. So you'll need to change what you select so you can DISTINCT it. Commented Mar 11, 2014 at 12:15
  • 2
    What is your expected output?? Commented Mar 11, 2014 at 12:15
  • Please mention your expected output Commented Mar 11, 2014 at 12:16
  • @Gone: ProductId should be unique. Commented Mar 11, 2014 at 12:16
  • Do you want join?? As you can use subquery for it Commented Mar 11, 2014 at 12:19

4 Answers 4

4

From sqlserver 2008+ you can use cross apply.

With cross apply you can make a join inside the subselect as demonstrated here. With top 1 you get maximum 1 row from table Inventory. It will also be possible to add an 'order by' statement to the subselect. However that seems out of scope for your question.

select p.ProductId,p.ProductName,x.Details 
from Products p
cross apply
(SELECT top 1 inv.Details FROM Inventory inv WHERE p.ProductId = inv.ProductId) x
Sign up to request clarification or add additional context in comments.

7 Comments

from the table Inventory, it's ok if I get top 1 record. your both query still display duplicate productid. query should display unique productid no matter which detail it takes from inventory table.
@AshwiniVerma the bottom example should not show duplicate productid
please run your query in Sqlfiddle which I linked. It does show duplicate.
@AshwiniVerma you are right. I misunderstood the question. I changed my answer now. If you include data and expected result in your question next time it is easier to solve it.
@Philip added explanation, I hope you find it useful
|
1

You can use row_number function to remove duplicates

   WITH New_Inventory AS
    (
        SELECT Productid, Details,
        ROW_NUMBER() OVER (Partition by Productid ORDER BY details) AS RowNumber
        FROM Products
    ) 
    select p.ProductId,p.ProductName,inv.Details from Products p
    inner join New_Inventory inv on(p.ProductId = inv.ProductId)
    where RowNumber = 1

Comments

0
select p.ProductId,p.ProductName 
,(select top 1 inv.Details from  Inventory inv where (p.ProductId = inv.ProductId))Details
from Products p

or

select * from 
(
select p.ProductId,p.ProductName,inv.Details
,ROW_NUMBER()over(partition by p.productid order by p.productid)rn from Products p
inner join Inventory inv on(p.ProductId = inv.ProductId)
)t4 
where rn=1

Comments

0
with cte as 
(select p.ProductId,p.ProductName,inv.Details,
RoW_Number() OVER(Partition by  p.ProductId,p.ProductName,inv.Details
                  Order by p.ProductId,p.ProductName,inv.Details) rn
 from Products p
inner join Inventory inv on(p.ProductId = inv.ProductId))

select ProductId,ProductName,Details from cte where rn = 1

SQLFIDDLE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.