使用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( ...
随机推荐
- 【转载】stm32定时器-----珍藏版
今天看到一个讲解定时器特别细致入微的文章,真是难得... 原文地址:http://www.cnblogs.com/zjvskn/p/5751591.html 一.STM32通用定时器原理 STM32 ...
- SELECT中的多表连接
MySQL多表连接查询 连接(join):将一张表中的行按照某个条件(连接条件)和另一张表中的行连接起来形成一个新行的过程. 根据连接查询返回的结果,分3类: 内连接(inner join) 外连接( ...
- HTTP笔记
"你知道当我们在网页浏览器(Web browser)的地址栏中输入 URL 时,Web 页面是如何呈现的吗?" HTTP协议 HTTP协议(HyperText Transfer P ...
- C语言学习第六章
今天开始尝试改变! 今天要学习函数,一个C语言中的重要组成部分. 首先先聊聊为什么要使用函数?随着学习的深入很多人会发现某段的代码重复使用的几率很大,而如果用一次写一次的话很明显的效率就会比较低,如果 ...
- lxml简明教程
最近要做下微信爬虫,之前写个小东西都是直接用正则提取数据就算了,如果需要更稳定的提取数据,还是使用 xpath 定位元素比较可靠.周末没事,从爬虫的角度研究了一下 python xml 相关的库. P ...
- PRML读书笔记——机器学习导论
什么是模式识别(Pattern Recognition)? 按照Bishop的定义,模式识别就是用机器学习的算法从数据中挖掘出有用的pattern. 人们很早就开始学习如何从大量的数据中发现隐藏在背后 ...
- System.Data.SqlClient.SqlException (0x80131904): EXECUTE 后的事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配。上一计数 = 1,当前计数 = 0。 EXECUTE 后的事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配。上一计数 = 1,当前计数 = 0。
EF使用ExecuteSqlCommand(db.Database.ExecuteSqlCommand("exec proc_DeleteCaseInfo_Output @caseID&qu ...
- Android获取状态栏高度、标题栏高度、编辑区域高度
一.Activity界面的划分 简单说明一下(上图Activity采用默认Style,状态栏和标题栏都会显示):最大的草绿色区域是屏幕界面,红色次大区域我们称之为"应用程序界面区域" ...
- 【树莓派】为树莓派配置或扩展swap分区
---恢复内容开始--- 由于树莓派3的默认内存只有1G,而应用程序运行过程中,存在大量的IO读写,以及网络转换,内存交换等.这样,也有很多buffer.cache资源占用等,很快就会接近1GB,最终 ...
- 关于股票最佳买卖时机的lintcode代码
class Solution {public: /** * @param prices: Given an integer array * @return: Maximum pr ...