一个竖表转横表的问题,折腾了大半天,唉,累死我了,赶紧趁热记一下,不然过会儿又忘了。
唉,网上的例子看了半天也没看懂,最后才发现,其实就是:0+0=0, 0+1=1, ......
a: id, name
1 li
2 wang
b: id aid lb
1 1 a
2 1 b
3 2 b
-------------------------------------
a+b: a.name b.lb-a b.lb-b
li 1 1
wang 0 1
SQL: select a.name, sun( decode(b.lb, 'a', 1, 0) ) as b.lb-a, sun( decode(b.lb, 'b', 1, 0) ) as b.lb-b
from a, b
where a.id=b.aid
group by a.name
唉,网上的例子看了半天也没看懂,最后才发现,其实就是:0+0=0, 0+1=1, ......
a: id, name
1 li
2 wang
b: id aid lb
1 1 a
2 1 b
3 2 b
-------------------------------------
a+b: a.name b.lb-a b.lb-b
li 1 1
wang 0 1
SQL: select a.name, sun( decode(b.lb, 'a', 1, 0) ) as b.lb-a, sun( decode(b.lb, 'b', 1, 0) ) as b.lb-b
from a, b
where a.id=b.aid
group by a.name