需要使用 sql join 将行转换为列的帮助 (Oracle 9i)

发布于 2024-09-13 06:44:49 字数 1781 浏览 3 评论 0原文

我有一个表,大约有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长不大的小祸害 2024-09-20 06:44:49

因此,您需要填补缺失的空白。以下代码段无法按预期工作。思考...

以下代码是 WIP。这是生成一整套 SEQ_NUM、UNIQUE_ID 和 NAME 列的第一次尝试,但我还没有机会测试它。

select seq_num
       , unique_id
       , case 
            when name is not null then name 
            else lag (name, 1) 
               over ( partition by unique_id, seq_num 
                      order by nvl2(name, 1, 0)
                    ) end as name
from
    (    
        select t.seq_num
               , y.unique_id
               , y.name
        from ( select 1 as seq_num from dual
               union all
               select 2 as seq_num from dual
               union all
               select 3 as seq_num from dual
               union all
               select 4 as seq_num from dual ) t
        left outer join
             your_table y 
                 on ( t.seq_num = y.seq_num )   
    )

注意 - 我可以选择使用 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.

select seq_num
       , unique_id
       , case 
            when name is not null then name 
            else lag (name, 1) 
               over ( partition by unique_id, seq_num 
                      order by nvl2(name, 1, 0)
                    ) end as name
from
    (    
        select t.seq_num
               , y.unique_id
               , y.name
        from ( select 1 as seq_num from dual
               union all
               select 2 as seq_num from dual
               union all
               select 3 as seq_num from dual
               union all
               select 4 as seq_num from dual ) t
        left outer join
             your_table y 
                 on ( t.seq_num = y.seq_num )   
    )

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).

一个人的旅程 2024-09-20 06:44:49

不是答案,而是要求澄清。在您的示例中:

1           abc             lmn             pqr             stu
1           cde             opq             pqr             stu

您将第一行中的“abc”与“lmn”配对,并将第二行中的“cde”与“opq”配对。以下行集也是有效的“解决方案”:

1           abc             opq             pqr             stu
1           cde             lmn             pqr             stu

其中“abc”与第一行中的“opq”配对,“cde”与第二行中的“lmn”配对?

如果是这样,我不确定您如何判断您拥有有效的答案集,因为数据中似乎没有任何内容可以帮助您区分两者。

Not an answer, but a request for clarification. In your example:

1           abc             lmn             pqr             stu
1           cde             opq             pqr             stu

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":

1           abc             opq             pqr             stu
1           cde             lmn             pqr             stu

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文