使用SQL语句使数据从坚向排列转化成横向排列(排班表)
知识重点:
1.extract(day from schedule01::timestamp)=13
Extract 属于 SQL 的 DML(即数据库管理语言)函数,同样,InterBase 也支持 Extract,它主要用于从一个日期或时间型的字段内抽取年、月、日、时、分、秒数据,因此,它支持其关健字 YEAR、MONTH、DAY、HOUR、MINUTE、SECOND、WEEKDAY、YEARDAY。
Extract 的使用语法为:
EXTRACT(关健字 FROM 日期或时间型字段)
如:extract(year from schedule01)=2017从日期中提取年份
2.max()函数:取最大值
3.case()函数的嵌套
注意嵌套case()函数时,每个case的开始和结束。
排班功能:现有两个人员(A和B),他们在不同日期的值班状态(state)不同,现在要查询他们在2017.6月的值班信息
表结构如下:
CREATE TABLE public.temp_schedule
(
id integer NOT NULL DEFAULT nextval('temp_schedule_id_seq'::regclass),
schedule01 timestamp without time zone,--日期
schedule03 character varying(255),--姓名
state character varying(255),--值班状态(0休 1班)
CONSTRAINT temp_schedule_pkey PRIMARY KEY (id)
)
1.查询SQL语句:
select schedule03,schedule01,state from temp_schedule
where extract(year from schedule01)=2017 and extract(month from schedule01)=6
order by schedule03,schedule01;
显示为:

2.现在需要根据(6月的)日期,从1号开始根据人员名称横向合并排列数据(即只显示两行)
显示效果如下:

实现的SQL语句如下:
select schedule03 as name
,max(case when extract(day from schedule01::timestamp)=1 then state end) as day1
,max(case when extract(day from schedule01::timestamp)=2 then state end) as day2
,max(case when extract(day from schedule01::timestamp)=3 then state end) as day3
,max(case when extract(day from schedule01::timestamp)=4 then state end) as day4
,max(case when extract(day from schedule01::timestamp)=5 then state end) as day5
,max(case when extract(day from schedule01::timestamp)=6 then state end) as day6
,max(case when extract(day from schedule01::timestamp)=7 then state end) as day7
,max(case when extract(day from schedule01::timestamp)=8 then state end) as day8
,max(case when extract(day from schedule01::timestamp)=9 then state end) as day9
,max(case when extract(day from schedule01::timestamp)=10 then state end) as day10
,max(case when extract(day from schedule01::timestamp)=11 then state end) as day11
,max(case when extract(day from schedule01::timestamp)=12 then state end) as day12
,max(case when extract(day from schedule01::timestamp)=13 then state end) as day13
from temp_schedule
where extract(year from schedule01)=2017 and extract(month from schedule01)=6
group by schedule03;
3.将人员的值班状态通过汉字(0休 1班)显示出来,显示效果如下:

