使用mysql查询更改mysql表的格式
我在使用mysql查询更改mysql表的格式,mysql,Mysql,我在mysql数据库中有下表 我想得到以下格式的结果 这是一个典型的表透视问题,请尝试以下操作: select customerName, coalesce(max(case when month(`date`) = 1 then paidamount end), '-') as `January` , coalesce(max(case when month(`date`) = 2 then paidamount end), '-') as `February`,
mysql
数据库中有下表
我想得到以下格式的结果
这是一个典型的表透视问题,请尝试以下操作:
select
customerName,
coalesce(max(case when month(`date`) = 1 then paidamount end), '-') as `January` ,
coalesce(max(case when month(`date`) = 2 then paidamount end), '-') as `February`,
coalesce(max(case when month(`date`) = 3 then paidamount end), '-') as `March`,
coalesce(max(case when month(`date`) = 4 then paidamount end), '-') as `April`,
coalesce(max(case when month(`date`) = 5 then paidamount end), '-') as `May`,
coalesce(max(case when month(`date`) = 6 then paidamount end), '-') as `June`,
coalesce(max(case when month(`date`) = 7 then paidamount end), '-') as `July`,
coalesce(max(case when month(`date`) = 8 then paidamount end), '-') as `August`,
coalesce(max(case when month(`date`) = 9 then paidamount end), '-') as `September`,
coalesce(max(case when month(`date`) = 10 then paidamount end), '-') as `October`,
coalesce(max(case when month(`date`) = 11 then paidamount end), '-') as `November`,
coalesce(max(case when month(`date`) = 12 then paidamount end), '-') as `December`
from yourtable
group by customerName
这是一个典型的表透视问题,请尝试以下操作:
select
customerName,
coalesce(max(case when month(`date`) = 1 then paidamount end), '-') as `January` ,
coalesce(max(case when month(`date`) = 2 then paidamount end), '-') as `February`,
coalesce(max(case when month(`date`) = 3 then paidamount end), '-') as `March`,
coalesce(max(case when month(`date`) = 4 then paidamount end), '-') as `April`,
coalesce(max(case when month(`date`) = 5 then paidamount end), '-') as `May`,
coalesce(max(case when month(`date`) = 6 then paidamount end), '-') as `June`,
coalesce(max(case when month(`date`) = 7 then paidamount end), '-') as `July`,
coalesce(max(case when month(`date`) = 8 then paidamount end), '-') as `August`,
coalesce(max(case when month(`date`) = 9 then paidamount end), '-') as `September`,
coalesce(max(case when month(`date`) = 10 then paidamount end), '-') as `October`,
coalesce(max(case when month(`date`) = 11 then paidamount end), '-') as `November`,
coalesce(max(case when month(`date`) = 12 then paidamount end), '-') as `December`
from yourtable
group by customerName
您可以使用以下查询:
select
customerName,
coalesce(sum(case when month(`date`) = 1 then paidamount end), '-') as `January` ,
coalesce(sum(case when month(`date`) = 2 then paidamount end), '-') as `February`,
coalesce(sum(case when month(`date`) = 3 then paidamount end), '-') as `March`,
coalesce(sum(case when month(`date`) = 4 then paidamount end), '-') as `April`,
coalesce(sum(case when month(`date`) = 5 then paidamount end), '-') as `May`,
coalesce(sum(case when month(`date`) = 6 then paidamount end), '-') as `June`,
coalesce(sum(case when month(`date`) = 7 then paidamount end), '-') as `July`,
coalesce(sum(case when month(`date`) = 8 then paidamount end), '-') as `August`,
coalesce(sum(case when month(`date`) = 9 then paidamount end), '-') as `September`,
coalesce(sum(case when month(`date`) = 10 then paidamount end), '-') as `October`,
coalesce(sum(case when month(`date`) = 11 then paidamount end), '-') as `November`,
coalesce(sum(case when month(`date`) = 12 then paidamount end), '-') as `December`
from test
group by customerName
您可以使用以下查询:
select
customerName,
coalesce(sum(case when month(`date`) = 1 then paidamount end), '-') as `January` ,
coalesce(sum(case when month(`date`) = 2 then paidamount end), '-') as `February`,
coalesce(sum(case when month(`date`) = 3 then paidamount end), '-') as `March`,
coalesce(sum(case when month(`date`) = 4 then paidamount end), '-') as `April`,
coalesce(sum(case when month(`date`) = 5 then paidamount end), '-') as `May`,
coalesce(sum(case when month(`date`) = 6 then paidamount end), '-') as `June`,
coalesce(sum(case when month(`date`) = 7 then paidamount end), '-') as `July`,
coalesce(sum(case when month(`date`) = 8 then paidamount end), '-') as `August`,
coalesce(sum(case when month(`date`) = 9 then paidamount end), '-') as `September`,
coalesce(sum(case when month(`date`) = 10 then paidamount end), '-') as `October`,
coalesce(sum(case when month(`date`) = 11 then paidamount end), '-') as `November`,
coalesce(sum(case when month(`date`) = 12 then paidamount end), '-') as `December`
from test
group by customerName
客户名称是唯一字段吗?没有客户名称不是唯一字段吗?没有客户名称不是唯一字段吗?没有客户名称不是唯一字段没有给出每月支付金额的总和我用sum()代替max()现在可以了谢谢没有给出每月支付金额的总和我用sum()代替max()现在可以了谢谢