2

我有一个表 OD_REFERENCE(OD_COL_1 到 5)

REFERENCE 有 5 列,其中 OD_COL_4 等于订单号(例如编号“12345”),OD_COL_5 包含订单的详细信息:

ROW  OD_COL_4  OD_COL_5 

1     12345       11

2     12345       11

3     12345         <---no number 

4     12345       99

5     12346       99

6     12347       99

一些订单号有空白(无值),需要手动更新为该订单列的最高号。

查找带有空白详细信息的订单号

SELECT DISTINCT OD_COL_4 FROM OD_REFERENCE

WHERE OD_COL_5 = ' '

然后我将列中的订单号复制到记事本并运行查询:

SELECT DISTINCT OD_COL_5 FROM OD_REFERENCE

WHERE OD_COL_4 = '--and i paste the order number in here' 

然后我手动检查每个订单并将所有列中的值更新为该列中的最大值。所以对于订单 12345,我将所有字段设置为 99。

有没有我可以使用的脚本来获取所有带有空白详细信息的订单号,并使用该列中包含的最高值(一次全部)更正它们?

4

3 回答 3

1

这应该在 SQL Server 中工作:

update a
    set OD_COL_5 = b.MaxOD_COL_5
from orders a
inner join(
            select OD_COL_4, max(OD_COL_5) as MaxOD_COL_5
            from orders
            where coalesce(OD_COL_5, '') <> ''
            group by OD_COL_4
          ) b
on a.OD_COL_4 = b.OD_COL_4  
where coalesce(OD_COL_5, '') = ''
于 2012-10-11T17:23:46.373 回答
0
update od_reference oref
set od_col_5 = (
    select max(od_col_5)
    from od_reference
    where od_col_4 = oref.od_col_4
    ) s
where od_col_5 = ' '
于 2012-10-11T17:04:50.430 回答
0

我认为这可能是您所追求的,这应该在 SqlServer Update o Set o.od_col5 = n.highvalue From od_reference o Join (select distinct t1.od_col4, (select max(t2.od_col5) from od_reference t2 where t2. od_col4 = t1.od_col4) as highvalue From od_reference t1 Where t1.od_col5 = '') as n On o.od_col4 = n.odcol4 where o.od_col5 = ''

于 2012-10-11T17:30:49.833 回答