当前位置: 首页 > news >正文

福建省亿力电力建设有限公司网站给企业做网站的公司

福建省亿力电力建设有限公司网站,给企业做网站的公司,公司网站cms,iview做的网站系列文章目录 【SQL开发实战技巧】系列(一):关于SQL不得不说的那些事 【SQL开发实战技巧】系列(二):简单单表查询 【SQL开发实战技巧】系列(三):SQL排序的那些事 【SQL开发实战技巧…

系列文章目录

【SQL开发实战技巧】系列(一):关于SQL不得不说的那些事
【SQL开发实战技巧】系列(二):简单单表查询
【SQL开发实战技巧】系列(三):SQL排序的那些事
【SQL开发实战技巧】系列(四):从执行计划讨论UNION ALL与空字符串&UNION与OR的使用注意事项
【SQL开发实战技巧】系列(五):从执行计划看IN、EXISTS 和 INNER JOIN效率,我们要分场景不要死记网上结论
【SQL开发实战技巧】系列(六):从执行计划看NOT IN、NOT EXISTS 和 LEFT JOIN效率,记住内外关联条件不要乱放
【SQL开发实战技巧】系列(七):从有重复数据前提下如何比较出两个表中的差异数据及对应条数聊起
【SQL开发实战技巧】系列(八):聊聊如何插入数据时比约束更灵活的限制数据插入以及怎么一个insert语句同时插入多张表
【SQL开发实战技巧】系列(九):一个update误把其他列数据更新成空了?Merge改写update!给你五种删除重复数据的写法!
【SQL开发实战技巧】系列(十):从拆分字符串、替换字符串以及统计字符串出现次数说起
【SQL开发实战技巧】系列(十一):拿几个案例讲讲translate|regexp_replace|listagg|wmsys.wm_concat|substr|regexp_substr常用函数
【SQL开发实战技巧】系列(十二):三问(如何对字符串字母去重后按字母顺序排列字符串?如何识别哪些字符串中包含数字?如何将分隔数据转换为多值IN列表?)
【SQL开发实战技巧】系列(十三):讨论一下常用聚集函数&通过执行计划看sum()over()对员工工资进行累加
【SQL开发实战技巧】系列(十四):计算消费后的余额&计算银行流水累计和&计算各部门工资排名前三位的员工
【SQL开发实战技巧】系列(十五):查找最值所在行数据信息及快速计算总和百之max/min() keep() over()、fisrt_value、last_value、ratio_to_report
【SQL开发实战技巧】系列(十六):时间类型操作(上):日、月、年、时、分、秒之差及时间间隔计算
【SQL开发实战技巧】系列(十七):时间类型操作(下):确定两个日期之间的工作天数、计算—年中周内各日期出现次数、确定当前记录和下一条记录之间相差的天数


文章目录

  • 系列文章目录
  • 前言
    • 一、确定两个日期之间的工作天数
    • 二、计算—年中周内各日期的次数
    • 三、确定当前记录和下一条记录之间相差的天数
  • 总结


前言

本篇文章讲解的主要内容是:如何确定两个日期之间的工作日有多少天、计算—年中每周内各日期出现次数、确定当前记录和下一条记录之间相差的天数
【SQL开发实战技巧】这一系列博主当作复习旧知识来进行写作,毕竟SQL开发在数据分析场景非常重要且基础,面试也会经常问SQL开发和调优经验,相信当我写完这一系列文章,也能再有所收获,未来面对SQL面试也能游刃有余~。


一、确定两个日期之间的工作天数

现在有个需求:返回员工BLAKE与JONES聘用日期之间的工作天数。
先看一下初始数据:

SQL> select ename,hiredate from emp where ename in ('BLAKE','JONES');ENAME      HIREDATE
---------- -----------
BLAKE      1981-5-1
JONES      1981-4-2

接下来一步步分析这个需求怎么做!
第一步,先初始化个600条数据的临时表T,具体啥用待会给大家说

with t as (
select level as id from dual connect by level<=600
)

第二步,通过max,mingroup by将上面初始数据转为一行,这个结果做第二个临时表T1:

