select * from table1 where id in (select min(id) from table1 group groupColumn)
select * from a where id in ( select min(id) from a group by XX having YY)
select * from table where id in (select min(id),group from table group by group )
impossible in t-sql, all columns that are not grouping columns must use aggregate function.the alternative way depends on your table structure, if your table have a primary key column named "id", and it is of int type. and assume you group by column bb then select * from table where id in (select min(id) from table group by bb)
min(id)? 是否应该为:....select min(col2) from table1 group by group
select * from table where id in (select min(col2) from table1 group by group)
select identity(int,1,1) as id, group, col2 into #tmp from table select group col2 from #tmp where id in (select min(id) from #tmp group by group)
这样呢? select * from table where cast(col2 as nvarchar)+id in (select min(cast(col2 as nvarchar)+id) from table1 group by group)
select [group],min(col2) from table_name group by [group]
what's wrong with my method, just use temp table.select identity(int,1,1) as id, group, col2 into #tmp from table select group, col2 from #tmp where id in (select min(id) from #tmp group by group)
1 1 33
2 1 38
3 2 34
4 2 32
5 2 34
6 2 38
7 2 32
8 3 34
9 3 38
如何只取1,2,3组的第一条id为1,3,8?
(select min(id) from table1 group groupColumn)
group by group
)
if your table have a primary key column named "id", and it is of int type.
and assume you group by column bb
then
select * from table where id in (select min(id) from table group by bb)
1 33
1 38
2 34
2 32
2 34
2 38
2 32
3 34
3 38
如何只取1,2,3组的第一条?
id是我加的,如果没有id呢?请教icyer
是否应该为:....select min(col2) from table1 group by group
select group col2 from #tmp where id in (select min(id) from #tmp group by group)
select * from table where cast(col2 as nvarchar)+id in (select min(cast(col2 as nvarchar)+id) from table1 group by group)
group by [group]
select group, col2 from #tmp where id in (select min(id) from #tmp group by group)