Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
Hello,

I have two tables named description and price with same duplicate ids and model.

Description table:
ID  DESCR MODEL
  1  tyre   A
  2  tyre   A
  1  tube   A
  1  bumper A
  2  pipe   A

Price table:
ID Price MODEL
  1   54    A
  1   23    A
  1   35    A
  2   55    A
  2   35    A

I want to join like this:

ID  DESCR Price MODEL
  1   tyre  54   A
  1   tube  23   A
  1   bumper 35  A
  2   tyre   55  A
  2   pipe   35  A

I tried with inner join but several combinations of duplicates comes as a result. Is there any way to handle this?

Thanks in advance.
Posted

Try this:
SQL
select d.id, d.descr, p.price, d.model from
(
select id,  descr, model,
ROW_NUMBER() OVER(PARTITION BY id ORDER BY descr DESC) row
from description
) d
inner join
(select id,  price,
ROW_NUMBER() OVER(PARTITION BY id ORDER BY id) row
from price
 ) p
 on d.row=p.row where d.id=p.id
 
Share this answer
 
Comments
priyae 19-Mar-15 6:08am    
Thank you very much Peter:)
I tried your code and got the expected solution.
It Helped me a lot.

Regards,
priya
Hello

I see an issue with the definition of the primary key.

I recommend you to take a look at this:
http://en.wikipedia.org/wiki/Unique_key[^]

I recommend you to define a single field Primary key with autoincrement (if you decide to use a numeric field), and if you want to constraint some kind of field combination, I recommend you to use Unique Key constraint (additional to Primary Key).

When you define the primary or unique key, you will be able to reference the table on another table.

I'd like to see your Data Dictionary to see how you define and use this fields and how you constraint them.

If you still have any other question, please fell free to ask.

Best Regards.
Aner
 
Share this answer
 
Comments
priyae 19-Mar-15 6:08am    
Hello Aner,

Thank you very much for the information.

How could I define a primary key / unique for the ID column that have duplicates.

If I define Id column as primary key, duplicates are not allowed.
But I don't want that to happen.

So i did not add keys:(

Is there any way to index?

Regards,
priya.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900