如何在 Oracle 中按组填充缺失日期并更改计数值

2024-05-22

这是对我之前发布的问题的轻微修改:如何在 Oracle 中按组填写缺失的日期 https://stackoverflow.com/questions/63002363/how-do-i-fill-in-missing-dates-by-group-in-oracle

我在 Oracle SQL 中有下表:

+-----------+-------+-------+
|   Date    | Color | Count |
+-----------+-------+-------+
| 16-Jan-20 | blue  |     7 |
| 19-Jan-20 | blue  |    32 |
| 23-Jan-20 | blue  |    16 |
| 28-Jan-20 | blue  |    24 |
| 31-Jan-20 | blue  |    41 |
| 2-Feb-20  | blue  |    11 |
| 28-Jan-20 | red   |     1 |
| 3-Feb-20  | red   |     5 |
| 6-Feb-20  | red   |    11 |
| 11-Feb-20 | red   |     4 |
| 15-Feb-20 | red   |     6 |
+-----------+-------+-------+

我想做的是为每一个color,填写缺失的日期从2020年1月1日到2020年2月29日。这样做时,我想使用COUNT列的方式如下:

  1. Get the COUNT for a COLOR以前向填充的方式。例如,在我的表中,缺少 2020 年 1 月 17 日BLUE。我将填写COUNT2020 年 1 月 17 日为 7,因为这是该颜色的最新可用计数。同样,对于 2020 年 1 月 22 日,BLUE,我就填32COUNT.
  2. 如果没有COUNT给定日期之前存在的值,然后填写COUNT最早的值COUNT可用值。例如,2020 年 1 月 2 日,COUNT值将为 7BLUE.

结果表如下:


