在MySQL中,行列轉(zhuǎn)換是一種常見的操作。它包括行轉(zhuǎn)列和列轉(zhuǎn)行兩種情況。
- 行轉(zhuǎn)列:行轉(zhuǎn)列是將表中的某些行轉(zhuǎn)換成列,以提供更為清晰、易讀的數(shù)據(jù)視圖。例如,假設(shè)我們有一個(gè)包含科目和分?jǐn)?shù)的表,我們可以使用SUM和CASE語(yǔ)句將每個(gè)科目的分?jǐn)?shù)轉(zhuǎn)換為單獨(dú)的列。此外,從MySQL 8.0版本開始,還提供了PIVOT函數(shù)來實(shí)現(xiàn)行轉(zhuǎn)列的操作。例如:
SELECT aggregated_column, [pivot_value_1], [pivot_value_2], ..., [pivot_value_n] FROM (select...) AS source_table PIVOT ( aggregate_function (column_for_aggregation) FOR column_for_pivot IN ([pivot_value_1], [pivot_value_2], ..., [pivot_value_n]) ) AS pivot_table;
- 列轉(zhuǎn)行:列轉(zhuǎn)行則是將表中的某些列轉(zhuǎn)換成行,每行包含一列的值。具體的操作方法包括使用聚合函數(shù)、group_concat函數(shù)或動(dòng)態(tài)SQL語(yǔ)句塊等。例如,可以使用GROUP_CONCAT函數(shù)將某一列的值拼接成一個(gè)字符串,然后用聚合函數(shù)進(jìn)行分組。
測(cè)試題:
DROP TABLE IF EXISTS `stu`;
CREATE TABLE `stu` (
`sname` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`sub` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`score` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of stu
-- ----------------------------
INSERT INTO `stu` VALUES ('zs', 'chinese', '100');
INSERT INTO `stu` VALUES ('zs', 'math', '99');
INSERT INTO `stu` VALUES ('zs', 'english', '98');
INSERT INTO `stu` VALUES ('li', 'chinese', '80');
INSERT INTO `stu` VALUES ('li', 'math', '89');
INSERT INTO `stu` VALUES ('li', 'english', '88');
INSERT INTO `stu` VALUES ('ww', 'chinese', '70');
INSERT INTO `stu` VALUES ('ww', 'math', '79');
INSERT INTO `stu` VALUES ('ww', 'english', '78');
SET FOREIGN_KEY_CHECKS = 1;
初始化數(shù)據(jù):?
現(xiàn)在進(jìn)行行列轉(zhuǎn)換:
select sname,
case sub when "chinese" then score end "語(yǔ)文",
case sub when "math" then score end "數(shù)學(xué)",
case sub when "english" then score end "英語(yǔ)"
from stu;
?
?現(xiàn)在進(jìn)行分組統(tǒng)計(jì),然后合并:
select sname,
max(case sub when "chinese" then score end) "語(yǔ)文",
min(case sub when "math" then score end) "數(shù)學(xué)",
avg(case sub when "english" then score end) "英語(yǔ)"
from stu
GROUP BY sname;
現(xiàn)在行列轉(zhuǎn)化已經(jīng)完成!
行轉(zhuǎn)化為列:
小結(jié):case [列名] when [條件] then [數(shù)據(jù)] end
面試題1:
人員情況表(employee)中字段包括,員工號(hào)(ID),姓名(name),年齡(age),文化程度(wh):
包括四種情況(本科以上,大專,高中,初中以下),
現(xiàn)在我要根據(jù)年齡字段查詢統(tǒng)計(jì)出:
表中文化程度為本科以上,大專,高中,初中以下,各有多少人,占總?cè)藬?shù)多少。
結(jié)果如下A:
學(xué)歷 年齡 人數(shù) 百分比
本科以上 20 34 14
大專 20 33 13
高中 20 33 13
初中以下 20 100 40
本科以上 21 50 20
。。。。。。
SQL 查詢語(yǔ)句如何寫?
create table employee(id int primary key auto_increment,
name varchar(20),
age int(2),
wh varchar(20)
) ;
insert into employee(id,name,age,wh) values (null,'a',20,'本科以上') ;
insert into employee(id,name,age,wh) values (null,'b',20,'本科以上') ;
insert into employee(id,name,age,wh) values (null,'c',21,'本科以上') ;
insert into employee(id,name,age,wh) values (null,'d',20,'本科以上') ;
insert into employee(id,name,age,wh) values (null,'e',20,'大專') ;
insert into employee(id,name,age,wh) values (null,'e',21,'大專') ;
insert into employee(id,name,age,wh) values (null,'e',21,'高中') ;
insert into employee(id,name,age,wh) values (null,'e',20,'高中') ;
insert into employee(id,name,age,wh) values (null,'e',20,'初中以下') ;
?起始數(shù)據(jù):
通過wh[文化]、age[年齡]分組,即可統(tǒng)計(jì)出來:?
select wh '學(xué)歷',age '年齡',count(*) '人數(shù)', round((count(*)/(select count(0) from employee)) * 100) '百分比'
from employee
GROUP BY wh ,age
ORDER BY age;
面試題2:
-- 8:00--12:00 為遲到, 12:00--18:00 為早退
-- 打卡表 card
create table card(
cid int(10),
ctime timestamp ,
cuser int(10)
);
-- 人員表 person
create table person(
pid int(10),
name varchar(10)
) ;
-- 插入人員表的數(shù)據(jù)
insert into person values(1,'a');
insert into person values(2,'b');
-- 插入打卡的數(shù)據(jù)
insert into card values(1,'2009-07-19 08:02:00',1);
insert into card values(2,'2009-07-19 18:02:00',1);
insert into card values(3,'2009-07-19 09:02:00',2);
insert into card values(4,'2009-07-19 17:02:00',2);
insert into card values(5,'2009-07-20 08:02:00',1);
insert into card values(6,'2009-07-20 16:02:00',1);
insert into card values(7,'2009-07-20 07:02:00',2);
insert into card values(8,'2009-07-20 20:02:00',2);
-- 查詢 遲到 早退的員工姓名?
查詢結(jié)果如下:
工號(hào) 姓名 打卡日期 上班打卡 下班打卡 遲到 早退
1 a 2009-07-19 08:02:00 18:02:00 是 否
1 a 2009-07-20 08:02:00 16:02:00 是 是
2 b 2009-07-19 09:02:00 17:02:00 是 是
初始化表:?
?文章來源:http://www.zghlxwxcb.cn/news/detail-800770.html
-- 先查出每一個(gè)員工打卡的時(shí)間
select p.*,c.ctime
from person p join card c on c.cuser = p.pid;
-- 將日期格式化
select p.pid "工號(hào)",p.name "姓名",DATE_FORMAT(c.ctime,'%y-%m-%d') "打卡日期",DATE_FORMAT(c.ctime,'%h:%i:%s') "打卡時(shí)間"
from person p join card c on c.cuser = p.pid;
-- 將日期分離成上下午
select p.pid "工號(hào)",p.name "姓名",DATE_FORMAT(c.ctime,'%y-%m-%d') "打卡日期",DATE_FORMAT(c.ctime,'%h:%i:%s') "打卡時(shí)間"
from person p join card c on c.cuser = p.pid
文章來源地址http://www.zghlxwxcb.cn/news/detail-800770.html
到了這里,關(guān)于行列轉(zhuǎn)化【附加面試題】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!