前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >mysql 列值与行值转换和统计

mysql 列值与行值转换和统计

原创
作者头像
Power
发布2025-03-05 11:31:07
发布2025-03-05 11:31:07
1500
代码可运行
举报
运行总次数:0
代码可运行

将 user_sample 表:

uid

201608

201609

201610

201611

张三

iPhone

mi

mi

Google

李四

mi

mi

Google

Google

王五

Google

mi

iPhone

iPhone

转为 phone_result 表:

uid

iPhone

mi

Google

张三

1

2

1

李四

0

2

2

王五

2

1

1

sql语句如下:

代码语言:javascript
代码运行次数:0
复制
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;

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档