2009年2月24日星期二

Crosstab in MySQL

A lot of people asking me how can I do in SQL statement as Crosstab. So, here I publish the solution at here. Hope really can settle some of the person whose face the same problem.

Examples,
select "",
sum( if(so_type = "WV", invoice_amount, null) ) as wv_amount,
count( if(so_type = "WV", invoice_amount, null) ) as wv_count,
sum( if(so_type = "RW", invoice_amount, null) ) as rw_amount,
count( if(so_type = "RW", invoice_amount, null) ) as rw_count,
sum( if(so_type <> "WV" and so_type <> "RW", invoice_amount, null) ) as misc_amount,
count( if(so_type <> "WV" and so_type <> "RW", invoice_amount, null) ) as misc_count
from rbs_so
WHERE
group by ""

For more detail regarding this issue. You can get more information at here http://blog.adaniels.nl/articles/creating-a-cross-tab-in-mysql/

没有评论:

发表评论