如有一个datatable,其中有一名为date的列,
date
200801
200802
200802
200803
200804
200805
如果不用对表进行for循环操作,如果相同的记录,只取其一条。我想查询的结果为 200801 200802 200803 200804 200805
请赐教!
date
200801
200802
200802
200803
200804
200805
如果不用对表进行for循环操作,如果相同的记录,只取其一条。我想查询的结果为 200801 200802 200803 200804 200805
请赐教!
单列?select distinct [date] from tablename
多列?
declare @t table([id] int identity(1,1),[date] varchar(6))
insert @t select '200801'
union all select '200802'
union all select '200802'
union all select '200803'
union all select '200804'
union all select '200805'
select *
from @t t
where not exists(select 1 from @t where t.[date]=[date] and t.[id]>[id])
/*(所影响的行数为 6 行)id date
----------- ------
1 200801
2 200802
4 200803
5 200804
6 200805(所影响的行数为 5 行)
*/
回答3楼的朋友,不是对数据库进行操作,是对datatable进行操作。