select a.BRANCH_SALE,
a.amount a_amount,
b.amount b_amount
(select BRANCH_SALE,sum(amount) amount
from table_name
group by BRANCH_SALE) a,
(select BRANCH_SALE,sum(amount) amount
from table_name
group by BRANCH_SALE) b
where a.BRANCH_SALE=b.BRANCH_SALE
a.amount a_amount,
b.amount b_amount
(select BRANCH_SALE,sum(amount) amount
from table_name
group by BRANCH_SALE) a,
(select BRANCH_SALE,sum(amount) amount
from table_name
group by BRANCH_SALE) b
where a.BRANCH_SALE=b.BRANCH_SALE
北京 上海 南京
销售 购入 销售 购入 销售 购入select decode(BRANCH_SALE,'北京',amount,0) 北京销售,decode(BRANCH_BUY,'北京',amount,0) 北京购入,
decode(BRANCH_SALE,'上海',amount,0) 上海销售,decode(BRANCH_BUY,'上海',amount,0) 上海购入,
decode(BRANCH_SALE,'南京',amount,0) 南京销售,decode(BRANCH_BUY,'南京',amount,0) 南京购入
from yourtable
sum(decode(BRANCH_SALE,'上海',amount,0)) 上海销售,sum(decode(BRANCH_BUY,'上海',amount,0)) 上海购入,
sum(decode(BRANCH_SALE,'南京',amount,0)) 南京销售,sum(decode(BRANCH_BUY,'南京',amount,0)) 南京购入
from table group by ROW_ID,BRANCH_SALE,BRANCH_BUY,
表名:TB_TESTSQL如下:(在ORACLE SQL*PLUS下运行通过)
select BRANCH_SALE||' 销售' 销售购入,
sum(decode(BRANCH_SALE,'北京',amount,0)) 北京销售,
sum(decode(BRANCH_BUY,'北京',amount,0)) 北京购入,
sum(decode(BRANCH_SALE,'南京',amount,0)) 南京销售,
sum(decode(BRANCH_BUY,'南京',amount,0)) 南京购入,
sum(decode(BRANCH_SALE,'上海',amount,0)) 上海销售,
sum(decode(BRANCH_BUY,'上海',amount,0)) 上海购入
from tb_test
group by BRANCH_SALE||' 销售'
union all
select BRANCH_BUY||'购入' 销售购入,
sum(decode(BRANCH_SALE,'北京',amount,0)) 北京销售,
sum(decode(BRANCH_BUY,'北京',amount,0)) 北京购入,
sum(decode(BRANCH_SALE,'南京',amount,0)) 南京销售,
sum(decode(BRANCH_BUY,'南京',amount,0)) 南京购入,
sum(decode(BRANCH_SALE,'上海',amount,0)) 上海销售,
sum(decode(BRANCH_BUY,'上海',amount,0)) 上海购入
from tb_test
group by BRANCH_BUY||'购入'
Order by 销售购入 Asc结果如下:(不知是不是你想要的结果)
销售购入 北京销售 北京购入 南京销售 南京购入 上海销售 上海购入
--------- --------- --------- --------- --------- --------- ---------
北京 销售 200 0 0 100 0 100
北京购入 0 100 0 0 100 0
南京 销售 0 0 100 0 0 100
南京购入 100 0 0 100 0 0
上海 销售 0 100 0 0 100 0
上海购入 100 0 100 0 0 200