Blablablaster
Blablablaster

Reputation: 3348

Select multiple top 1

I have this table with data:

   ID   |   Data   
--------+---------
    1   |   ONE
    2   |   ONE
    3   |   ONE
    5   |   TWO
    7   |   TWO
    10  |   TWO
    15  |   THREE
    14  |   THREE
    8   |   THREE

and I want to get this result

   ID   |   Data   
--------+---------
    1   |   ONE
    5   |   TWO
    15  |   THREE

so I want to collect only first record of each value in Data. Values ONE, TWO, THREE may exist in second table so I can get them merged using join. How can I do this?

Upvotes: 3

Views: 4032

Answers (1)

YetAnotherUser
YetAnotherUser

Reputation: 9356

Try this -

Select Min(ID), Data
From YourTableAfterAllJoinsYouWant
Group By Data

You won't need to join if your Data table already has column Data in it. Or else, you can replace the YourTableAfterAllJoinsYouWant with joined tables.

Upvotes: 8

Related Questions