【问题标题】:Difference in dates when actions are taken multiple times多次执行操作的日期差异
【发布时间】:2023-01-12 04:59:11
【问题描述】:

我有下表:

Table (History h)
| Source ID | Action             | Created Date |
|  1        | Filing Rejected    | 1/3/2023     |
|  2        | Filing Rejected    | 1/4/2023     |
|  1        | Filing Resubmitted | 1/5/2023     |
|  3        | Filing Rejected    | 1/5/2023     |
|  2        | Filing Resubmitted | 1/6/2023     |
|  1        | Filing Rejected    | 1/7/2023     |
|  3        | Filing Resubmitted | 1/8/2023     |
|  1        | Filing Resubmitted | 1/9/2023     |

我想要的结果是:

|Source ID | Rejected Date | Resubmitted Date | Difference |
|  1       | 1/3/2023      | 1/5/2023         | 2          |
|  1       | 1/7/2023      | 1/9/2023         | 2          |
|  2       | 1/4/2023      | 1/6/2023         | 2          |   
|  3       | 1/5/2023      | 1/8/2023         | 3          |   
          

我当前的查询语言是:

SELECT h1.Source_ID, min(CONVERT(varchar,h1.CREATED_DATE,101)) AS 'Rejected Date',
       min(CONVERT(varchar,h2.Created_Date,101)) AS 'Resubmitted Date',
       DATEDIFF(HOUR, h1.Created_Date, min(h2.Created_Date)) / 24 Difference
FROM History h1 INNER JOIN History h2
ON h2.Source_ID = h1.Source_ID AND h2.Created_Date > h1.Created_Date
WHERE (h1.Created_Date >= '2023-01-01 00:00:00.000' AND h1.Created_Date <= '2023-01-31 23:59:59.000') 
AND ((h1.CHANGE_VALUE_TO = 'Filing Rejected' AND h2.CHANGE_VALUE_TO = 'Filing Resubmitted'))
GROUP BY h1.Source_ID, h1.Created_Date,h2.Created_Date
ORDER BY 'Rejected Date' ASC;

我得到的结果是:

|Source ID | Rejected Date | Resubmitted Date | Difference |
|  1       | 1/3/2023      | 1/5/2023         | 2          |
|  1 *      | 1/3/2023      | 1/9/2023         | 6          |
|  1       | 1/7/2023      | 1/9/2023         | 2          |
|  2       | 1/4/2023      | 1/6/2023         | 2          |   
|  3       | 1/5/2023      | 1/8/2023         | 3          |   
          

所以有一排不应该出现。我已经用星号标记了它。

我只是想要从第一次拒绝到第一次重投,第二次拒绝到第二次拒绝的区别。

非常感谢任何帮助,关于如何做的另一个想法,任何真的。

【问题讨论】:

  • DISTINCTGROUP BY 始终表示您的查询存在缺陷。 GROUP BY 已经导致您的数据返回清楚的集,所以如果你得到重复项,这可能意味着你的 GROUP BY 是错误的。否则 DISTINCT 是多余的和不需要的开销。
  • 我很尴尬。在删除问题不需要的东西的过程中,我在解决问题的一些失败尝试中留下了。我现在已经取出了独特的。
  • (1) 请用您正在运行的数据库标记您的问题;这是sql服务器吗? (2) 如果连续两次被拒,或者投稿怎么办?

标签: sql sql-server ssms datediff


【解决方案1】:

如果事件总是正确交错,一种方法使用 row_number() 和条件聚合:

select source_id,
    max(case when action = 'Filing Rejected'    then created_date end) rejected_date,
    max(case when action = 'Filing Resubmitted' then created_date end)  resubmitted_date
from (
    select h.*,
        row_number() over(partition by source_id, action order by created_date) rn
    from history h
) h
group by source_id, rn
order by source_id, rn

如果您的数据连续被拒绝或重新提交,这将不起作用。

至于日期差异,我们可以添加另一层,这样我们就不需要输入两次条件表达式:

select h.*, datediff(date, rejected_date, resubmitted_date) diff_in_days
from (
    select source_id,
        max(case when action = 'Filing Rejected'    then created_date end) rejected_date,
        max(case when action = 'Filing Resubmitted' then created_date end)  resubmitted_date
    from (
        select h.*,
            row_number() over(partition by source_id, action order by created_date) rn
        from history h
    ) h
    group by source_id, rn
) h
order by source_id, rn

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多