Subject RE: Possible to use FIRST 1 inside a group by?
Author Maya Opperman

>I’ve got quite a tricky SQL query (well, at least for me it is, I’m hoping someone may have done this before)

 

>Here’s a simplified example of what I’m trying to do:

 

>I have a table with

 

ID            GroupID               FKCode                 Value

1              1                              ABC                        +5

2              1                              XYZ                         -5

3              2                              ABC                        +8

4              2                              XYZ                         -8

5              3                              ABC                        -2

6              3                              XYZ                         +2

 

>I’d like to select the first record of each group, eg.

 

ID            GroupID               FKCode                 Value

1              1                              ABC                        +5

3              2                              ABC                        +8

5              3                              ABC                        -2

 

I think I just solved my own problem.

 

Might not be the most efficient method, but this works:

 

select ID, GroupID, FKCode, Value

from MyTable T

where T.ID = (select first 1 T2.ID from MyTable T2 where T.GroupID = T2.GroupID order by T2.ID)