需要使用 sql join 将行转换为列的帮助 (Oracle 9i)
我有一个表,大约有 22 列和 6-7 千行,格式如下
Seq_num unique_id name ...
------------------------------------
1 1 abc
1 1 cde
2 1 lmn
2 1 opq
3 1 pqr
4 1 stu
1 2 oaq
2 2 zxq
3 2 fgw
3 2 pie
4 2 tie
我试图将每个 unique_id 的 4 个连续序列转换为单行 看起来
unique_id name ... name ... name ... name ...
--------------------------------------------------------------------------
1 abc lmn pqr stu
2 oaq zxq fgw tie
我使用 unique_Id 和 seq_num 上的分区以及左连接来实现此目的。
但是,我需要以以下格式从表中获取所有数据
unique_id name ... name ... name ... name ...
---------------------------------------------------------------------------
1 abc lmn pqr stu
1 cde opq pqr stu
2 oaq zxq fgw tie
2 oaq zxq pie tie
,即我需要在另一行中显示唯一的 seq_num (对于给定的 unique_id),如果缺少任何 seq_num, 使用通用的 seq_num (来自相同的 unique_id )来填充空白。
例如,
由于 unique_id 2 有两个 seq_num 3 (fgw,pie),unique_id 2 将有两行,看起来
2 oaq zxq fgw tie
2 oaq zxq pie tie
像这样可能吗? 那么,怎么样?
我使用的是 Oracle 9i。
谢谢,
R·保罗
I have a table, with abt 22 columns and 6-7 thousand rows in the following format
Seq_num unique_id name ...
------------------------------------
1 1 abc
1 1 cde
2 1 lmn
2 1 opq
3 1 pqr
4 1 stu
1 2 oaq
2 2 zxq
3 2 fgw
3 2 pie
4 2 tie
i'm trying to convert 4 consecutive sequences for each unique_id into a single row
which looks like
unique_id name ... name ... name ... name ...
--------------------------------------------------------------------------
1 abc lmn pqr stu
2 oaq zxq fgw tie
Im using partition over unique_Id and seq_num along with left joins to achieve this.
However, I need to get all the data from the table in the following format
unique_id name ... name ... name ... name ...
---------------------------------------------------------------------------
1 abc lmn pqr stu
1 cde opq pqr stu
2 oaq zxq fgw tie
2 oaq zxq pie tie
i.e. I need to display unique seq_num in another row (for a given unique_id) and if any seq_num is missing,
use the common seq_num (from same unique_id) to fill in the blank..
for e.g.
Since unique_id 2 has two seq_num 3 (fgw,pie), unique_id 2 will have two rows that look like
2 oaq zxq fgw tie
2 oaq zxq pie tie
Is something like this possible and if
so, how?
Im using Oracle 9i.
Thanks,
R. Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,您需要填补缺失的空白。以下代码段无法按预期工作。思考...
以下代码是 WIP。这是生成一整套 SEQ_NUM、UNIQUE_ID 和 NAME 列的第一次尝试,但我还没有机会测试它。注意 - 我可以选择使用 CONNECT BY 技巧生成数字,但这在 9i 中有点不稳定(而且也没有记录)。So, you need to fill in the missing gaps. The following piece of code doesn't work as intended. Thinking...
The following piece of code is a WIP. It is a first stab at generating a complete set of SEQ_NUM, UNIQUE_ID and NAME columns, but I haven't had the chance to test it yet.NB - I could have chosen to use the CONNECT BY trick to generate the numbers but that was a bit flaky in 9i (and was also undocumented).不是答案,而是要求澄清。在您的示例中:
您将第一行中的“abc”与“lmn”配对,并将第二行中的“cde”与“opq”配对。以下行集也是有效的“解决方案”:
其中“abc”与第一行中的“opq”配对,“cde”与第二行中的“lmn”配对?
如果是这样,我不确定您如何判断您拥有有效的答案集,因为数据中似乎没有任何内容可以帮助您区分两者。
Not an answer, but a request for clarification. In your example:
You pair up "abc" with "lmn" in the first row, and "cde" with "opq" in the second row. Is the following set of rows also a valid "solution":
Where "abc" is paired with "opq" in the first row and "cde" is paired with "lmn" in the second?
If so, I'm not sure how you can tell that you have a valid answer set, as there doesn't appear to be anything in the data that would help you distinguish the two.