怎么取所有分组后的每组的第一行?
select * from table1 where id in
(select min(id) from table1 group groupColumn)
这样呢?
select * from table where cast(col2 as nvarchar)+id in (select min(cast(col2 as nvarchar)+id) from table1 group by group)
加一个id就都搞定了,为什么还想那么复杂的语句
加一个辅助列也不是什么大不了的,不影响库结构
我是没有什么好办法了。
因为不允许循环,只能再建立一个相同结构的表,给新表加一个插入记录的触发器来控制记录的插入,不仅仅可以解决你的问题。
whats 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)
还是用临时表吧。
可以把分组的数据插入到一张带有identity的临时表中。