首页 \ 问答 \ 是否有一个很好的SQL方法来汇总带有日期范围块作为输入的表?(Is there a good SQL approach for summarizing a table with blocks of date ranges as input?)

是否有一个很好的SQL方法来汇总带有日期范围块作为输入的表?(Is there a good SQL approach for summarizing a table with blocks of date ranges as input?)

有没有人有一个很好的方法来从包含开始和停止日期给出的时间间隔的表中生成每日摘要(其中空止点意味着无限期地继续)?

描述问题的最佳方式是一个例子。 想象一下,餐馆工人的数据库跟踪时间表,雇员经常被雇用和解雇。 有时员工可能会回来重新雇用。

给出每个员工一行的表格,如下所示:

position,name,start,end
bottle washer,Fred,1/2/2013,1/5/2013
bottle washer,Barney,1/4/2013,1/7/2013
bottle washer,Betty,1/10/2013,
bottle washer,Wilma,1/12/2013,1/13/2013
cook,Bilbo,1/1/2013,1/3/2013
cook,Frodo,1/5/2013,1/8/2013
cook,Bilbo,1/7/2013

我正在寻找一个查询,每天/每个位置产生一行,给出当天的人员配备水平,如:

position,date,staffing
bottle washer,1/1/2013,0
bottle washer,1/2/2013,1
bottle washer,1/3/2013,1
bottle washer,1/4/2013,2
bottle washer,1/5/2013,2
bottle washer,1/6/2013,1
bottle washer,1/7/2013,1
bottle washer,1/8/2013,0
bottle washer,1/9/2013,0
bottle washer,1/10/2013,1
bottle washer,1/11/2013,1
bottle washer,1/12/2013,2
bottle washer,1/13/2013,2
bottle washer,1/14/2013,1
bottle washer,1/15/2013,1
cook,1/1/2013,1
cook,1/2/2013,1
cook,1/3/2013,1
cook,1/4/2013,0
cook,1/5/2013,1
cook,1/6/2013,1
cook,1/7/2013,2
cook,1/8/2013,2
cook,1/9/2013,1
cook,1/10/2013,1
cook,1/11/2013,1
cook,1/12/2013,1
cook,1/13/2013,1
cook,1/14/2013,1
cook,1/15/2013,1

我意识到我可以通过在客户端编写过程代码或在服务器端编写存储过程来解决这个问题,但它会简化很多事情以使用单个SQL查询。 如果它是一个复杂的查询或使用窗口或分析函数,那就没问题了。 目前,如果重要的话,我们正在使用Oracle。

关于好方法的任何建议?


Does anyone have a good approach for generating a daily summary from a table containing intervals of times given by start and stop dates (where a null stop means continues indefinitely)?

The best way to describe the problem is with an example. Imagine a database tracking schedules for workers at a restaurant, where employees are hired and fired frequently. Sometimes employees might come back and get rehired.

Given a table with one row for each employee like the following:

position,name,start,end
bottle washer,Fred,1/2/2013,1/5/2013
bottle washer,Barney,1/4/2013,1/7/2013
bottle washer,Betty,1/10/2013,
bottle washer,Wilma,1/12/2013,1/13/2013
cook,Bilbo,1/1/2013,1/3/2013
cook,Frodo,1/5/2013,1/8/2013
cook,Bilbo,1/7/2013

I'm looking for a query that produces one row for each day/position giving the staffing level for that day, as in:

position,date,staffing
bottle washer,1/1/2013,0
bottle washer,1/2/2013,1
bottle washer,1/3/2013,1
bottle washer,1/4/2013,2
bottle washer,1/5/2013,2
bottle washer,1/6/2013,1
bottle washer,1/7/2013,1
bottle washer,1/8/2013,0
bottle washer,1/9/2013,0
bottle washer,1/10/2013,1
bottle washer,1/11/2013,1
bottle washer,1/12/2013,2
bottle washer,1/13/2013,2
bottle washer,1/14/2013,1
bottle washer,1/15/2013,1
cook,1/1/2013,1
cook,1/2/2013,1
cook,1/3/2013,1
cook,1/4/2013,0
cook,1/5/2013,1
cook,1/6/2013,1
cook,1/7/2013,2
cook,1/8/2013,2
cook,1/9/2013,1
cook,1/10/2013,1
cook,1/11/2013,1
cook,1/12/2013,1
cook,1/13/2013,1
cook,1/14/2013,1
cook,1/15/2013,1

I realize that I could solve this problem by writing procedural code on the client side or a stored procedure on the server side, but it would simplify alot of things to use a single SQL query. It would be fine if it were a complicated query or if it used windowing or analytic functions. At the moment, we're using Oracle if that matters.

Any suggestions on good approaches?


原文:https://stackoverflow.com/questions/21567199
更新时间:2023-01-08 08:01

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。