我做过类似的。这样吧!
select z.name name, x."n月数量", x."n月金额", y."n + 1月数量", y."n + 1月金额", z."n + 2月数量", z."n + 1月金额"
from (
(select  name, round(sum(qty)) "n月数量", round(sum(qty * price)) "n月金额"
 from  table
 where  table.YMD > ADD_MONTHS (TRUNC (SYSDATE), -n)) x,
(select  name, round(sum(qty)) "n + 1月数量", round(sum(qty * price)) "n + 1月金额"
 from  table
 where  table.YMD > ADD_MONTHS (TRUNC (SYSDATE), -(n + 1))) y
(select  name, round(sum(qty)) "n + 2月数量", round(sum(qty * price)) "n + 2月金额"
 from  table
 where  table.YMD > ADD_MONTHS (TRUNC (SYSDATE), -(n + 2))) z
)
where x.name(+) = y.name and x.name(+) = z.name and y.name(+) = z.name

解决方案 »

  1.   

    select z.name name, x."n月数量", x."n月金额", y."n + 1月数量", y."n + 1月金额", z."n + 2月数量", z."n + 1月金额"
    from (
          (select name, round(sum(qty)) "n月数量", round(sum(qty * price)) "n月金额"
           from   table
           where  table.YMD > ADD_MONTHS (TRUNC (SYSDATE), -n)) x,
          (select name, round(sum(qty)) "n + 1月数量", round(sum(qty * price)) "n + 1月金额"
           from   table
           where  table.YMD > ADD_MONTHS (TRUNC (SYSDATE), -(n + 1))) y
          (select name, round(sum(qty)) "n + 2月数量", round(sum(qty * price)) "n + 2月金额"
           from   table
           where  table.YMD > ADD_MONTHS (TRUNC (SYSDATE), -(n + 2))) z
          )
    where x.name(+) = y.name and x.name(+) = z.name and y.name(+) = z.name
      

  2.   

    select a.* ,b.* ,c.* from (select distinct name , qty , price*qty from table_name where YMD like '%n%') a, (select distinct qty , price*qty from table_name where YMD like '%n+1%') b, (select qty , price*qty from table_name  where  YMD like '%n+2%') c
    试试看啊