当前位置: 代码迷 >> Sql Server >> DISTINCT有关问题
  详细解决方案

DISTINCT有关问题

热度:29   发布时间:2016-04-27 13:08:45.0
DISTINCT问题
有tb1表
code name
0001 1
0002 2
0001 1
NULL 100
0002 2
0003 3

想得到TB2表
code name
0001 1
0002 2
0003 3

用insert into tb2 (code,name) select DISTINCT code,name from tb1不行,
用insert into tb2 code select DISTINCT code from tb1就可以,但只有一列!


------解决方案--------------------
SQL code
insert into tb2 (code,name) select code,min(name) from tb1 where code is not null or upper(code)!='NULL'group by code
------解决方案--------------------
insert into tb2 (code,name)
select code,min(name) from tb1 where code is not null group by code,name
  相关解决方案