oracle相同行多列分组

时间:2013-10-16 07:50:12

标签: sql oracle

你能帮我解决一下这个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 个答案:

答案 0 :(得分: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

这里是demo in SQLFiddle

答案 1 :(得分:0)

Read this

试试这个:

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;

答案 2 :(得分:0)

如果我得到你打算做的话,那就快点吧:

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
;

这给出了:

MNP 123 234
MN  345 
PQR 567 678
XYZ 789 890

根据您使用的Oracle版本,您可以使用pivot子句保存一个子查询(但它的使用有点复杂)。

答案 3 :(得分:0)

有意添加另一个答案:From here

在Oracle 11G中进行转置的三种可能选项

  1. DECODE选项
  2. 一个。如果要转置和汇总

    ,请对新列使用SUM和DECODE

    湾如果要转置而不是汇总

    ,请使用MAX和DECODE作为新列
    1. 使用PIVOT

    2. 如Gordon所说,使用WITH和SUB SELECT

    3. 要使用数据透视表,您的代码中无法使用数据集,因为您的初始数据集已经过旋转。