(排班表一)使用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(),--姓名
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语句使数据从坚向排列转化成横向排列的更多相关文章
- 使用SQL语句使数据从坚向排列转化成横向排列(排班表)
知识重点: 1.extract(day from schedule01::timestamp)=13 Extract 属于 SQL 的 DML(即数据库管理语言)函数,同样,InterBase 也支持 ...
- 用SQL语句将数据表中的数据保存为JSON格式
没有找到好的工具,只想到了拼字符串的方式,用 NVARCHAR(MAX) 可能有截断,不推荐使用,方法中使用了 FOR XML PATH('') 实现,有关其使用方法参考这里 表结构: SQL ...
- Hibernate原生SQL查询多表关联,SQL语句要注意的问题
Hibernate原生SQL查询多表关联,SQL语句要注意的问题 @for&ever 2009-9-4 系统环境: MySQL5.1 Hibernate3.3 有如下的假定: 实体类 Ques ...
- (排班表二)后台动态绘制Grid表格
后台动态绘制值班表(Grid表格 列名不固定) 要求:表头除了值班人姓名,还要显示日期,及每天的星期值,用斜杠‘/’分隔.即:几号/星期几 最终实现的效果:根据查询的年月显示每个值班人查询月份每天的值 ...
- 监控SQL:执行表中所有sql语句、记录每个语句运行时间(3)
原文:监控SQL:执行表中所有sql语句.记录每个语句运行时间(3) 通过执行一个 带参数的存储过程 exec OpreateTB('OpreateUser','IsRun') 更新表的数据 表 ...
- 通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷
通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷.效果如下: 步骤1:通过MyEclipse中的window->show View->ot ...
- 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,解决此问题的方法是,要重新选择一下对应的数据接收表.
随机推荐
- Linux下判断磁盘是SSD还是HDD的几种方法
环境介绍 Fedora release 25 (Twenty Five) 判断方法 方法一 判断cat /sys/block/*/queue/rotational的返回值(其中*为你的硬盘设备名称,例 ...
- 性能测试工具LoadRunner22-LR之Analysis 简介
Analysis功能: 对测试运行结果进行查看.分析和比较 导入分析文件 注意LoadRunner Results文件和Analysis Session Files的区别.LoadRunner Res ...
- 转:POST 400 Bad Request The request sent by the client was syntactically incorrect
最近在做Web开发的时候,使用$.post提交数据,但是回调函数却没有被触发,按F12看控制台输出是:POST *** 400 Bad Request 后台是SpringMVC的,设置了断点也不会被触 ...
- mac os 和 ubuntu 上测试工具check-0.9.10的安装
由于工作需要,要使用check 这个单元测试工具. 首先,说一说在Mac10.9上面的安装.我是直接在官网(http://check.sourceforge.net)上下载源码包. 1,解压 2,进入 ...
- Window WindowManager 和WindowManager.LayoutParams
<一> Window window是android中的窗口,表示顶级窗口的意思,也就是主窗口,它有两个实现类, PhoneWindow和MidWindow,我们一般的activity对应的 ...
- System.Net.Mail
System.Net.Mail命名空间包含用于将电子邮件发送到简单邮件传输协议(SMTP)服务器进行传送的类. 在此命名空间中,有两个很重要的类: MailMessage 表示可以使用SmtpCli ...
- js获取日期:前天、昨天、今天、明天、后天
前天,昨天,今天,明天,后天 <html> <head> <meta http-equiv="Content-Type" content=" ...
- git合并分支上的commit为一条commit到master
标签: git 缘由? 有一次被人问到怎么把一个分支的所有commit按一个commit合并到主分支上,当时一脸蒙B,平时开发都是直接merge,很少考虑到这种问题,于是特意搜索了相关资料. 场景 其 ...
- RNQOJ [stupid]愚蠢的矿工(树形依赖背包)
题意 题目链接 Sol 树形依赖背包板子题 树形依赖背包大概就是说:对于一个点,只有选了它的父亲才能选自身 把dfs序建出来,倒过来考虑 设\(f[i][j]\)表示从第\(i\)个节点往后背包体积为 ...
- window下隐藏apache版本和PHP脚本等敏感信息
隐藏Apache信息 1.1 主配置中启用httpd-default.conf 文件: conf/httpd.Conf 找到httpd-default.conf,删除前面的注释“#”,改成如下 Inc ...