1

我正在对 oracle 进行查询以更新表中的令牌以对标记的行进行一些工作。

我的问题是我想将每次更新的行数限制为特定的批量大小,也限制为按结果分组。

我很难用抽象的术语来解释,所以我将其具体化。

假设我有一个名为“staging”的表,其中包含以下字段:(Id、IdFile Grouping、Name、Address1、Address2、Address3、Country、Token)

我想要做的是创建一个查询来批量处理这些信息。

对于每个批次,我都会更新令牌,以便将这些记录标记为以后可以使用。我想要一个批次的文件,每个批次都有一个大小限制,所以我使用 rownum 来限制它。我的另一个限制是按批次分组。

假设我的批处理大小为 5,如果我有 3 行且只有 3 行填充了分组列,我必须创建一个只有 3 行的批处理。

这是我最大的烦恼,因为当我进行分组时,我得到的结果只有一批有和没有分组的结果。(如果我有一个分组,我必须查看所有行的命运地址是否相同并用它创建一个批次。)

你能帮我理解我必须如何创建这个查询吗?

我现在使用的是:

update schema.staging set
token = 'token4'
where id in ( select t.id
               from (
    select stage.id, stage.grouping 
    from (select idFile 
            from (select a.idFile
                  from schema.staging a
                  inner join schema.config c on c.id = a.idcfgpriority
                  where nvl2(a.token,0,1) = 1
                  group by a.idFile, c.order
                  order by c.order desc) files 
            where rownum = 1 ) priorityFile
          inner join schema.staging stage on stage.idFile = priorityFile.idFile
        where nvl2(stage.token,0,1) = 1 
        group by stage.idFile, stage.token, stage.id, grouping sets ( (stage.grouping),
                                                                          (stage.name, stage.Address1,stage.Address2,stage.Address3,stage.Country))
        order by stage.grouping
    ) t
where rownum <= 5 )

对于以下数据:

 Id IdFile  Grouping    Name    Address1    Address2    Address3    Country     Token
 ====================================================================================
 1  1       null        Name1   Address1    Address1    Address1    Country     null
 2  1       1           Name1   Address1    Address1    Address1    Country     null
 3  1       1           Name1   Address1    Address1    Address1    Country     null
 4  1       1           Name1   Address1    Address1    Address1    Country     null
 5  1       2           Name1   Address1    Address1    Address1    Country     null
 6  1       2           LALAL   XPTO        Address1    Address1    Country     null
 7  1       null        Name1   Address1    Address1    Address1    Country     null
 8  1       null        Name1   Address1    Address1    Address1    Country     null
 9  1       null        Name1   Address1    Address1    Address1    Country     null
 10 2       null        Name1   Address1    Address1    Address1    Country     null
 11 2       null        Name1   Address1    Address1    Address1    Country     null
 12 2       null        Name1   Address1    Address1    Address1    Country     null
 13 2       null        Name1   Address1    Address1    Address1    Country     null
 14 2       null        Name1   Address1    Address1    Address1    Country     null
 15 2       null        Name1   Address1    Address1    Address1    Country     null

我期待6批。

batch#1: 1, 7, 8, 9
batch#2: 2, 3, 4
batch#3: 5
batch#4: 6
batch#5: 10, 11, 12, 13, 14
batch#6: 15

Sooo ...有人有什么想法吗?

干杯

4

1 回答 1

1

我不确定是否完全了解所有详细要求,但根据您的示例数据和所需的输出,以下查询将起作用:

SELECT dense_rank() over(ORDER BY "IdFile",
                                  "Grouping" nulls FIRST,
                                  "Name" desc,
                                  "Address1",
                                  "Address2",
                                  "Address3",
                                  "Country",
                                  "Token",
                                  row_num) batch_num,
       q.*
  FROM (SELECT trunc((row_number()
                      over(partition BY "IdFile",
                           "Grouping",
                           "Name",
                           "Address1",
                           "Address2",
                           "Address3",
                           "Country",
                           "Token" ORDER BY "Id") - 1) / 5) row_num,
               s.*
          FROM sample s) q
 ORDER BY "Id";

结果:

| BATCH_NUM | ROW_NUM | ID | IDFILE | GROUPING |  NAME | ADDRESS1 | ADDRESS2 | ADDRESS3 | COUNTRY |  TOKEN |
|-----------|---------|----|--------|----------|-------|----------|----------|----------|---------|--------|
|         1 |       0 |  1 |      1 |   (null) | Name1 | Address1 | Address1 | Address1 | Country | (null) |
|         2 |       0 |  2 |      1 |        1 | Name1 | Address1 | Address1 | Address1 | Country | (null) |
|         2 |       0 |  3 |      1 |        1 | Name1 | Address1 | Address1 | Address1 | Country | (null) |
|         2 |       0 |  4 |      1 |        1 | Name1 | Address1 | Address1 | Address1 | Country | (null) |
|         3 |       0 |  5 |      1 |        2 | Name1 | Address1 | Address1 | Address1 | Country | (null) |
|         4 |       0 |  6 |      1 |        2 | LALAL |     XPTO | Address1 | Address1 | Country | (null) |
|         1 |       0 |  7 |      1 |   (null) | Name1 | Address1 | Address1 | Address1 | Country | (null) |
|         1 |       0 |  8 |      1 |   (null) | Name1 | Address1 | Address1 | Address1 | Country | (null) |
|         1 |       0 |  9 |      1 |   (null) | Name1 | Address1 | Address1 | Address1 | Country | (null) |
|         5 |       0 | 10 |      2 |   (null) | Name1 | Address1 | Address1 | Address1 | Country | (null) |
|         5 |       0 | 11 |      2 |   (null) | Name1 | Address1 | Address1 | Address1 | Country | (null) |
|         5 |       0 | 12 |      2 |   (null) | Name1 | Address1 | Address1 | Address1 | Country | (null) |
|         5 |       0 | 13 |      2 |   (null) | Name1 | Address1 | Address1 | Address1 | Country | (null) |
|         5 |       0 | 14 |      2 |   (null) | Name1 | Address1 | Address1 | Address1 | Country | (null) |
|         6 |       1 | 15 |      2 |   (null) | Name1 | Address1 | Address1 | Address1 | Country | (null) |

SQL 小提琴

我在订购方面玩了一点,以完全模拟您提供的批次编号。您可以安全地丢弃descnulls FIRST零件。让我知道查询是否解决了您的问题。

于 2014-02-25T08:00:57.500 回答