0

次の例のようにクエリを選択する必要があります。

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_12, FEB_12. 次のような結果を結合したい: name , JAN_11, JAN_12, .FEB_11FEB_12

4

3 に答える 3

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
于 2013-01-12T17:10:50.263 に答える
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
于 2013-01-12T17:07:44.127 に答える
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
于 2013-01-12T17:14:25.797 に答える