知识重点:

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语句使数据从坚向排列转化成横向排列(排班表)的更多相关文章

  1. (排班表一)使用SQL语句使数据从坚向排列转化成横向排列

    知识重点: 1.extract(day from schedule01::timestamp)=13 Extract 属于 SQL 的 DML(即数据库管理语言)函数,同样,InterBase 也支持 ...

  2. SQL语句之 数据约束

    SQL语句之 数据约束 什么是数据约束 数据约束用来限制用户对数据的非法的修改操作. 1.约束字段的默认值 如果插入记录时,没有给某个字段赋值,那么我们可以设置它的默认值 关键字:default CR ...

  3. PHP如何通过SQL语句将数据写入MySQL数据库呢?

    1,php和MySQL建立连接关系 2,打开 3,接受页面数据,PHP录入到指定的表中 1.2两步可直接使用一个数据库链接文件即可:conn.php <?phpmysql_connect(&qu ...

  4. 通过Sql语句导数据

    在通过SQL Server向导中的SQL语句导数据时,默认情况下源表中的nvarchar字段类型会变成202,解决此问题的方法是,要重新选择一下对应的数据接收表.

  5. 通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷

    通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷.效果如下:     步骤1:通过MyEclipse中的window->show View->ot ...

  6. 使用SQL语句进行数据复制

    使用SQL语句对数据或者表进行复制,一般用于两张表结构相同的时候使用. SQL Server中,如果目标表存在: insert into 目标表 select * from 原表; SQL Serve ...

  7. sql语句编写 有时候一个子查询可以拆分成多个子查询

    sql语句编写 有时候一个子查询可以拆分成多个子查询

  8. sql语句百万数据量优化方案

    一:理解sql执行顺序 在sql中,第一个被执行的是from语句,每一个步骤都会产生一个虚拟表,该表供下一个步骤查询时调用,比如语句:select top 10 column1,colum2,max( ...

  9. <搬运> SQL语句百万数据量优化方案

    一:理解sql执行顺序 在sql中,第一个被执行的是from语句,每一个步骤都会产生一个虚拟表,该表供下一个步骤查询时调用,比如语句:select top 10 column1,colum2,max( ...

随机推荐

  1. C#小知识点记录(QQ交流群的一个小问题)Linq提取数据

    请教 这里 LINQ想 找到 最后的 4条 记录 然后放在 这里这个 List Linq查找怎么写呀? 解答:写了一个小例子作为解答. namespace C_Sharp { class Progra ...

  2. 内嵌的Component调用外部的方法

    如果一个内嵌的Component控件需要调用外部定义的方法,用outerDocument.方法名来调用,前提是该方法是public的.如:<mx:DataGridColumn headerTex ...

  3. EZChart - 在线图表生成器

    朋友写材料时,需要用到一些分析图表,嫌Excel的太丑,就为他写了一个在线图表生成器. 纯静态实现,基于:H5 + Bootstrap + FusionCharts 本地存储使用H5的localSto ...

  4. bzoj2876 [Noi2012]骑行川藏

    Description 蛋蛋非常热衷于挑战自我,今年暑假他准备沿川藏线骑着自行车从成都前往拉萨.川藏线的沿途有着非常美丽的风景,但在这一路上也有着很多的艰难险阻,路况变化多端,而蛋蛋的体力十分有限,因 ...

  5. 关于div+css布局值得注意的地方

    注意项 我们知道,如果想要两个 div(即块级元素)挨着一起排列,可以将其设置为inline-block(行内-块元素). 不过要注意两个div内的内容的对齐方式将是垂直中间对齐,所以这时候就需要使用 ...

  6. JMS学习篇《一》ActiveMQ消息中间件的简单介绍与用法-概念篇

    原创说明:本篇博文为本人原创作品,转载请注明出处 1.何为消息中间件 消息中间件是一种在分布式应用中互相交换信息的一种技术,常见的成熟消息中间件有:RabbitMQ.SonicMQ,activeMQ. ...

  7. iOS关于Cookie验证登录状态

    1.第一次进入应用,登录获取Cookie,此时如果用到的是AFN去获取接口数据,Cookie已经写入了,所以无需处理,每次请求的时候,会自动将该cookie传给后台去验证 2.将Cookie缓存到本地 ...

  8. ListView控件详解

    ListView是个较为复杂的控件   1.定义 把它拽进来,系统会自动在Designer.cs里添加一个  this.listView1 = new System.Windows.Forms.Lis ...

  9. Hive篇之安装

    1,安装 hive的版本的选择,是选择内置的数据库保存元数据,还是用外部的mysql之类的数据库保存元数据,同时,如果使用外置的mysql,需要注意对mysql远程访问的配置. 再就是关于文件的配置了 ...

  10. HDU4686——Arc of Dream矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4686 题目大意: 已知a0=A0, ai=Ax*ai-1+Ay; b0=B0, bi=Bx*bi-1 ...