Select a distinct RowId based on series of trainnu

2019-08-29 06:17发布

I have a excel(2010) sheet with data like this:

trainnumber RowId
2           0
2           1
2           3
4           4
4           5
4           6

And I want a list like this:

RowId
0
4

Meaning the first RowId of each of the trainnumber series

I've tried build a SELECT statement, but in doesn't work.(I suck at SQL lol)

sSQL = "select " & _
              "RowId, " & _
              "(select top 1 trainnumber from @t t2 where t.RowId = t2.RowId order by RowId), " & _
              "from @t t " & _
              "group by RowId"

It tells me, that there's an Syntax Error in the subselect, so I tried the used the sheetname, but it gives me the same error:

sSQL = "select " & _
              "RowId, " & _
              "(select top 1 trainnumber from @[Conversion$] t2 where t.RowId = t2.RowId order by RowId), " & _
              "from @[Conversion$] t " & _
              "group by RowId"

1条回答
乱世女痞
2楼-- · 2019-08-29 06:53

If you want first for each , you can get the min:

Select min(rowID)
from youtable
group by trainer_number
;
查看更多
登录 后发表回答