知识重点:

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(),--姓名

   state character varying(),--值班状态(0休 1班)

   CONSTRAINT temp_schedule_pkey PRIMARY KEY (id)

 )

1.查询SQL语句:

 select schedule03,schedule01,state  from temp_schedule

  where extract(year from schedule01)= and extract(month from schedule01)=

  order by  schedule03,schedule01;

显示为:

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

显示效果如下:

实现的SQL语句如下:

 select schedule03 as name

 ,max(case when extract(day from schedule01::timestamp)= then state end) as day1

 ,max(case when extract(day from schedule01::timestamp)= then state end) as day2

 ,max(case when extract(day from schedule01::timestamp)= then state end) as day3

 ,max(case when extract(day from schedule01::timestamp)= then state end) as day4

 ,max(case when extract(day from schedule01::timestamp)= then state end) as day5

 ,max(case when extract(day from schedule01::timestamp)= then state end) as day6

 ,max(case when extract(day from schedule01::timestamp)= then state end) as day7

 ,max(case when extract(day from schedule01::timestamp)= then state end) as day8

 ,max(case when extract(day from schedule01::timestamp)= then state end) as day9

 ,max(case when extract(day from schedule01::timestamp)= then state end) as day10

 ,max(case when extract(day from schedule01::timestamp)= then state end) as day11

 ,max(case when extract(day from schedule01::timestamp)= then state end) as day12

 ,max(case when extract(day from schedule01::timestamp)= then state end) as day13

 from temp_schedule

 where extract(year from schedule01)= and extract(month from schedule01)=

 group by schedule03;

3.将人员的值班状态通过汉字(0休 1班)显示出来,显示效果如下:

SQL语句(主要是实现case的嵌套):

 select schedule03 as name

 ,max(case when extract(day from schedule01::timestamp)= then (case when state='' then '休' else '班' end) end) as day1

 ,max(case when extract(day from schedule01::timestamp)= then (case when state='' then '休' else '班' end) end) as day2

 ,max(case when extract(day from schedule01::timestamp)= then (case when state='' then '休' else '班' end) end) as day3

 ,max(case when extract(day from schedule01::timestamp)= then (case when state='' then '休' else '班' end) end) as day4

 ,max(case when extract(day from schedule01::timestamp)= then (case when state='' then '休' else '班' end) end) as day5

 ,max(case when extract(day from schedule01::timestamp)= then (case when state='' then '休' else '班' end) end) as day6

 ,max(case when extract(day from schedule01::timestamp)= then (case when state='' then '休' else '班' end) end) as day7

 ,max(case when extract(day from schedule01::timestamp)= then (case when state='' then '休' else '班' end) end) as day8

 ,max(case when extract(day from schedule01::timestamp)= then (case when state='' then '休' else '班' end) end) as day9

 ,max(case when extract(day from schedule01::timestamp)= then (case when state='' then '休' else '班' end) end) as day10

 ,max(case when extract(day from schedule01::timestamp)= then (case when state='' then '休' else '班' end) end) as day11

 ,max(case when extract(day from schedule01::timestamp)= then (case when state='' then '休' else '班' end) end) as day12

 ,max(case when extract(day from schedule01::timestamp)= then (case when state='' then '休' else '班' end) end) as day13

 from temp_schedule

 where extract(year from schedule01)= and extract(month from schedule01)=

 group by schedule03 ;

  知识一点点的累积,技术一点点的提高!加油!

(排班表一)使用SQL语句使数据从坚向排列转化成横向排列的更多相关文章

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

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

  2. 用SQL语句将数据表中的数据保存为JSON格式

    没有找到好的工具,只想到了拼字符串的方式,用   NVARCHAR(MAX)  可能有截断,不推荐使用,方法中使用了 FOR XML PATH('') 实现,有关其使用方法参考这里 表结构: SQL ...

  3. Hibernate原生SQL查询多表关联,SQL语句要注意的问题

    Hibernate原生SQL查询多表关联,SQL语句要注意的问题 @for&ever 2009-9-4 系统环境: MySQL5.1 Hibernate3.3 有如下的假定: 实体类 Ques ...

  4. (排班表二)后台动态绘制Grid表格

    后台动态绘制值班表(Grid表格 列名不固定) 要求:表头除了值班人姓名,还要显示日期,及每天的星期值,用斜杠‘/’分隔.即:几号/星期几 最终实现的效果:根据查询的年月显示每个值班人查询月份每天的值 ...

  5. 监控SQL:执行表中所有sql语句、记录每个语句运行时间(3)

    原文:监控SQL:执行表中所有sql语句.记录每个语句运行时间(3) 通过执行一个 带参数的存储过程  exec  OpreateTB('OpreateUser','IsRun')  更新表的数据 表 ...

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

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

  7. SQL语句之 数据约束

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

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

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

  9. 通过Sql语句导数据

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

随机推荐

  1. 夜神模拟器连不上adb的解决办法

    解决办法: 1.任务管理器里看下,adb.exe以及nox_adb.exe这2个进程有没有在运行?有的话就结束掉 2.找到开发环境的SDK的目录和夜神模拟器的目录,将SDK\platform-tool ...

  2. 几种常见的排序算法Java实现总结

    public class MySort { final int MAX=20; int num[]=new int[MAX]; { System.out.print("生成的随机数组是:&q ...

  3. [Android]JsonObject解析

    android和服务器进行交互的时候往往会有数据的传输,而数据中有一种类型就是Json型,这两天在研究API接口的问题,服务器返回的数据类型都是Json型的.例如: 1.接收到的json字符串分为两种 ...

  4. Props 和 IActorRef 3

    在Actor模式中我们使用IActorRef通过ActorSystem来发送消息数据.这么做有两个理由 1.ActorSystem会在消息中封装一些元数据(metadata),这些数据我们可以通过上下 ...

  5. 什么是css sprites,如何使用?

    css sprites:精灵图(雪碧图):把一堆小图片整合在一张大图上,通过背景图片相关设置(背景图片.背景图是否重复.背景图定位),显示图片,减轻服务器对图片的请求数量 优点: 1.减少网页的HTT ...

  6. oracle 递归查询(来源于网络)

    比如 a   b a   c   a   e b   b1 b   b2 c   c1 e   e1 e   e3 d   d1 指定parent=a,选出 a   b a   c   a   e b ...

  7. 时域反射计(TDR)原理与应用

    [施工编辑中...] 1. 什么是TDR? TDR = Time Domain Reflectometry 时域反射计TDR用来测量信号在通过某类传输环境传导时引起的反射,如电路板轨迹.电缆.连接器等 ...

  8. 使用ActiveMQ 传输文件 以及使用Jetty搭建内嵌文件服务器

    使用Active发送文件 ActiveMq 本身提供对于传输文件的支持. 1. 直接传输文件: 使用connection.createOutputStream 的形式.这种方式适合小文件.不能传输大文 ...

  9. npm升级自身

    参考:https://github.com/felixrieseberg/npm-windows-upgrade Usage First, ensure that you can execute sc ...

  10. centos6.5_64bit_tomcat日志合并在一个.log下

    问题   tomcat每次启动时,自动在logs目录下生产以下日志文件,且每天都会生成对应日期的一个文件,造成日志文件众多:   目的        Tomcat以上日志都输出到同一个文件中.   修 ...