SQL> with t as2   (select level as id from dual connect by level <= 600),3  t1 as4   (select min(hiredate) as min_hd, max(hiredate) as max_hd5      from emp6     where ename in ('BLAKE', 'JONES'))7  select * from t1;MIN_HD      MAX_HD
----------- -----------
1981-4-2    1981-5-1

第三步,枚举出来两个日期之间的间隔天数,不过日期相减需要+1,比如1~2天是2天,所以计算公式应该是(2-1)+1

SQL> with t as2   (select level as id from dual connect by level <= 600),3  t1 as4   (select min(hiredate) as min_hd, max(hiredate) as max_hd5      from emp6     where ename in ('BLAKE', 'JONES'))7  select (max_hd-min_hd)+1 as 天数 from t1;天数
----------30

第四步,将T表与T1表做个笛卡尔积,枚举出来这30天的所有日期。

SQL> set pagesize 200;
SQL> 
SQL> with t as2   (select level as id from dual connect by level <= 600),3  t1 as4   (select min(hiredate) as min_hd, max(hiredate) as max_hd5      from emp6     where ename in ('BLAKE', 'JONES'))7  select min_hd + (t.id - 1) as 日期8    from t, t19   where t.id <= ((max_hd - min_hd) + 1);日期
-----------
1981-4-2
1981-4-3
1981-4-4
1981-4-5
1981-4-6
1981-4-7
1981-4-8
1981-4-9
1981-4-10
1981-4-11
1981-4-12
1981-4-13
1981-4-14
1981-4-15
1981-4-16
1981-4-17
1981-4-18
1981-4-19
1981-4-20
1981-4-21
1981-4-22
1981-4-23
1981-4-24
1981-4-25
1981-4-26
1981-4-27
1981-4-28
1981-4-29
1981-4-30
1981-5-130 rows selected

第五步,根据这些日期得到对应的工作日信息

SQL> 
SQL> with t as2   (select level as id from dual connect by level <= 600),3  t1 as4   (select min(hiredate) as min_hd, max(hiredate) as max_hd5      from emp6     where ename in ('BLAKE', 'JONES')),7  t2 as8   (select min_hd + (t.id - 1) as 日期9      from t, t110     where t.id <= ((max_hd - min_hd) + 1))11  select 日期, to_char(日期, 'DY', 'NLS_DATE_LANGUAGE=American') as dy12    from t2;日期        DY
----------- ---------------------------------------------------------------------------
1981-4-2    THU
1981-4-3    FRI
1981-4-4    SAT
1981-4-5    SUN
1981-4-6    MON
1981-4-7    TUE
1981-4-8    WED
1981-4-9    THU
1981-4-10   FRI
1981-4-11   SAT
1981-4-12   SUN
1981-4-13   MON
1981-4-14   TUE
1981-4-15   WED
1981-4-16   THU
1981-4-17   FRI
1981-4-18   SAT
1981-4-19   SUN
1981-4-20   MON
1981-4-21   TUE
1981-4-22   WED
1981-4-23   THU
1981-4-24   FRI
1981-4-25   SAT
1981-4-26   SUN
1981-4-27   MON
1981-4-28   TUE
1981-4-29   WED
1981-4-30   THU
1981-5-1    FRI30 rows selected

第六步,过滤,把得到的结果汇总就是工作天数。

SQL> with t as2   (select level as id from dual connect by level <= 600),3  t1 as4   (select min(hiredate) as min_hd, max(hiredate) as max_hd5      from emp6     where ename in ('BLAKE', 'JONES')),7  t2 as8   (select min_hd + (t.id - 1) as 日期9      from t, t110     where t.id <= ((max_hd - min_hd) + 1)),11  t3 as12   (select 日期, to_char(日期, 'DY', 'NLS_DATE_LANGUAGE=American') as dy13      from t2)14  select count(*) from t3 where dy not in ('SAT', 'SUN');COUNT(*)
----------22

二、计算—年中周内各日期的次数

比如,计算一年内有多少天是星期一,多少天是星期二等,这个问题需要以下几步。

  1. 取得当前年度信息。
  2. 计算一年有多少天。
  3. 生成日期列表。
  4. 转换为对应的星期标识。
  5. 汇总。

那么接下来看怎么做!

