sql server从组中选择第一行
问题描述:
我有这样的桌子:
a b
1 23
1 2
1 7
2 9
2 11
我想从GROUP BY a"查询中选择第一行(顺序无关紧要),结果应该是
I want to select the first row(order does not matter) from a "GROUP BY a" query , the result should be
a b
1 23
2 9
我正在使用 SQL SERVER 2008 如何为此编写查询?
I am using SQL SERVER 2008 how to write the query for this?
答
如果按照您的指示,顺序无关紧要,任何 聚合函数 b
就足够了.
If as you indicated, order doesn't matter, any aggregate function on b
would be sufficient.
SELECT a, b = MIN(b)
FROM YourTable
GROUP BY
a