SQL语句(主要是实现case的嵌套):
select schedule03 as name
,max(case when extract(day from schedule01::timestamp)=1 then (case when state='0' then '休' else '班' end) end) as day1
,max(case when extract(day from schedule01::timestamp)=2 then (case when state='0' then '休' else '班' end) end) as day2
,max(case when extract(day from schedule01::timestamp)=3 then (case when state='0' then '休' else '班' end) end) as day3
,max(case when extract(day from schedule01::timestamp)=4 then (case when state='0' then '休' else '班' end) end) as day4
,max(case when extract(day from schedule01::timestamp)=5 then (case when state='0' then '休' else '班' end) end) as day5
,max(case when extract(day from schedule01::timestamp)=6 then (case when state='0' then '休' else '班' end) end) as day6
,max(case when extract(day from schedule01::timestamp)=7 then (case when state='0' then '休' else '班' end) end) as day7
,max(case when extract(day from schedule01::timestamp)=8 then (case when state='0' then '休' else '班' end) end) as day8
,max(case when extract(day from schedule01::timestamp)=9 then (case when state='0' then '休' else '班' end) end) as day9
,max(case when extract(day from schedule01::timestamp)=10 then (case when state='0' then '休' else '班' end) end) as day10
,max(case when extract(day from schedule01::timestamp)=11 then (case when state='0' then '休' else '班' end) end) as day11
,max(case when extract(day from schedule01::timestamp)=12 then (case when state='0' then '休' else '班' end) end) as day12
,max(case when extract(day from schedule01::timestamp)=13 then (case when state='0' then '休' else '班' end) end) as day13
from temp_schedule
where extract(year from schedule01)=2017 and extract(month from schedule01)=6
group by schedule03 ;
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
知识一点点的累积,技术一点点的提高!加油!
使用SQL语句使数据从坚向排列转化成横向排列(排班表)的更多相关文章
- (排班表一)使用SQL语句使数据从坚向排列转化成横向排列
知识重点: 1.extract(day from schedule01::timestamp)=13 Extract 属于 SQL 的 DML(即数据库管理语言)函数,同样,InterBase 也支持 ...
- SQL语句之 数据约束
SQL语句之 数据约束 什么是数据约束 数据约束用来限制用户对数据的非法的修改操作. 1.约束字段的默认值 如果插入记录时,没有给某个字段赋值,那么我们可以设置它的默认值 关键字:default CR ...
- PHP如何通过SQL语句将数据写入MySQL数据库呢?
1,php和MySQL建立连接关系 2,打开 3,接受页面数据,PHP录入到指定的表中 1.2两步可直接使用一个数据库链接文件即可:conn.php <?phpmysql_connect(&qu ...
- 通过Sql语句导数据
在通过SQL Server向导中的SQL语句导数据时,默认情况下源表中的nvarchar字段类型会变成202,解决此问题的方法是,要重新选择一下对应的数据接收表.
- 通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷
通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷.效果如下: 步骤1:通过MyEclipse中的window->show View->ot ...
- 使用SQL语句进行数据复制
使用SQL语句对数据或者表进行复制,一般用于两张表结构相同的时候使用. SQL Server中,如果目标表存在: insert into 目标表 select * from 原表; SQL Serve ...
- sql语句编写 有时候一个子查询可以拆分成多个子查询
sql语句编写 有时候一个子查询可以拆分成多个子查询
- sql语句百万数据量优化方案
一:理解sql执行顺序 在sql中,第一个被执行的是from语句,每一个步骤都会产生一个虚拟表,该表供下一个步骤查询时调用,比如语句:select top 10 column1,colum2,max( ...
- <搬运> SQL语句百万数据量优化方案
一:理解sql执行顺序 在sql中,第一个被执行的是from语句,每一个步骤都会产生一个虚拟表,该表供下一个步骤查询时调用,比如语句:select top 10 column1,colum2,max( ...
随机推荐
- GitHub上最受欢迎的iOS开源项目TOP20
AFNetworking 在众多iOS开源项目中,AFNetworking可以称得上是最受开发者欢迎的库项目.AFNetworking是一个轻量级的iOS.Mac OS X网络通信类库,现在是GitH ...
- C++学习笔记1(标准的输入输出)
前言: 个人一直以来比较懒,最近才准备记录一下自己学习C++的学习过程,希望自己能在写博客的时候能够坚持下去,欢迎大家在博客中支出存在的问题,好了不多说了,自己能坚持下去.我准备在我的博客中通过与C语 ...
- Extjs6(一)——用sencha cmd建立一个ExtJs小项目
本文基于ext-6.0.0 一.用sencha cmd建立一个ExtJs小项目 首先,需要一个命令行工具.进入extjs所在目录. 然后,输入:sencha -sdk [ExtJs6.0文件夹地址] ...
- sass学习入门篇(三)
这章我们讲“嵌套”,嵌套包括两种:一,选择器嵌套.二是属性的嵌套.一般用选择器嵌套居多 一,选择器嵌套:指的是在一个选择器中嵌套另一个选择器来实现继承.使用&表示父元素选择器 li{ floa ...
- hust1010 kmp
There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I go ...
- vue组件(将页面公用的头部组件化)
呃……重新捡起前面用vue-cli快速生成的项目. 之前是做过一个多页面的改造,以及引入vux的ui组件,这次在这个项目的基础上,再来聊聊vue中的component. 别问我为啥总是写关于vue的博 ...
- selenium 远程服务设置
第一步:将浏览器的安装地址以及浏览器的驱动地址添加到系统变量path中.浏览器只需要添加此浏览器exe文件所在的目录就可以,驱动需要添加完整的地址包括驱动本身XXX.exe. 第二步:需要安装jdk环 ...
- 深入理解Java常用类----String
Java中字符串的操作可谓是最常见的操作了,String这个类它封装了有关字符串操作的大部分方法,从构建一个字符串对象到对字符串的各种操作都封装在该类中,本篇我们通过阅读String类的源码 ...
- SparkMLib分类算法之朴素贝叶斯分类
SparkMLib分类算法之朴素贝叶斯分类 (一)朴素贝叶斯分类理解 朴素贝叶斯法是基于贝叶斯定理与特征条件独立假设的分类方法.简单来说,朴素贝叶斯分类器假设样本每个特征与其他特征都不相关.举个例子, ...
- 怎么在vue中使用less
最近使用vue2.0重构项目, 使用vue-cli脚手架构建, 采用webpack模板, 要在项目中使用less进行样式的编写 首先, 打开终端, 在当前项目目录下安装less npm install ...