知识重点:

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. 点击按钮,缩放图片(img.width、img.style.width、img.offsetWidth)

    前几天在慕课网上看到一个关于图片缩放的教学视频,因为当时对老师使用img.width,而不是使用img.style.width而感到奇怪,所以周末得空了,想来自己试着写出来,相关视频网址如下:http ...

  2. Fill-rate, Canvases and input 【译】

    翻译自https://unity3d.com/cn/learn/tutorials/topics/best-practices/fill-rate-canvases-and-input?playlis ...

  3. 不完全图解HTTP

    在2D平面上行走的时候,认识只局限于“点”,刚认识一个新的点,就把之前的那个点忘记了,捡了芝麻丢西瓜.只从3D视角俯瞰时,把这些点连接在一起,点成线,线成面时,才能有所顿悟.话不多说,这是我对HTTP ...

  4. 分布式锁与实现(二)——基于ZooKeeper实现

    引言 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提 ...

  5. vue.js2.0 自定义组件初体验

    理解 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素, Vue.js 的编译器为它添加特殊功能.在有些情况 ...

  6. Could not find a valid gem 'compass' (>= 0) in any repository compass安装失败解决方案

    安装完成ruby gem 之后,通过 gem install compass 安装compass~~ 出现如下报错 Could not find a valid gem 'compass' (> ...

  7. 前端开发需要了解的JS插件

    excanvas.js/Chart.js/cubism.js/d3.js/dc.js/dx.chartjs.js/echarts.js/flot.js 用途:构建数据统计图表,兼容多浏览器 jquer ...

  8. hdu2089 不要62 我的第一个数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 数位DP的入门题,我是根据kuangbin的博客写出来的 思路: dp[i][0],表示长度为i ...

  9. 回锅的美食:JSP+EL+JSTL大杂烩汤

    title: Servlet之JSP tags: [] notebook: javaWEB --- JSP是什么 ? JSP就是Servlet,全名是"JavaServer Pages&qu ...

  10. [刷题]算法竞赛入门经典(第2版) 5-10/UVa1597 - Searching the Web

    题意:不难理解,照搬题意的解法. 代码:(Accepted,0.190s) //UVa1597 - Searching the Web //#define _XIENAOBAN_ #include&l ...