合并怎么取舍? 空的字段用第二条的去代替,非空的呢? 是否合并规则是这样的?name,area 相同的合并,其他字段保留第一条非空的记录?
解决方案 »
- 没想到MSSQL2008如此脆弱!
- 索引建立方法
- 使用"字符串函数"查询名称的第三位不是A的名称
- 为什么创建了两个或两个以上的惟一约束,约束就不起作用了?
- 如何删除指定的行
- 紧急求救?
- 这样的SQL语句怎么写,一个表有多个字段对应另一个表中的同一字段?
- 一个数字型的字段,保存数据时,如果没有数据,我在程序里把他设置为了-1,得到dataset帮定datagrid时却显示了-1,这时本应该为空才对,
- vfp----如何调整窗体的大小!!!!!!
- 求救!怎样提高VB6和SQL Server 2000处理jpg扫描图的速度?
- 从xml文件读取数据和从数据库中读出数据,哪种方式好一点?哪种速度快一点?
- 小鸟问题,在字符串前加N是代表什么意思,有何作用
也就是说,有可能一个id 是1, 另一个id是10
需要先找出 name,area相同的 然后再合并不知道我解释清楚了么?
,birthday=(select top 1 birthday from tb where name=a.name and area=a.area and isnull(birthday,'')<>'')
,sex=(select top 1 sex from tb where name=a.name and area=a.area and isnull(sex,'')<>'')
,email=(select top 1 email from tb where name=a.name and area=a.area and isnull(email,'')<>'')
,Memo=(select top 1 Memo from tb where name=a.name and area=a.area and isnull(Memo,'')<>'')
from(
select id=min(id),name,area
from tb
group by name,area
)a
from tb
group by name,erea
还有更优化的方法么? 如果不以第一条为准,以记录中birthday 日期大的为准呢?
如:
id name area birthday sex email memo
1 张三 北京 1980-01-01 自由人
2 张三 北京 1981-01-01 男 自由
10 张三 北京 1979-01-01 男 [email protected] 自由2结果:
id name area birthday sex email memo
1 张三 北京 1981-01-01 男 [email protected] 自由
where not exists(
select * from tb where name=a.name and area=a.area and birthday>a.birthday)
--或者是这样
select a.*
from tb a,(
select birthday=max(birthday),name,area
from tb group by name,area
)b where a.name=b.name and a.area=b.area and a.birthday=b.birthday
select a.* from tb a
where not exists(
select * from tb
where name=a.name and area=a.area
and(birthday>a.birthday or birthday=a.birthday and id>a.id))--或者是这样
select a.*
from tb a,(
select id=max(id) --假设id为主键
from tb a,(
select birthday=max(birthday),name,area
from tb group by name,area
)b where a.name=b.name and a.area=b.area and a.birthday=b.birthday
group by a.name,a.area,a.birthday
)b where a.id=b.id