Mediator
Mediator

Reputation: 15378

How to do distinct by first column

Select DISTINCT wpg.ID as id1,wr.ID as id2
FROM Table1 wpg
inner join Table2 wp ON wp.WpgId = wpg.ID
inner join Table3 wr ON wr.WpId = wp.ID

I need wpg.Id distinct how do this?

I need from:

1 2

2 3

1 4

get:

1 2

2 3

Upvotes: 0

Views: 77

Answers (2)

podiluska
podiluska

Reputation: 51494

select wpg.ID, min(wr.ID)
FROM Table1 wpg 
inner join Table2 wp ON wp.WpgId = wpg.ID 
inner join Table3 wr ON wr.WpId = wp.ID 
group by wpg.ID

Upvotes: 2

Christoffer Lette
Christoffer Lette

Reputation: 14816

The answer depends on what you want to do with the second column. I'm assuming you want the smallest value:

select
    wpg.ID as id1,
    min(wr.ID) as id2
from
    Table1 wpg
    inner join Table2 wp on wp.WpgId = wpg.ID
    inner join Table3 wr on wr.WpId = wp.ID
group by
    wpg.ID

Upvotes: 3

Related Questions