将 user_sample 表:
uid | 201608 | 201609 | 201610 | 201611 |
---|---|---|---|---|
张三 | iPhone | mi | mi | |
李四 | mi | mi | ||
王五 | mi | iPhone | iPhone |
转为 phone_result 表:
uid | iPhone | mi | |
---|---|---|---|
张三 | 1 | 2 | 1 |
李四 | 0 | 2 | 2 |
王五 | 2 | 1 | 1 |
sql语句如下:
drop table if exists tmp;
drop table if exists phone_result;
create table tmp as select uid, `201608` as brand, `201608` as month from `user_sample`;
insert into tmp select uid, `201609` as brand, `201609` as month from `user_sample`;
insert into tmp select uid, `201610` as brand, `201610` as month from `user_sample`;
insert into tmp select uid, `201611` as brand, `201611` as month from `user_sample`;
create table phone_result as select distinct uid,
count(case brand when 'iPhone' then 1 end) as 'iPhone',
count(case brand when 'mi' then 1 end) as 'mi',
count(case brand when 'Google' then 1 end) as 'Google' from tmp group by uid;
drop table tmp;
想要了解更多内容,请持续关注码农资源网,一起探索发现编程世界的无限可能!
本站部分资源来源于网络,仅限用于学习和研究目的,请勿用于其他用途。
如有侵权请发送邮件至1943759704@qq.com删除
码农资源网 » mysql 列值与行值转换和统计
本站部分资源来源于网络,仅限用于学习和研究目的,请勿用于其他用途。
如有侵权请发送邮件至1943759704@qq.com删除
码农资源网 » mysql 列值与行值转换和统计