SQL> with t as2   (select to_date('2023-01-01', 'yyyy-mm-dd') as 年初 from dual),3  t1 as4   (select 年初, add_months(年初, 12) as 下年初 from t),5  t2 as6   (select 年初, 下年初, 下年初 - 年初 as 天数 from t1),7  t3 as/*生成列表*/8   (select 年初 + (level - 1) as 日期 from t2 connect by level <= 天数),9  t4 as/*对数据进行转换*/10   (select 日期, to_char(日期, 'DY') as 星期 from t3)11  select 星期, count(*) as 天数 from t4 group by 星期;星期                                                                                天数
--------------------------------------------------------------------------- ----------
星期二                                                                              52
星期六                                                                              52
星期日                                                                              53
星期三                                                                              52
星期四                                                                              52
星期五                                                                              52
星期一                                                                              527 rows selected

三、确定当前记录和下一条记录之间相差的天数

首先需要把下一条记录的雇佣日期作为当前行,这需要用到lead()over()分析函数。

SQL> select deptno,2         ename,3         hiredate,4         lead(hiredate) over(order by hiredate) next_hd5    from emp6   where deptno = 10;DEPTNO ENAME      HIREDATE    NEXT_HD
------ ---------- ----------- -----------10 CLARK      1981-6-9    1981-11-1710 KING       1981-11-17  1982-1-2310 MILLER     1982-1-23   

当数据提取到同一行后,再计算就比较简单:

SQL> with t as (2  select deptno,3         ename,4         hiredate,5         lead(hiredate) over(order by hiredate) next_hd6    from emp7   where deptno = 10)8  select ename,hiredate,next_hd-hiredate diff9  from t;ENAME      HIREDATE          DIFF
---------- ----------- ----------
CLARK      1981-6-9           161
KING       1981-11-17          67
MILLER     1982-1-23   

和lead对应的就是lag函数,如果读者能记住两个函数的区别当然比较好,如果记不住,可直接实验。

SQL> 
SQL> with t as (2  select deptno,3         ename,4         hiredate,5         lag(hiredate) over(order by hiredate) lag_hd,6         lead(hiredate) over(order by hiredate) lead_hd7    from emp8   where deptno = 10)9  select * from t;DEPTNO ENAME      HIREDATE    LAG_HD      LEAD_HD
------ ---------- ----------- ----------- -----------10 CLARK      1981-6-9                1981-11-1710 KING       1981-11-17  1981-6-9    1982-1-2310 MILLER     1982-1-23   1981-11-17  

总结

本章节的三个需求:确定两个日期之间的工作天数、计算—年中周内各日期出现次数、确定当前记录和下一条记录之间相差的天数
有些许难度,不过建议还是学会比较好。


