22,209
社区成员
发帖
与我相关
我的任务
分享
create table t_1(ID int, MID int, Date datetime)
insert t_1 select 1, NULL, '2007-1-21'
insert t_1 select 2, NULL, '2007-3-25'
insert t_1 select 3, NULL, '2007-3-26'
create table t_2(ID int, Date datetime)
insert t_2 select 1, '2007-1-22'
insert t_2 select 2, '2007-1-25'
insert t_2 select 3, '2007-1-29'
update a set mid=b.id
from t_1 a
left join t_2 b
on b.date=
(select max(date) from t_2 x
where date<=a.date
and not exists(select 1 from t_1 y
where id<a.id and date>=x.date
and not exists(select 1 from t_2 where date>x.date and date<=y.date)
)
)
--or
update a set mid=b.id
from t_1 a
left join t_2 b
on b.date=
(select max(date) from t_2 x
where date <=a.date
and (select count(*) from t_2 where date>=x.date and date<=a.date)=
(select count(*) from t_1 where id<a.id and date>=x.date)+1
)
drop table t_1,t_2
select a.*,cnt = (select count(*) from tb where group_id= a.group_id) from ta a
select a.*,isnull(b.cnt,0) from ta a,
left join (select group_id,count(*) from tb group by group_id) b
on a.group_id = b.group_id
select distinct b.sku_no, b.vend_no
into #sku
from part b, inv_qty a
where a.sku_no = b.sku_no
and a.inv_type = 300
create table #sku(sku_no int null, vend_no int null)
insert #sku(sku_no) select distinct sku_no from inv_qty where inv_type =300
exec('create index idx1 on #sku(sku_no)')
exec('update #sku set vend_no = b.vend_no from #sku a,part b where a.sku_no = b.sku_no')
delete from #sku where vend_no is null
create table #sku(sku_no int null, vend_no int null)
insert #sku(sku_no) select distinct sku_no from inv_qty where inv_type =300
create index idx1 on #sku(sku_no)
update #sku set vend_no = b.vend_no from #sku a,part b where a.sku_no = b.sku_no
delete from #sku where vend_no is null
update #orders
set cust_no = b.cust_no
from #orders a,Cur..history_header b
where a.order_no = b.order_no
and a.order_type = b.order_type
set rowcount 10000
select @@rowcount
while @@rowcount>0
update #orders
set cust_no = b.cust_no
from #orders a,Cur..history_header b
where a.order_no = b.order_no
and a.order_type = b.order_type
and a.cust_no is null
and b.cust_no is not null
set rowcount 0