2

我写了以下查询,不幸的是它不起作用。我想要实现的只是我为每一行选择的日期是客户关系的生效日期。

ci_per_per表包含以下列:

per_id1, per_id2, start_dt,end_dt

per_id1是父母,per_id2是孩子。

start_dtend_dt定义关系的持续时间

SELECT
  CONNECT_BY_ROOT per_id2                                                           AS per_id2,
  per_id1,
  (CASE WHEN start_dt < (PRIOR start_dt1)
            THEN (PRIOR start_dt1) 
            ELSE start_dt 
        END) AS start_dt1,
  (CASE WHEN end_dt > (PRIOR end_dt1) 
             THEN (PRIOR end_dt1) 
             ELSE end_dt 
        END)         AS end_dt1,
  level                                                                             AS "PER_LEVEL"
  FROM ci_per_per
  CONNECT BY NOCYCLE PRIOR per_id1 = per_id2

我得到的 sql 错误是:END_DT1: Invalid identifier.

有没有办法在 oracle 中实现这个功能?

示例场景:

per_id1 | per_id2 |   start_dt  |   end_dt
-------------------------------------------
B       |   A     |  01-01-2011 | 01-01-2011
C       |   B     |  01-01-2010 | 01-01-2010
E       |   B     |  01-01-2011 | 01-01-2014
D       |   C     |  01-01-2009 | 01-01-2015

现在我希望所有记录都01-01-2011作为他们的开始日期。但是,如果层次结构颠倒(意味着交换 per_id_1 和 Per_id2 值),则记录应按原样显示。

请注意,对于上面的示例,我们需要使用start with带有条件的子句来运行它per_id2='A'。此外,请了解如果您使用per_id2='B'

因此,当我从per_id2='B'结果开始时应该使所有记录成为01-01-2010而不是01-01-2011

现在有了结束日期,它应该01-01-2011适用于所有记录,除了带有 E 的记录和带有 A 的记录。结束日期将是null(假设我们从 A 开始)。

4

2 回答 2

1

Oracle Hierarchy 和 CTE 的组合将产生您想要的结果:

WITH cte AS 
(SELECT 
  per_id1,
  max(connect_by_root start_dt) as max_start,
  min(connect_by_root end_dt) as min_end
FROM 
  ci_per_per
CONNECT BY 
  NOCYCLE PRIOR per_id1 = per_id2
GROUP BY 
  per_id1)
SELECT
  ci_per_per.per_id1,
  ci_per_per.per_id2,
  cte.max_start,
  cte.min_end
FROM
  cte
  join ci_per_per on cte.per_id1=ci_per_per.per_id1

请参阅此处的 SQLFiddle 您的示例相当简单,因此我不能确定此逻辑是否适用于您的所有测试用例。

编辑更新为最低end_dt

于 2013-09-03T18:02:30.030 回答
0

我一直在尝试解决这个问题并找到了解决方案。

如果您认为以下 SQL 有任何问题,请纠正我:

select *
  from (SELECT root as per_id2,
               per_id1,
               PER_LEVEL,
               (case
                 when max(connect_by_root start_dt) >= max(start_dt) then
                  max(connect_by_root start_dt)
                 else
                  max(start_dt)
               end) as max_start,
               (case
                 when min(connect_by_root end_dt) <= min(end_dt) then
                  min(connect_by_root end_dt)
                 else
                  min(end_dt)
               end) AS min_end
          FROM (SELECT connect_by_root per_id2 AS root,
                       per_id1,
                       per_id2,
                       level as per_level,
                       (case
                         when connect_by_root start_dt >= start_dt then
                          connect_by_root start_dt
                         else
                          start_dt
                       end) as start_dt,
                       (case
                         when connect_by_root end_dt <= end_dt then
                          connect_by_root end_dt
                         else
                          end_dt
                       end) as end_dt
                  FROM ci_per_per
                CONNECT BY NOCYCLE PRIOR per_id1 = per_id2)
        CONNECT BY NOCYCLE PRIOR per_id1 = per_id2
               and PRIOR root = root
         GROUP BY per_id1, per_id2, root, per_level)
 where max_start <= min_end 

这是相同的小提琴

于 2013-09-10T12:54:51.247 回答