首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > PB >

执行sql解决方案

2012-03-05 
执行sqlupdate test set qty qty - 1 where (location,sku,valid_date)in (select location,sku,min(val

执行sql
update test set qty = qty - 1 where (location,sku,valid_date) 
in (select location,sku,min(valid_date) from test
where location = 'chuwei' and item_code = '5678' group by location,sku)
以上语句在pb中用下列方式执行 为什么第一条记录没有update
Execute Immediate :as_sql Using atran_ex

[解决办法]
你是想让相同的location,sku中,最小的valid_date那条记录中qty-1?
以下两种方法都行。

update test 
set qty = qty - 1 
from test t where valid_date = 
(select min(valid_date) from test where location = t.location and sku = t.sku)

update test 
set qty = qty - 1 
from test t where not exists 
(select 1 from test where location = t.location and sku = t.sku and valid_date < t.valid_date)

[解决办法]
不好意思,上面把条件漏了.

你是想让相同的location,sku中,最小的valid_date那条记录中qty-1?
以下两种方法都行。

update test 
set qty = qty - 1 
from test t where location = 'chuwei' and item_code = '5678' and 
valid_date = (select min(valid_date) from test where location = 'chuwei' and item_code = '5678' and location = t.location and sku = t.sku)

update test 
set qty = qty - 1 
from test t where location = 'chuwei' and item_code = '5678' and 
not exists (select 1 from test where location = 'chuwei' and item_code = '5678' and location = t.location and sku = t.sku and valid_date < t.valid_date)

[解决办法]
update test set qty = qty - 1 
where item_code = '5678' 
and valid_date=(
select min(valid_date) from test a
where a.location= location and a.sku=sku )

热点排行