简体   繁体   English

SQL:需要为每个单个记录创建两个唯一记录

[英]SQL: Need to create two unique records for each single record

The simple question is how can you take a set of records with a PK and create exactly two records for each source with a slightly altered key for the duplicate? 一个简单的问题是,如何使用PK来获取一组记录,并为每个源创建两个完全相同的记录,而复制项的密钥却略有更改? In other words, I take 4000 records and produce 8000 records where 4000 are identical and the other 4000 have a slightly altered key. 换句话说,我记录了4000条记录,并产生了8000条记录,其中4000条相同,而其他4000条的键稍有改变。 I cannot do a union because this is essentially two selects (long story). 我无法进行合并,因为这实质上是两个选择(长话短说)。

The rest gets complicated, but maybe necessary to provide examples. 其余的变得很复杂,但也许有必要提供示例。

This is my original set (it contains over 4000 records) 这是我的原始集(包含4000多个记录)

dateGroup areaGroup itemID editionID
   1          1        1       1
   1          1        1       2
   1          2        1       1
   1          2        2       1

   2          1        1       1
   2          1        1       2
   2          2        1       1
   2          2        1       2

For each record I need to create a duplicate record ganging the areaGroups together under '0', then create a comma separated list of original areaGroups as a separate field. 对于每条记录,我需要创建一个重复的记录,将areaGroups组合在一起在“ 0”下,然后创建一个以逗号分隔的原始AreaGroups列表作为单独的字段。 (The "why" is some dumb programmer (me) made a mistake about 15 years ago.) I can renumber the editionIDs as necessary, but the original and duplicate record must have the same editionID (thus why a union wouldn't work). (“为什么”是一个愚蠢的程序员(我)在15年前犯了一个错误。)我可以根据需要对版本ID进行重新编号,但是原始记录和重复记录必须具有相同的版本ID(因此,联合将不起作用) 。 The PK remains the same as above (all fields) PK与上面相同(所有字段)

dateGroup areaGroup itemID editionID aGroups
   1          0        1       1        1
   1          0        1       2        1
   1          0        1       1        2    // Duplicate (EditionID)
   1          0        2       1        2
   2          0        1       1        1
   2          0        1       2        1
   2          0        1       1        2    // Duplicate (EditionID)
   2          0        1       2        2

The end result would renumber the editionID as needed to make the record unique. 最终结果将根据需要对版本ID重新编号,以使记录唯一。

dateGroup areaGroup itemID editionID aGroups  (EditionID is what is altered)
   1          0        1       1        1
   1          0        1       2        1
   1          0        1       2        2    1 changed to 2 (one more than row 1)
   1          0        2       1        2
   2          0        1       1        1
   2          0        1       2        1
   2          0        1       2        2    1 changed to 2 (one more than row 1)
   2          0        1       2        2

   1          1        1       1
   1          1        1       2
   1          2        1       2             1 changed to 2 (editionID) to match
   1          2        2       1

   2          1        1       1
   2          1        1       2
   2          2        1       2             1 changed to 2 to match above
   2          2        1       2

I know you could calculate the editionID like a row rank like so: 我知道您可以像这样计算rowID的版本ID:

select row_number() over ( 
       partition by dateGroup, itemID 
       order by dateGroup, itemID) as editionID

So all I need is to know how to duplicate the records from a single set 所以我所需要的就是知道如何从单个集合中复制记录

在派生表上进行交叉联接:(选择1作为aGroups并全部选择2)

I'd create a temporary table with duplicates and their count. 我将创建一个包含重复项及其计数的临时表。 Then I'd filter the original table to have only unique rows, and insert another row for each row in the temporary table, incrementing their editionID. 然后,我将原始表筛选为仅具有唯一的行,然后在临时表中为每行插入另一行,以增加其editionID。

In MySQL, I'd use user @variables; 在MySQL中,我将使用用户@variables; not sure about MS SQL. 不确定MS SQL。

Did you try UNION ALL instead of just UNION 您是否尝试了UNION ALL而不只是UNION

UDPATE perhaps I misunderstood the problem and I thought you were having a problem with the union loosing the duplicates. UDPATE也许我误解了问题,并且我认为您在工会失去重复项方面遇到了问题。

If the problem is that you want to do a row_number over a union why don't you do somthing like 如果问题是您要对联合执行row_number的操作,为什么不这样做

select row_number() over ( 
       partition by dateGroup, itemID 
       order by dateGroup, itemID) as editionID
FROM
(

         SELECT 

              dateGroup, itemID
          FROM TableA
          UNION ALL 
         SELECT 

              dateGroup, itemID
          FROM TableB 
) Data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM