Oracle多重更新查询

时间:2010-06-22 05:29:56

标签: sql oracle plsql

我的数据库中有两个如下表所示的表。在evry DEPARTMENT_CODE的第一个表ITEM中,将有多个ITEM_CODE。

ITEM
----------------------------------------------------
"STORE_CODE" "ITEM_CODE" "DEPARTMENT_CODE"
"011"         "912003" "14"
"011"         "912004" "14"
"011"         "914001" "14"
----------------------------------------------------

COMPETITOR
--------------------------------------------------------------
"STORE_CODE"  "ITEM_CODE" "DEPARTMENT_CODE" "COMPETITOR_CODE"

"011"     "912003"     "14"       "01"
"011"     "912003"     "14"       "02"
"011"     "912003"     "14"       "03"
"011"     "912004"     "14"       "01"
"011"     "912004"     "14"       "02"
"011"     "912004"     "14"       "04"
"011"     "914001"     "14"       "01"
"011"     "914001"     "14"       "02"
"011"     "914001"     "14"       "03"
-------------------------------------------------------------

在表COMPETITOR中,evey ITEMCODE将有三个条目,并且具有不同的competitor_Code

我有三个值Comp_1,comp_2,comp_3和department_code = 14;

我要做的是使用comp_1,comp_2,comp_3更新COMPETITOR表,用于evry Item_code,在ITEM表中将department_code设为14

示例输出

COMPETITOR
--------------------------------------------------------------
"STORE_CODE"  "ITEM_CODE" "DEPARTMENT_CODE" "COMPETITOR_CODE"

"011"     "912003"     "14"       "Comp_1"
"011"     "912003"     "14"       "Comp_2"
"011"     "912003"     "14"       "Comp_3"
"011"     "912004"     "14"       "Comp_1"
"011"     "912004"     "14"       "Comp_2"
"011"     "912004"     "14"       "Comp_3"
"011"     "914001"     "14"       "Comp_1"
"011"     "914001"     "14"       "Comp_2"
"011"     "914001"     "14"       "Comp_3"
-------------------------------------------------------------

如何为此编写单个oracle查询?

1 个答案:

答案 0 :(得分:2)

以下假设ITEM_CODE仅分配给一个DEPARTMENT_CODE,因为它很简单并且您没有向我们提供任何进一步的业务规则。这种假设是错误的,你需要相应地调整逻辑。

我还将您的要求纳入COMPETITOT.COMPETITOR_CODE的现有价值不可靠。

鉴于此测试日期:

SQL> select * from competitor
  2  /

STORE_CODE  ITEM_CODE DEPARTMENT_CODE COMPETITOR
---------- ---------- --------------- ----------
        11     912003              14 01
        11     912003              14 04
        11     912003              14 03
        11     912004              14 01
        11     912004              14 02
        11     912004              14 04
        11     914001              14 01
        11     914001              14 02
        11     914001              14 05

9 rows selected.

SQL>

我们可以使用分析ROW_NUMBER()为COMPETITOR_CODE生成必要的句柄:

SQL> update competitor c
  2  set competitor_code =
  3          (select decode (dr
  4                         , 1, 'Comp_1'
  5                         , 2, 'Comp_2'
  6                         , 3, 'Comp_3')
  7             from ( select row_number() over ( partition by x.item_code
  8                                                  order by x.rowid ) as dr
  9                           , x.rowid as row_id
 10                    from competitor x
 11                    where x.item_code in ( select item_code
 12                                           from item
 13                                           where  department_code = 14 ) ) l
 14             where c.rowid = l.rowid )
 15  /

9 rows updated.

SQL>

这是理想的结果(除非进一步增加业务规则):

SQL> select * from competitor
  2  /

STORE_CODE  ITEM_CODE DEPARTMENT_CODE COMPETITOR
---------- ---------- --------------- ----------
        11     912003              14 Comp_1
        11     912003              14 Comp_2
        11     912003              14 Comp_3
        11     912004              14 Comp_1
        11     912004              14 Comp_2
        11     912004              14 Comp_3
        11     914001              14 Comp_1
        11     914001              14 Comp_2
        11     914001              14 Comp_3

9 rows selected.

SQL>