1

我想更新一个表的一列,如下所示:

TB_任务:

cod_1   cod_2   cod_3   Description  Value      Date
-----------------------------------------------------------------
123     181     6401    product A   7526836    19/02/2013
123     181     6401    product B   4638607    18/02/2013
123     181     6401    product C   9633450    06/02/2013
789     314     1361    product 1   6218035    15/02/2013
789     314     1361    product 2   6189032    14/02/2013

结果应该是:

cod_1   cod_2   cod_3   Description   Value      Date
-----------------------------------------------------------------
123     181     6401    product A     7526836    19/02/2013
123     181     6401    product A     4638607    18/02/2013
123     181     6401    product A     9633450    06/02/2013
789     314     1361    product 1     6218035    15/02/2013
789     314     1361    product 1     6189032    14/02/2013

有人知道吗?

提前致谢。

4

3 回答 3

2

您没有明确说明如何选择值。这是一种方法:

with toupdate (
     select t.*, min(description) over (partition by cod_1) as minDescription
     from t
    )
update toupdate
    set Description = minDescription

它将描述设置为由 定义的每个组的最小值cod_1。这符合您问题中的数据。

如果你想要最新的描述,我会用一个子查询来做到这一点:

【上传代码问题】

update t
    set description = 
        (select top 1 description
         XXX
         where t2.cod_1 = t.cod_1
         order by date desc)

(现在,在 XXX 所在的子查询中的select和之间添加一个“from t t2” 。)where

于 2013-02-19T15:22:30.247 回答
1
    select cod_1, cod_2,   cod_3, max(Date) as date
    From TB_Quest
    group by cod_1, cod_2,   cod_3

使用 cod_1、cod_2、cod_3、日期将其加入 TB_Quest 以获取描述并更新表再次加入与表的选择,但仅使用 cod_1、cod_2、cod_3

于 2013-02-19T15:24:32.183 回答
0

试试这个

   update TB_Quest set Description =
      if (Description= 'product 2' , 'product 1' , 
      if(Description= 'product 1' , 'product 1',
      if (Description = 'product A' , 'product A' , 'product A' ))) 

在这里演示 SQLFIDDLE

于 2013-02-19T15:32:24.850 回答