cast(sum(case when t.enkey = 'a' then 1 else 0 end) as varchar) a,
cast(sum(case when t.enkey = 'b' then 2 else 0 end) as varchar) b,
cast(sum(case when t.enkey = 'c' then 3 else 0 end) as varchar) c
from (select enkey,listkey from indexlists where enkey in('M','A','XS','YS','TZ') ) t
union all
select 'listkey','a','b','c'
1、在mysql环境,创建数据库表,
create table test_data1(id int, name VARCHAR(20), day VARCHAR(20))
2、插入测试数据,
insert into test_data1 values(1,'liu',1)
insert into test_data1 values(2,'liu',3)
insert into test_data1 values(3,'wang',1)
insert into test_data1 values(4,'wang',2)
insert into test_data1 values(4,'wang',4)
3、查询表中数据,select * from test_data1;
4、编写目标sql;
select name,
max(case when day=1 then 1 end) d_1,
max(case when day=2 then 2 end) d_2,
max(case when day=3 then 3 end) d_3,
max(case when day=4 then 4 end) d_4
from test_data1 t group by name
整理数据 :
看一下数据
拼接成Table,这里简单演示
结果展示
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)