0

你能帮我解决这个 SQL 查询吗?

我有两个表如下

表A

ID      NUM 
------------
AA      123 
BB      234 
CC      345 
DD      567 
EE      678 
FF      789 
GG      890 

表 B

ID      SER 
----------- 
AA      MNP 
BB      MNP 
CC      MN  
DD      PQR 
EE      PQR 
FF      XYZ 
GG      XYZ 

我希望结果显示如下

表 C

SER        NUM     NUM2 
-----------------------
MNP        123     234  
MN         345     NULL 
PQR        123     234  
XYZ        123     234  

我尝试了不同的查询。但是,无法获得如何获得相同列结果的线索。

任何帮助将不胜感激 。

4

4 回答 4

1

你也可以试试这个:

select ser, 
       max(decode(rank, 1, num, null)) num, 
       max(decode(rank, 2, num, null)) num2
from (
  select SER, num, rank() over(partition by SER order by num) rank
  from tablea a 
  inner join tableb b on a.id = b.id
  order by ser, rank() over(partition by SER order by num)
)  
group by ser

这里是SQLFiddle 中的演示

于 2013-10-16T09:07:07.453 回答
0

A quick one, if I got what you intend to do:

select Y.SER, max(Y.NUM) NUM, max(Y.NUM2) NUM2
from
(
  select B.SER, decode(X.lvl, 1, X.NUM, null) NUM, decode(X.lvl, 2, X.NUM, null) NUM2
  from
  (
    select B.SER, B.ID, A.NUM, rank() over (partition by B.SER order by A.ID) lvl
    from B, A
    where A.ID (+) = B.ID
  ) X, B
  where B.SER = X.SER
    and B.ID = X.ID
) Y
group by Y.SER
;

This gives:

MNP 123 234
MN  345 
PQR 567 678
XYZ 789 890

Depending on the version of Oracle you're using, you could use the pivot clause to save one sub-query (but its use is a bit complex).

于 2013-10-16T08:58:57.637 回答
0

故意添加另一个答案:从这里

在 Oracle 11G 中转置的三个可能选项

  1. 解码选项

一个。如果要转置和汇总,请对新列使用 SUM 和 DECODE

湾。如果要转置而不是汇总,请对新列使用 MAX 和 DECODE

  1. 使用枢轴

  2. 使用 Gordon 所说的 WITH 和 SUB SELECT

要使用数据透视,在您的代码中是不可能的,因为您的初始数据集已经被透视。

于 2013-10-16T09:54:10.110 回答
0

读这个

尝试这个:

WITH TABLEA
    AS (SELECT
             'AA' AS ID,
             123 AS NUM
        FROM
             DUAL
        UNION ALL
        SELECT
             'BB' AS ID,
             234 AS NUM
        FROM
             DUAL
        UNION ALL
        SELECT
             'CC' AS ID,
             345 AS NUM
        FROM
             DUAL
        UNION ALL
        SELECT
             'DD' AS ID,
             567 AS NUM
        FROM
             DUAL
        UNION ALL
        SELECT
             'EE' AS ID,
             678 AS NUM
        FROM
             DUAL
        UNION ALL
        SELECT
             'FF' AS ID,
             789 AS NUM
        FROM
             DUAL
        UNION ALL
        SELECT
             'GG' AS ID,
             890 AS NUM
        FROM
             DUAL),
    TABLEB
    AS (SELECT
             'AA' AS ID,
             'MNP' AS SER
        FROM
             DUAL
        UNION ALL
        SELECT
             'BB' AS ID,
             'MNP' AS SER
        FROM
             DUAL
        UNION ALL
        SELECT
             'CC' AS ID,
             'MN' AS SER
        FROM
             DUAL
        UNION ALL
        SELECT
             'DD' AS ID,
             'PQR' AS SER
        FROM
             DUAL
        UNION ALL
        SELECT
             'EE' AS ID,
             'PQR' AS SER
        FROM
             DUAL
        UNION ALL
        SELECT
             'FF' AS ID,
             'XYZ' AS SER
        FROM
             DUAL
        UNION ALL
        SELECT
             'GG' AS ID,
             'XYZ' AS SER
        FROM
             DUAL)
SELECT
      TABLEB.SER,
      RTRIM ( XMLAGG ( XMLELEMENT ( E,
                              TABLEA.NUM
                              || ',' ) ).EXTRACT ( '//text()' ),
            ',' )
          NUM
FROM
      TABLEA,
      TABLEB
WHERE
      TABLEA.ID = TABLEB.ID
GROUP BY
      TABLEB.SER
ORDER BY
      TABLEB.SER;
于 2013-10-16T08:47:02.087 回答