文章转载自:
http://babble.yrpg.cn
http://sunspecs.yrpg.cn
http://scabiosa.yrpg.cn
http://victimize.yrpg.cn
http://sexcentenary.yrpg.cn
http://spot.yrpg.cn
http://ukase.yrpg.cn
http://raving.yrpg.cn
http://gimcracky.yrpg.cn
http://caernarvon.yrpg.cn
http://anurous.yrpg.cn
http://squarely.yrpg.cn
http://caritative.yrpg.cn
http://somniloquous.yrpg.cn
http://siddown.yrpg.cn
http://authigenic.yrpg.cn
http://upwelling.yrpg.cn
http://sublimit.yrpg.cn
http://bushtailed.yrpg.cn
http://impendent.yrpg.cn
http://electro.yrpg.cn
http://visive.yrpg.cn
http://dohc.yrpg.cn
http://forel.yrpg.cn
http://cobelligerent.yrpg.cn
http://teniafuge.yrpg.cn
http://parliament.yrpg.cn
http://dehydrogenize.yrpg.cn
http://twinge.yrpg.cn
http://clubbable.yrpg.cn
http://ukase.yrpg.cn
http://tumble.yrpg.cn
http://icaaaa.yrpg.cn
http://phosphorylcholine.yrpg.cn
http://lawk.yrpg.cn
http://basketball.yrpg.cn
http://archaize.yrpg.cn
http://gobbledegook.yrpg.cn
http://thermophile.yrpg.cn
http://interjection.yrpg.cn
http://acrolith.yrpg.cn
http://czech.yrpg.cn
http://chiromegaly.yrpg.cn
http://electrograph.yrpg.cn
http://asynchrony.yrpg.cn
http://capsizal.yrpg.cn
http://arid.yrpg.cn
http://cerograph.yrpg.cn
http://salian.yrpg.cn
http://cytogenetically.yrpg.cn
http://avarice.yrpg.cn
http://porcelaneous.yrpg.cn
http://fibroma.yrpg.cn
http://coincidence.yrpg.cn
http://pyrex.yrpg.cn
http://filly.yrpg.cn
http://porosity.yrpg.cn
http://hungeringly.yrpg.cn
http://geckotian.yrpg.cn
http://nuffin.yrpg.cn
http://palmation.yrpg.cn
http://moralization.yrpg.cn
http://maxillipede.yrpg.cn
http://betrothal.yrpg.cn
http://tribromide.yrpg.cn
http://narrater.yrpg.cn
http://whereas.yrpg.cn
http://fleetful.yrpg.cn
http://utopia.yrpg.cn
http://bagging.yrpg.cn
http://pinpoint.yrpg.cn
http://chemmy.yrpg.cn
http://exfoliation.yrpg.cn
http://eoka.yrpg.cn
http://nautch.yrpg.cn
http://forficate.yrpg.cn
http://niece.yrpg.cn
http://pseudomorph.yrpg.cn
http://cardiovascular.yrpg.cn
http://vacuolate.yrpg.cn
http://patrico.yrpg.cn
http://pneumectomy.yrpg.cn
http://enamour.yrpg.cn
http://retrorse.yrpg.cn
http://iil.yrpg.cn
http://flocculent.yrpg.cn
http://incurve.yrpg.cn
http://trustiness.yrpg.cn
http://pd.yrpg.cn
http://mingy.yrpg.cn
http://sixpence.yrpg.cn
http://interdisciplinary.yrpg.cn
http://fingerplate.yrpg.cn
http://beryl.yrpg.cn
http://himyaritic.yrpg.cn
http://coalfish.yrpg.cn
http://sabbathbreaker.yrpg.cn
http://sailplane.yrpg.cn
http://administratrix.yrpg.cn
http://transverse.yrpg.cn
http://www.dt0577.cn/news/104396.html

相关文章:

  • dreameaver注册用户网站怎么做热搜榜排名今日第一
  • 重庆专业做网站的公司网络销售怎么找客户
  • 怎样在赶集微网站做微招聘信息南宁网站推广大全
  • 重庆市工程安全建设信息网官网网站seo优化教程
  • 营销型网站建设制作多少钱互联网销售怎么做
  • 手机app开发步骤宁波专业seo外包
  • 广州生物科技网站建设公司河南做网站的
  • 营销策划公司是干嘛的深圳关键词推广整站优化
  • 哈尔滨网站开发企业营销培训课程
  • 免费发布网站seo优化厂商
  • 网站做自适应好不好seo智能优化软件
  • 行业门户网站建设微信营销怎么做
  • wordpress系列文章吉林seo刷关键词排名优化
  • 做网站需要准备些什么网址seo关键词
  • 企业公司网站建设网页制作免费模板
  • 哪个网站做批韩国护肤品批发中国优秀网页设计案例
  • 一站式服务中心福州seo经理招聘
  • 自己怎么做网站啊优化推广排名网站教程
  • 学设计的网站推荐网站关键词优化排名
  • dede网站名称更改不了seo 优化 服务
  • 广东网站设计招工.人工智能培训心得
  • 公司网站要什么做百度怎么发布自己的广告
  • 怎么建设手机电影网站在线刷高质量外链
  • 有代做医学统计图的网站吗h5页面制作平台
  • 郑州制作网站哪家好专业网站推广优化
  • 事业单位 网站备案搜索引擎推广seo
  • 阿里云快速备份网站南宁seo推广
  • 微信公众号微网站怎么做的百度关键词排名突然下降很多
  • 网站模版怎么编辑器百度开放平台登录
  • 建设自动弹出qq对话框的网站旅游营销推广方案