+-----------+-------+-------+
|   Date    | Color | Count |
+-----------+-------+-------+
| 1-Jan-20  | blue  |     7 |
| 2-Jan-20  | blue  |     7 |
| 3-Jan-20  | blue  |     7 |
| 4-Jan-20  | blue  |     7 |
| 5-Jan-20  | blue  |     7 |
| 6-Jan-20  | blue  |     7 |
| 7-Jan-20  | blue  |     7 |
| 8-Jan-20  | blue  |     7 |
| 9-Jan-20  | blue  |     7 |
| 10-Jan-20 | blue  |     7 |
| 11-Jan-20 | blue  |     7 |
| 12-Jan-20 | blue  |     7 |
| 13-Jan-20 | blue  |     7 |
| 14-Jan-20 | blue  |     7 |
| 15-Jan-20 | blue  |     7 |
| 16-Jan-20 | blue  |     7 |
| 17-Jan-20 | blue  |     7 |
| 18-Jan-20 | blue  |     7 |
| 19-Jan-20 | blue  |    32 |
| 20-Jan-20 | blue  |    32 |
| 21-Jan-20 | blue  |    32 |
| 22-Jan-20 | blue  |    32 |
| 23-Jan-20 | blue  |    16 |
| 24-Jan-20 | blue  |    16 |
| 25-Jan-20 | blue  |    16 |
| 26-Jan-20 | blue  |    16 |
| 27-Jan-20 | blue  |    16 |
| 28-Jan-20 | blue  |    24 |
| 29-Jan-20 | blue  |    24 |
| 30-Jan-20 | blue  |    24 |
| 31-Jan-20 | blue  |    41 |
| 1-Feb-20  | blue  |    41 |
| 2-Feb-20  | blue  |    11 |
| 3-Feb-20  | blue  |    11 |
| 4-Feb-20  | blue  |    11 |
| 5-Feb-20  | blue  |    11 |
| 6-Feb-20  | blue  |    11 |
| 7-Feb-20  | blue  |    11 |
| 8-Feb-20  | blue  |    11 |
| 9-Feb-20  | blue  |    11 |
| 10-Feb-20 | blue  |    11 |
| 11-Feb-20 | blue  |    11 |
| 12-Feb-20 | blue  |    11 |
| 13-Feb-20 | blue  |    11 |
| 14-Feb-20 | blue  |    11 |
| 15-Feb-20 | blue  |    11 |
| 16-Feb-20 | blue  |    11 |
| 17-Feb-20 | blue  |    11 |
| 18-Feb-20 | blue  |    11 |
| 19-Feb-20 | blue  |    11 |
| 20-Feb-20 | blue  |    11 |
| 21-Feb-20 | blue  |    11 |
| 22-Feb-20 | blue  |    11 |
| 23-Feb-20 | blue  |    11 |
| 24-Feb-20 | blue  |    11 |
| 25-Feb-20 | blue  |    11 |
| 26-Feb-20 | blue  |    11 |
| 27-Feb-20 | blue  |    11 |
| 28-Feb-20 | blue  |    11 |
| 29-Feb-20 | blue  |    11 |
| 1-Jan-20  | red   |     1 |
| 2-Jan-20  | red   |     1 |
| 3-Jan-20  | red   |     1 |
| 4-Jan-20  | red   |     1 |
| 5-Jan-20  | red   |     1 |
| 6-Jan-20  | red   |     1 |
| 7-Jan-20  | red   |     1 |
| 8-Jan-20  | red   |     1 |
| 9-Jan-20  | red   |     1 |
| 10-Jan-20 | red   |     1 |
| 11-Jan-20 | red   |     1 |
| 12-Jan-20 | red   |     1 |
| 13-Jan-20 | red   |     1 |
| 14-Jan-20 | red   |     1 |
| 15-Jan-20 | red   |     1 |
| 16-Jan-20 | red   |     1 |
| 17-Jan-20 | red   |     1 |
| 18-Jan-20 | red   |     1 |
| 19-Jan-20 | red   |     1 |
| 20-Jan-20 | red   |     1 |
| 21-Jan-20 | red   |     1 |
| 22-Jan-20 | red   |     1 |
| 23-Jan-20 | red   |     1 |
| 24-Jan-20 | red   |     1 |
| 25-Jan-20 | red   |     1 |
| 26-Jan-20 | red   |     1 |
| 27-Jan-20 | red   |     1 |
| 28-Jan-20 | red   |     1 |
| 29-Jan-20 | red   |     1 |
| 30-Jan-20 | red   |     1 |
| 31-Jan-20 | red   |     1 |
| 1-Feb-20  | red   |     1 |
| 2-Feb-20  | red   |     1 |
| 3-Feb-20  | red   |     5 |
| 4-Feb-20  | red   |     5 |
| 5-Feb-20  | red   |     5 |
| 6-Feb-20  | red   |    11 |
| 7-Feb-20  | red   |    11 |
| 8-Feb-20  | red   |    11 |
| 9-Feb-20  | red   |    11 |
| 10-Feb-20 | red   |    11 |
| 11-Feb-20 | red   |     4 |
| 12-Feb-20 | red   |     4 |
| 13-Feb-20 | red   |     4 |
| 14-Feb-20 | red   |     4 |
| 15-Feb-20 | red   |     6 |
| 16-Feb-20 | red   |     6 |
| 17-Feb-20 | red   |     6 |
| 18-Feb-20 | red   |     6 |
| 19-Feb-20 | red   |     6 |
| 20-Feb-20 | red   |     6 |
| 21-Feb-20 | red   |     6 |
| 22-Feb-20 | red   |     6 |
| 23-Feb-20 | red   |     6 |
| 24-Feb-20 | red   |     6 |
| 25-Feb-20 | red   |     6 |
| 26-Feb-20 | red   |     6 |
| 27-Feb-20 | red   |     6 |
| 28-Feb-20 | red   |     6 |
| 29-Feb-20 | red   |     6 |
+-----------+-------+-------+

请注意,数量不定COLORs. The COLOR_COUNT也会发生难以预料的变化。

有人可以告诉我如何在 Oracle SQL 中执行此操作吗?

任何帮助将不胜感激!

为了方便您使用,下面是在Oracle中生成表的sql查询:

with tbl as (
    select to_date('1/28/2020 09:29', 'MM/DD/YYYY HH24:MI') as color_date, 'red' as color,  1 color_count from dual union
    select to_date('2/3/2020 07:04', 'MM/DD/YYYY HH24:MI') as color_date,  'red' as color,  5 color_count from dual union
    select to_date('2/6/2020 12:11', 'MM/DD/YYYY HH24:MI') as color_date,  'red' as color,  11 color_count from dual union
    select to_date('2/11/2020 17:15', 'MM/DD/YYYY HH24:MI') as color_date,  'red' as color,  4 color_count from dual union
    select to_date('2/15/2020 03:46', 'MM/DD/YYYY HH24:MI') as color_date,  'red' as color,  6 color_count from dual union
    select to_date('1/16/2020 14:52', 'MM/DD/YYYY HH24:MI') as color_date, 'blue' as color, 7 color_count from dual union
    select to_date('1/19/2020 22:30', 'MM/DD/YYYY HH24:MI') as color_date, 'blue' as color, 32 color_count from dual union
    select to_date('1/23/2020 05:17', 'MM/DD/YYYY HH24:MI') as color_date, 'blue' as color, 16 color_count from dual union
    select to_date('1/28/2020 18:35', 'MM/DD/YYYY HH24:MI') as color_date, 'blue' as color, 24 color_count from dual union
    select to_date('1/31/2020 15:38', 'MM/DD/YYYY HH24:MI') as color_date, 'blue' as color, 41 color_count from dual union
    select to_date('2/2/2020 16:01', 'MM/DD/YYYY HH24:MI') as color_date,  'blue' as color, 11 color_count from dual
)
select *
from tbl
order by color, color_date

