表如下:
a(单位标识) b(变更日期) c(区划)d(情况)
甲公司 2000-1-1 广州
甲公司 1999-1-1 深圳
乙公司 2000-1-1 广州
乙公司 2002-1-1 深圳
想写个脚本 在同一个公司的2条记录为一组 把 c=广州 b(日期)大 的那条记录的d中写入 ‘新’
在同一个公司的2条记录为一组 把 c=广州 b(日期)小 的那条记录的d中写入 ‘旧’
例如 甲公司 区划为广州的这条 写入 新 乙公司 区划为广州的这条 写入 旧
谁能帮忙给个脚本啊
a(单位标识) b(变更日期) c(区划)d(情况)
甲公司 2000-1-1 广州
甲公司 1999-1-1 深圳
乙公司 2000-1-1 广州
乙公司 2002-1-1 深圳
想写个脚本 在同一个公司的2条记录为一组 把 c=广州 b(日期)大 的那条记录的d中写入 ‘新’
在同一个公司的2条记录为一组 把 c=广州 b(日期)小 的那条记录的d中写入 ‘旧’
例如 甲公司 区划为广州的这条 写入 新 乙公司 区划为广州的这条 写入 旧
谁能帮忙给个脚本啊
解决方案 »
- 怎样把MSSQL SERVER2005数据导出到EXCEL表格中
- ntext replace 超8000
- 聚合查询
- 数据库的疑难问题!!!各位有兴趣吗!!!
- 请问怎么给自定义数据类型设置默认值?
- "select subString(dataA,"+beg+","+num+") as mem from databin where ver='"+newbbh+"'"错在什么地方
- 关于数据库设计的问题!
- 如何使变量成为表的字段?
- 各位哥哥!求救!昨晚给客户备份了个数据库还原时提示:数据库 ‘sl' 的媒体集有 2 个家族成员,但只提供了 1 个。必须提供所有的成员。
- 如何获得SQL server7.0 中数据库已经使用空间的尺寸?急!急!
- 这样表的设计以及显示层的显示如何实现?
- 如何从exec sp_who中取出我想要的记录,并kill,请大家帮忙!
set d =( case when b = (select max(b) from table where a = t.a and c= t.c) then '新' else '旧' end)
from table t
declare @t table(a char(10),b datetime,c char(4),d char(2))
insert @t select '甲公司','2000-1-1','广州',' '
insert @t select '甲公司','1999-1-1','深圳',''
insert @t select '乙公司','2000-1-1','广州',''
insert @t select '乙公司','2002-1-1','深圳',''update t
set d =(
case when b =(select max(b) from @t where a <> t.a and c= t.c)then '新 ' else '旧 ' end)
from @t t
select * from @t/*a b c d
---------- ------------------------------------------------------ ---- ----
甲公司 2000-01-01 00:00:00.000 广州 新
甲公司 1999-01-01 00:00:00.000 深圳 旧
乙公司 2000-01-01 00:00:00.000 广州 新
乙公司 2002-01-01 00:00:00.000 深圳 旧*/
insert @t select '甲公司','2000-1-1','广州',' '
insert @t select '甲公司','1999-1-1','深圳',''
insert @t select '乙公司','2000-1-1','广州',''
insert @t select '乙公司','2002-1-1','深圳',''update t
set d =(
case when b =(select max(b) from @t where c= t.c)then '新 ' else '旧 ' end)
from @t t
select * from @t/*a b c d
---------- ------------------------------------------------------ ---- ----
甲公司 2000-01-01 00:00:00.000 广州 新
甲公司 1999-01-01 00:00:00.000 深圳 旧
乙公司 2000-01-01 00:00:00.000 广州 新
乙公司 2002-01-01 00:00:00.000 深圳 新
*/