选择Union查询中的列标题

时间:2013-01-12 17:03:19

标签: sql oracle

我必须像以下示例一样选择查询:

Select name,
       SUM(DECODE(TO_CHAR(Datee,'MM'),'01',PRICE)) JAN_12,
       SUM(DECODE(TO_CHAR(Datee,'MM'),'02',PRICE)) FEB_12
 from price_list_LastYear

 UNION ALL

 Select name,
        SUM(DECODE(TO_CHAR(Datee,'MM'),'01',PRICE)) JAN_11,
        SUM(DECODE(TO_CHAR(Datee,'MM'),'02',PRICE)) FEB_11
  from price_list_TwoLastYear.

当我执行查询时,在我的列标题中,我得到名称JAN_12FEB_12。 我希望将结果合并为:name,JAN_11JAN_12FEB_11FEB_12

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT P1.name,
       SUM(DECODE(TO_CHAR(P1.Datee,'MM'),'01',P1.PRICE)) JAN_12,
       SUM(DECODE(TO_CHAR(P1.Datee,'MM'),'02',P1.PRICE)) FEB_12,
       SUM(DECODE(TO_CHAR(P2.Datee,'MM'),'01',P2.PRICE)) JAN_11,
       SUM(DECODE(TO_CHAR(P2.Datee,'MM'),'02',P2.PRICE)) FEB_11
FROM price_list_LastYear P1
   JOIN price_list_TwoLastYear P2
     ON P1.nmae = P2.nmae
GROUP BY P1.nmae

如果两个表中都没有相同的名称,请尝试:

SELECT N.name,
   SUM(DECODE(TO_CHAR(P1.Datee,'MM'),'01',P1.PRICE)) JAN_12,
   SUM(DECODE(TO_CHAR(P1.Datee,'MM'),'02',P1.PRICE)) FEB_12,
   SUM(DECODE(TO_CHAR(P2.Datee,'MM'),'01',P2.PRICE)) JAN_11,
   SUM(DECODE(TO_CHAR(P2.Datee,'MM'),'02',P2.PRICE)) FEB_11
FROM
(
    SELECT name FROM price_list_LastYear
    UNION 
    SELECT name FROM price_list_TwoLastYear
) N
LEFT JOIN price_list_LastYear P1
   ON N.nmae = P1.name
LEFT JOIN price_list_TwoLastYear P2
   ON N.nmae = P2.name
GROUP BY N.name

答案 1 :(得分:0)

试试这个

SELECT A.*, B,JAN_11, B.FEB_11 FROM (
Select name,
       SUM(DECODE(TO_CHAR(Datee,'MM'),'01',PRICE)) JAN_12,
       SUM(DECODE(TO_CHAR(Datee,'MM'),'02',PRICE)) FEB_12
 from price_list_LastYear
 Group By Name) A

 LEFT OUTER JOIN (

 Select name,
        SUM(DECODE(TO_CHAR(Datee,'MM'),'01',PRICE)) JAN_11,
        SUM(DECODE(TO_CHAR(Datee,'MM'),'02',PRICE)) FEB_11
  from price_list_TwoLastYear
  Group By Name) B ON A.name = B.Name

或者

Select name,
SUM(CASE WHEN Yr = '11' THEN DECODE(Mnth,'01',PRICE) ELSE NULL END) JAN_11,
SUM(CASE WHEN Yr = '12' THEN DECODE(Mnth,'01',PRICE) ELSE NULL END) JAN_12,
SUM(CASE WHEN Yr = '11' THEN DECODE(Mnth,'02',PRICE) ELSE NULL END) FEB_11,
SUM(CASE WHEN Yr = '12' THEN DECODE(Mnth,'02',PRICE) ELSE NULL END) FEB_12
from 
(

 SELECT '12' As yr, name, TO_CHAR(Datee,'MM') Mnth, Price
 FROM price_list_LastYear
 UNION ALL
 SELECT '11' As yr, name, TO_CHAR(Datee,'MM') Mnth, Price
 FROM price_list_TwoLastYear
)
GROUP BY Name

答案 2 :(得分:0)

如果两个表中都不存在name,则可能难以使用JOIN。所以这是一个使用UNION ALL的版本,然后应用带有CASE表达式的聚合函数将数据转换为列:

SELECT name,
  sum(case when Month = 'JAN_12' then price else 0 end) JAN_12,
  sum(case when Month = 'FEB_12' then price else 0 end) FEB_12,
  sum(case when Month = 'JAN_11' then price else 0 end) JAN_11,
  sum(case when Month = 'FEB_11' then price else 0 end) FEB_11
FROM
(
  Select name,
    PRICE,
    case 
      when TO_CHAR(Datee,'MM') = '01' Then 'JAN_12'
      when TO_CHAR(Datee,'MM') = '02' Then 'FEB_12' End Month
  from price_list_LastYear
  WHERE TO_CHAR(Datee,'MM') in ('01', '02')
  UNION ALL
  Select name,
    PRICE,
    case 
      when TO_CHAR(Datee,'MM') = '01' Then 'JAN_11'
      when TO_CHAR(Datee,'MM') = '02' Then 'FEB_11' End Month
  from price_list_TwoLastYear
  WHERE TO_CHAR(Datee,'MM') in ('01', '02')
) src
GROUP BY name