MySql透视表

上脚本

 SELECT aa.cus_number, aa.real_name ,aa.mobile ,
SUM( CASE WHEN aa.`invest_limit` = '7' THEN aa.`amount` ELSE 0 END ) AS `7`,
SUM( CASE WHEN aa.`invest_limit` = '14' THEN aa.`amount` ELSE 0 END ) AS `14`,
SUM( CASE WHEN aa.`invest_limit`= '15' THEN aa.`amount` ELSE 0 END ) AS `15`,
SUM( CASE WHEN aa.`invest_limit` = '28' THEN aa.`amount` ELSE 0 END ) AS `28`,
SUM( CASE WHEN aa.`invest_limit` = '30' THEN aa.`amount` ELSE 0 END ) AS `30`,
SUM( CASE WHEN aa.`invest_limit` = '42' THEN aa.`amount` ELSE 0 END ) AS `42`,
SUM( CASE WHEN aa.`invest_limit` = '56' THEN aa.`amount` ELSE 0 END ) AS `56`,
SUM( CASE WHEN aa.`invest_limit` = '60' THEN aa.`amount` ELSE 0 END ) AS `60`,
SUM( CASE WHEN aa.`invest_limit` = '90' THEN aa.`amount` ELSE 0 END ) AS `90`,
SUM( CASE WHEN aa.`invest_limit` = '180' THEN aa.`amount` ELSE 0 END ) AS `180`
from(
SELECT a.*,mr.real_name,cl.mobile
from invest a
LEFT JOIN member_realname mr on a.cus_number = mr.cus_number
LEFT JOIN cus_login cl on a.cus_number = cl.cus_number
where a.cus_number in (SELECT x.cus_number from (SELECT cus_number,sum(amount) from invest GROUP BY cus_number ORDER BY sum(amount) desc limit 200) x) ) aa
GROUP BY aa.cus_number

你可能感兴趣的:(MySql透视表)