就是这样:

with tbl as (
    select to_date('1/28/2020 09:29', 'MM/DD/YYYY HH24:MI') as color_date, 'red' as color,  1 color_count from dual union
    select to_date('2/3/2020 07:04', 'MM/DD/YYYY HH24:MI') as color_date,  'red' as color,  5 color_count from dual union
    select to_date('2/6/2020 12:11', 'MM/DD/YYYY HH24:MI') as color_date,  'red' as color,  11 color_count from dual union
    select to_date('2/11/2020 17:15', 'MM/DD/YYYY HH24:MI') as color_date,  'red' as color,  4 color_count from dual union
    select to_date('2/15/2020 03:46', 'MM/DD/YYYY HH24:MI') as color_date,  'red' as color,  6 color_count from dual union
    select to_date('1/16/2020 14:52', 'MM/DD/YYYY HH24:MI') as color_date, 'blue' as color, 7 color_count from dual union
    select to_date('1/19/2020 22:30', 'MM/DD/YYYY HH24:MI') as color_date, 'blue' as color, 32 color_count from dual union
    select to_date('1/23/2020 05:17', 'MM/DD/YYYY HH24:MI') as color_date, 'blue' as color, 16 color_count from dual union
    select to_date('1/28/2020 18:35', 'MM/DD/YYYY HH24:MI') as color_date, 'blue' as color, 24 color_count from dual union
    select to_date('1/31/2020 15:38', 'MM/DD/YYYY HH24:MI') as color_date, 'blue' as color, 41 color_count from dual union
    select to_date('2/2/2020 16:01', 'MM/DD/YYYY HH24:MI') as color_date,  'blue' as color, 11 color_count from dual
)
-- get first and last dates:
,first_record(color_date,color,color_count, end_date) as (
   select
      trunc(min(color_date),'yyyy'), -- 1st of Jan of min(color_date) or use own hardcoded date
      min(color) keep(dense_rank first order by color_date), 
      min(color_count) keep(dense_rank first order by color_date),
      last_day(max(color_date)) as end_date -- last dat of max(color_date) or you can replace it with own hardcoded date
   from tbl
)
-- add them into the data from tbl:
,data as (
   select color_date,color,color_count from first_record
   union
   select * from tbl   
)
-- generating all dates:
,dates_generator as (
    select color_date+N as color_date from first_record, xmltable('0 to 100000' columns N int path '.') x
    where x.n <= end_date-color_date
)
-- simple left join:
select 
   g.color_date
  ,nvl(d.color, lag(color ignore nulls) over(order by g.color_date)) as color
  ,nvl(d.color_count, lag(color_count ignore nulls) over(order by g.color_date)) as color_count
from dates_generator g
     left join data d
          on g.color_date=d.color_date
/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Oracle 中按组填充缺失日期并更改计数值 的相关文章

