mysql 分组统计 case when 及 if 语句使用

  • 作者: 凯哥Java(公众号:凯哥Java)
  • mysql
  • 时间:2021-07-23 14:12
  • 3904人已阅读
简介 需求:同时:中文名重复了就是1部剧,不重复就是多部剧需求分析:这个需要分组、需要统计、需要判断、需要distinct。分组:根据地区、类别、中文名称分类;判断:根据类型进行判断同时要求和因为中文名称重复的就是1部,所以统计的时候,要使用到distinct去重。表语句:DROP TABLE IF EXISTS `yj_item_plan`;CREATE&nbs

🔔🔔好消息!好消息!🔔🔔

 如果您需要注册ChatGPT,想要升级ChatGPT4。凯哥可以代注册ChatGPT账号代升级ChatGPT4

有需要的朋友👉:微信号 kaigejava2022

需求:
bf421218d2151028d8354839b2d4d4a1.png

同时:中文名重复了就是1部剧,不重复就是多部剧

需求分析:

这个需要分组、需要统计、需要判断、需要distinct。

分组:根据地区、类别、中文名称分类;

判断:根据类型进行判断同时要求和

因为中文名称重复的就是1部,所以统计的时候,要使用到distinct去重。

表语句:

DROP TABLE IF EXISTS `yj_item_plan`;
CREATE TABLE `yj_item_plan` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `company_id` bigint(20) DEFAULT '0' COMMENT '引进公司ID,关联yj_company id',
  `apply_num` varchar(100) NOT NULL DEFAULT '' COMMENT '申请编号',
  `company_type` tinyint(4) DEFAULT '0' COMMENT '引进单位类型:10、中央单位 20、省级单位 30、地(市)级单位',
  `play_type` tinyint(1) DEFAULT '0' COMMENT '引进剧类型:1、引进境外影视剧 2、引进其他境外电视节目 3、以卫星传输方式引进境外其他电视节目',
  `play_name_zh` varchar(255) DEFAULT '' COMMENT '剧名(中文)',
  `play_name_foreign` varchar(255) DEFAULT '' COMMENT '剧名(外文)',
  `play_classification` tinyint(4) DEFAULT '0' COMMENT '类别:10、电视剧 20、电影 30、动画片',
  `language` varchar(100) DEFAULT '' COMMENT '语种',
  `theme_type` tinyint(1) DEFAULT '0' COMMENT '题材:1、都市生活 2、家庭伦理 3、情感偶像 4、古装传奇 5、历史传记 6、动作武打',
  `producing_area` varchar(200) DEFAULT '' COMMENT '产地',
  `standard` tinyint(1) DEFAULT '0' COMMENT ' 制式:1、高清 2、标清',
  `submit_num` int(11) DEFAULT '0' COMMENT '报审集数 单位 45''',
  `all_play_num` int(11) DEFAULT '0' COMMENT '全剧集数 单位 45''',
  `production_org` varchar(100) DEFAULT '' COMMENT '制片机构',
  `feed_org` varchar(100) DEFAULT '' COMMENT '供片机构',
  `fo_contacts` varchar(50) DEFAULT '' COMMENT '供片机构联系人',
  `import_company` varchar(200) DEFAULT '' COMMENT '引进单位',
  `ic_contacts` varchar(50) DEFAULT '' COMMENT '引进单位联系人',
  `submission_company` varchar(200) DEFAULT '' COMMENT '送审单位',
  `sc_contacts` varchar(50) DEFAULT NULL COMMENT '送审单位联系人',
  `state` tinyint(4) NOT NULL DEFAULT '0' COMMENT '审批状态:-1:退回补正  10:审批中 20:审批失败 30:审批完成 ',
  `copyright_path` varchar(255) DEFAULT NULL COMMENT '版权由来路径',
  `copyright_start` varchar(30) DEFAULT NULL COMMENT '版权期限,开始日期',
  `copyright_end` varchar(30) DEFAULT NULL COMMENT '版权期限,结束日期',
  `introduction` text COMMENT '故事梗概',
  `create_by` varchar(50) DEFAULT '' COMMENT '创建人',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_by` varchar(30) DEFAULT NULL COMMENT '修改人',
  `update_time` datetime DEFAULT NULL COMMENT '修改时间',
  PRIMARY KEY (`id`),
  KEY `item_plan_apply_num_index` (`apply_num`),
  KEY `item_plan_play_name_zh_index` (`play_name_zh`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COMMENT='';

插入数据:

INSERT INTO `yj_item_plan` VALUES ('1', '1', '1', '20', '1', '向往的生活', 'xwsh', '10', 'EN', '1', '美国', '1', '10', '45', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '30', '凯哥影视', '2021-01-01', '2021-01-31', '凯哥影视', '凯哥影视', '2021-07-21 17:36:27', '凯哥影视', '2021-07-21 17:36:31');
INSERT INTO `yj_item_plan` VALUES ('2', '1', '1', '20', '1', '向往的生活2', 'xwsh', '10', 'EN', '1', '美国', '1', '10', '45', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '30', '凯哥影视', '2021-01-01', '2021-01-31', '凯哥影视', '凯哥影视', '2021-07-21 17:36:27', '凯哥影视', '2021-07-21 17:36:31');
INSERT INTO `yj_item_plan` VALUES ('3', '1', '1', '20', '1', '未来', 'xwsh', '20', 'EN', '2', '泰国', '1', '10', '30', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '30', '凯哥影视', '2021-01-01', '2021-01-31', '凯哥影视', '凯哥影视', '2021-07-21 17:36:27', '凯哥影视', '2021-07-21 17:36:31');
INSERT INTO `yj_item_plan` VALUES ('4', '1', '1', '20', '1', '小心心', 'xwsh', '10', 'EN', '3', '泰国', '1', '10', '20', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '30', '凯哥影视', '2021-01-01', '2021-01-31', '凯哥影视', '凯哥影视', '2021-07-21 17:36:27', '凯哥影视', '2021-07-21 17:36:31');
INSERT INTO `yj_item_plan` VALUES ('5', '1', '1', '20', '1', '半夜鬼叫', 'xwsh', '10', 'EN', '5', '日本', '1', '10', '60', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '30', '凯哥影视', '2021-01-01', '2021-01-31', '凯哥影视', '凯哥影视', '2021-07-21 17:36:27', '凯哥影视', '2021-07-21 17:36:31');
INSERT INTO `yj_item_plan` VALUES ('6', '1', '1', '20', '1', '中国心', 'xwsh', '10', 'EN', '4', '中国', '1', '10', '55', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '30', '凯哥影视', '2021-01-01', '2021-01-31', '凯哥影视', '凯哥影视', '2021-07-21 17:36:27', '凯哥影视', '2021-07-21 17:36:31');
INSERT INTO `yj_item_plan` VALUES ('7', '1', '1', '20', '1', '在哪里', 'xwsh', '10', 'EN', '6', '美国', '1', '10', '15', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '30', '凯哥影视', '2021-01-01', '2021-01-31', '凯哥影视', '凯哥影视', '2021-07-21 17:36:27', '凯哥影视', '2021-07-21 17:36:31');
INSERT INTO `yj_item_plan` VALUES ('8', '1', '1', '20', '1', '东北大决战', 'xwsh', '10', 'EN', '1', '中国', '1', '10', '25', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '30', '凯哥影视', '2021-01-01', '2021-01-31', '凯哥影视', '凯哥影视', '2021-07-21 17:36:27', '凯哥影视', '2021-07-21 17:36:31');
INSERT INTO `yj_item_plan` VALUES ('9', '1', '1', '20', '1', '哇嘎嘎', 'xwsh', '30', 'EN', '1', '泰国', '1', '10', '65', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '30', '凯哥影视', '2021-01-01', '2021-01-31', '凯哥影视', '凯哥影视', '2021-07-21 17:36:27', '凯哥影视', '2021-07-21 17:36:31');
INSERT INTO `yj_item_plan` VALUES ('10', '1', '1', '20', '1', '向往的生活2', 'xwsh', '10', 'EN', '0', '美国', '1', '10', '30', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '凯哥影视', '30', '凯哥影视', '2021-01-01', '2021-01-31', '凯哥影视', '凯哥影视', '2021-07-23 13:50:07', '凯哥影视', '2021-07-23 13:50:11');

根据上面的分析,我们可以得到,查询sql:

# 题材分布
SELECT t.producing_area,play_classification,t.play_name_zh,

-- 部数
 if(t.play_classification=10,count(distinct t.play_name_zh),0) as '电视剧部数' ,
 if(t.play_classification=20,count(distinct t.play_name_zh),0) as '电影部数' ,
 if(t.play_classification=30,count(distinct t.play_name_zh),0) as '动画片部数',

-- 集数 all_play_num
SUM(CASE when   t.play_classification = 10  then t.submit_num  else 0 end  ) as  '电视剧集数' ,
SUM(CASE when   t.play_classification = 20  then t.submit_num  else 0 end  ) as  '电影集数' ,
SUM(CASE when   t.play_classification = 30  then t.submit_num  else 0 end  ) as  '动画片集数',
#
-- 题材情况
SUM(CASE when t.theme_type = 1  then t.submit_num  else 0 end  ) as  '都市生活' ,
SUM(CASE when t.theme_type = 2  then t.submit_num  else 0 end  ) as '家庭伦理',
SUM(CASE when t.theme_type = 3  then t.submit_num  else 0 end  ) as '情感偶像',
SUM(CASE when t.theme_type = 4  then t.submit_num  else 0 end  ) as '古装传奇',
SUM(CASE when t.theme_type = 5  then t.submit_num  else 0 end  ) as '历史传记',
SUM(CASE when t.theme_type = 6  then t.submit_num  else 0 end  ) as '动作武打'
from yj_item_plan t 
where t.state=30 and t.create_time between '2021-05-22 09:05:10' and '2021-08-29 09:05:10'
group by t.play_name_zh ,t.producing_area ,t.play_classification;

结果:

13156b2dec6d04d2f52a73be386f741b.png

TopTop