English 中文(简体)
如何生成基于最小值和最大值一系列数字的列
原标题:How to generate a column with a series of numbers based on a min and max value

我有一张按以下结构排列的桌子:

fake_id          start          end          misc_data
------------------------------------------------------
1                101            105          ab
1                101            105          cd
1                101            105          ef
2                117            123          gh
2                117            123          ij
2                117            123          kl
2                117            123          mn
3                51             53           op
3                51             53           qr

注意假的字段不是真的一个主键,而是重复数倍于开始和结束时指定范围内不同奇数的数量。每个记录的 real id是该范围内的奇数之一。 我需要写入一个返回假的、错误的数据的查询, 以及包含这些奇数的另一列以生成一个真实的奇数, 具体如下 :

fake_id          real_id          misc_data
------------------------------------------
1                101              ab
1                103              cd
1                105              ef
2                117              gh
2                119              ij
2                121              kl
2                123              mn
3                51               op
3                53               qr

就我所知,无法保证序列中不会出现空白(例如,可能没有21-31范围的记录 ) 。 我如何告诉查询(或程序,但查询更可取 ), 对于每个带有特定假代号的记录,它应该从开始到结束之间返回下一个奇数?

还有, 是否有一种方法可以让错误数据值属于一个特定的真实身份? 以第二个表格为例, 我如何告诉询问“ AB” 属于真实的 101 而不是 103?

提前感谢。

问题回答

在此猜测您计划排序 misc_ data :

SELECT "fake_id",
       ((ROW_NUMBER()OVER(PARTITION BY "start"
                        ORDER BY "misc_data")-1)*2)+"start" AS "real_id",
       "misc_data"
FROM t
ORDER BY "misc_data";

http://www.sqlfiddle.com/#!4/ae23c/23" rel="no follow" >http://www.sqlfiddle.com/#!4/ae23c/23

抱歉不早点回答或个别评论。 @ John Dewey, 我相信当我尝试您的脚本时, 它没有正确保留开始结束序列之间的间隔, 但我有志于了解更多关于partition 关键词的信息,

由于这是ETL的任务, 我最后写出代码来生成真实的ID, 在提取的侧面上循环( 我猜它也会算作变换) 。





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

热门标签