把分类转成列,然后进行统计,哪位有时间帮我看看怎么写这个SQL呀。分类那一列是动态的
支付方式 分类 单价
现金 A 1
现金 B 2
微信 B 3
微信 B 4
微信 A 5
支付宝 A 6
支付宝 C 7
结果:
支付方式 A B C 总价
现金 1 2 0 3
微信 5 7 0 12
支付宝 6 0 7 13
...全文
2912打赏收藏
MYSQL统计请教
把分类转成列,然后进行统计,哪位有时间帮我看看怎么写这个SQL呀。分类那一列是动态的 支付方式 分类 单价 现金 A 1 现金 B 2 微信 B 3 微信 B 4 微信 A 5 支付宝 A 6 支付宝 C 7 结果: 支付方式 A B C 总价 现金 1 2 0 3 微信 5 7 0 12 支付宝 6 0 7 13
中文用拼音代替了:
with tb (zffs, fl, dj) as
(
select 'xj', 'A', 1 union all
select 'xj', 'B', 2 union all
select 'wx', 'B', 3 union all
select 'wx', 'B', 4 union all
select 'wx', 'A', 5 union all
select 'zfb', 'A', 6 union all
select 'zfb', 'C', 7
)
select
zffs
, (select ISNULL(SUM(dj),0) from tb where fl= 'A' AND zffs = t.zffs) as A
, (select ISNULL(SUM(dj),0) from tb where fl= 'B' AND zffs = t.zffs) as B
, (select ISNULL(SUM(dj),0) from tb where fl= 'C' AND zffs = t.zffs) as C
, (select ISNULL(SUM(dj),0) from tb where zffs = t.zffs) as zj
from tb t
group by zffs