随机推荐

  • 更新匿名方法内的 ref 参数

    是否有解决方法可以更新匿名方法内的 ref 参数 我知道匿名方法不允许访问外部作用域的 ref 参数 但是还有其他方法可以做到这一点吗 我正在使用 MessageStream 的外部库 因此无法更改委托的参数 void DoWork ref
  • 如何从 Python 中的请求处理程序内部关闭 HTTPServer?

    当我收到 StopIteration 异常时 如何关闭该服务器 sys exit 不起作用 usr bin env python from BaseHTTPServer import BaseHTTPRequestHandler HTTPS
  • Swift 在调试和发布模式下的行为不同

    不确定这是否是 Swift XCode 或 Alamofire 的问题 但我认识到我的混合 Swift Objc 应用程序中不同地方的奇怪行为 它只发生在用 Swift 编写并使用闭包 网络的部分 这是发生这种情况的示例代码 Alamofi
  • StreamWriter 错误字符

    遇到一个问题 streamwriter 在我正在生成的 csv 中生成错误的字符 那些角色 仅出现在文件的开头 5 GEN 555555555 Evan Smith email protected cdn cgi l email prote
  • Pywinauto 如何获取返回元素的列表

    我正在使用具有自定义堆栈面板的 WPF 应用程序 它基本上是一个列表 列表中的项目完全相同 因此我无法选择特定文本来唯一标识元素 其他一些值 例如时间 是动态的 有没有办法让我获得返回的元素列表 我知道这是可能的 因为抛出了错误 Eleme
  • 如何在Python中存储while循环和sentinel的结果?

    我已经为此工作了几个小时 以为我已经把它记下来了 但事实证明我全错了 任务是 编写一个程序来计算该课程的学期平均成绩和字母成绩 用户将输入这些数字 A list测验分数 每个分数的范围为 0 10 用户输入哨兵值 1来结束输入 降低测验的最
  • 通过令牌进行 Firebase CLI 身份验证

    我是按照官方描述的步骤进行的使用 Firebase CLI 的 GitHub 教程 https github com firebase firebase tools blob master README md 命令行 带有 CI 系统 没有
  • Jquery:是否有某种方法可以使 val() 返回空字符串而不是空列表的“未定义”?

    使用 Jquery 是否有某种方法可以使 val 在针对空元素列表调用时返回空字符串而不是 未定义 例如 我有这样的代码 var x my textbox id not watermark val 我的想法是 我想获取文本框的值 但如果它当
  • 将所有 0 值替换为 NA

    我有一个包含一些数字列的数据框 某些行具有 0 值 在统计分析中应将其视为空 在R中将所有0值替换为NULL的最快方法是什么 将所有零替换为 NA df df 0 lt NA 解释 1 它不是NULL你应该用什么来替换零 正如它所说 NUL
  • 打开 SFML 窗口出现内存读取错误

    我试图打开 SFML 窗口 但每次启动时都会显示 访问冲突读取位置 0xCCCCCCC0 错误发生在 init 方法中 相关代码 class AirportGame private sf RenderWindow window public
  • 如何将 r 数据框转换为 h2o 对象

    我对 R 和 H2O 很陌生 我试图找到一种将 r 数据帧转换为 h2o 对象的方法 我花了一些时间研究如何做到这一点 但没有运气 其他方式也是可能的 并且有详细记录如下 prosPath system file extdata prost
  • SQL 分隔符上的逗号分隔列

    这是一个 split 函数 它可以应用为dbo Split sf we fs we 当我将字符串更改为列名时 它不起作用 例如dbo Split table columnName Select from dbo Split email pr
  • Jasmin + karma:“错误:模块‘DynamicTestModule’导入了意外的值‘HttpClient’。请添加 @NgModule 注释。”

    我使用 jasmine 作为测试框架 使用 karma 作为测试运行程序 我正在尝试创建一个 HttpClient 对象 以便我可以创建一个服务作为该对象的依赖项 TestBed configureTestingModule declara
  • 是否可以用任何其他网络字体覆盖“Roboto”字体?

    根据底座样式https mirror api playground appspot com assets css base style css https mirror api playground appspot com assets c
  • Prolog家谱

    我做到了 但没有显示答案 当我询问兄弟姐妹 叔叔 阿姨时 这是我写的 有什么问题吗 uncle X Y male X sibling X Z parent Z Y uncle X Y male X spouse X W sibling W
  • Primefaces 动态覆盖面板仅显示一次

    我有一个带有打开 Primefaces 按钮的表单overlayPanel 面板中还有另一个按钮 用于执行 Ajax 操作 然后关闭覆盖层 这是一个完全没有 Ajax 操作的简化版本
  • 如何在 C# 中应用 XSLT 样式表

    我想使用 C 将 XSLT 样式表应用到 XML 文档 并将输出写入文件 我在这里找到了一个可能的答案 http web archive org web 20130329123237 http www csharpfriends com A
  • MVC5 对象引用未设置为 Scripts.Render 上的对象实例

    我正在使用 Asp net MVC5 自从使用 Microsoft MVC 平台 4 年以来 还没有遇到过这样的模棱两可的错误 错误发生在Manage默认 MVC5 模板中的页面 这里在第 26 行 at the layout cshtml
  • 如何挂载加密的APK扩展文件?

    我尝试以这种方式挂载扩展文件 final StorageManager storageManager StorageManager getSystemService STORAGE SERVICE String obbPath Enviro
  • 如何在 Oracle 中按组填充缺失日期并更改计数值

    这是对我之前发布的问题的轻微修改 如何在 Oracle 中按组填写缺失的日期 https stackoverflow com questions 63002363 how do i fill in missing dates by grou