同一表上的LEFT OUTER JOIN返回0条记录

时间:2018-07-09 09:29:59

标签: sql oracle join

我已经在其他帖子中搜索了我的问题,但没有任何效果。

我有一个包含客户的表和餐厅的营业额预测数据,计划值和实现值都在同一表中(必须与以前的数据库保持一致)。

我需要获得特定餐厅(站点)和特定活动(餐厅,外卖店,单击n收集)的最后一年的计划的预测和已实现的预测,以及日期范围(通常是一个月)的预测。

我的问题是,当计划的预测不存在并且实现的最后一年存在时,请求什么都不会返回。

这是我的要求:

SELECT 
    P.AFO_ACTIVITY_ID,
    P.AFO_SITE_ID,
    P.AFO_FORECAST_DATE,
    P.AFO_SERVICE_ID,
    P.AFO_PLANNED_CUSTOMERS,
    P.AFO_REALIZED_CUSTOMERS,
    P.AFO_GROUP_CUSTOMERS,
    P.AFO_PLANNED_TURNOVER,
    P.AFO_REALIZED_TURNOVER,
    P.AFO_GROUP_TURNOVER,
    P.AFO_NB_EMPLOYEES,
    P.AFO_TURNOVER_EMPLOYEES,
    P.AFO_ID,
    p2.AFO_REALIZED_CUSTOMERS AS REAL_CUSTOMERS_PREVIOUS_YEAR,
    p2.AFO_REALIZED_TURNOVER  AS REAL_TURNOVER_PREVIOUS_YEAR
FROM 
    ACT_FORECAST P
LEFT OUTER JOIN 
    act_forecast p2 ON p2.AFO_FORECAST_DATE = p.AFO_FORECAST_DATE - 364
                    AND p2.AFO_SITE_ID = p.AFO_SITE_ID
                    AND p2.AFO_ACTIVITY_ID = p.AFO_ACTIVITY_ID
                    AND p2.AFO_SERVICE_ID = p.AFO_SERVICE_ID
WHERE 
    P.AFO_SITE_ID = :siteid
    AND P.AFO_ACTIVITY_ID = :activityid
    AND P.AFO_FORECAST_DATE BETWEEN :startdate AND :enddate

如果我播放2018年2月的请求,则会有我的数据(2018年和2017年)。

但是如果我在2019年2月玩的话我什么都没有,那对于今年的计划部分来说还可以,因为尚未完成预测,但对于上一年的部分,我应该有一些数据。

我也尝试了RIGHT OUTER JOIN,但是什么也没出现。

样本数据:

ActivityId SiteId ForecastDate ServiceId PlannedCustomers RealizedCustomers GroupCustomers PlannedTurnover RealizedTurnover GroupTurnover NbEmployeeMeal TurnoverEmployeeMeal ID      RealizedCustomersPreviousYear RealizedTurnoverPreviousYear
1          58     01/02/18     1         0                0                 0              0               11               0             0              11                   2953773 0                             39,85
1          58     01/02/18     3         345              398               0              3467,25         4052,01          0             0              0                    2953774 328                           3291,53
1          58     01/02/18     5         10               16                0              140             194,04           0             16             60,9                 2953775 7                             91,12
1          58     01/02/18     7         125              151               0              1200            1413,93          0             0              0                    2953776 112                           1063,89

有什么想法吗?

致谢

4 个答案:

答案 0 :(得分:3)

您的where子句仅在您指定范围内的P中查找记录。如果没有,则没有源行可以离开

您可以通过内联视图或CTE生成您正在查看的时段内的所有可能日期,然后查找与任一日期集匹配的记录:

WITH CTE (this_year, last_year) AS (
  SELECT :startdate + level - 1, :startdate + level - 365
  FROM dual
  CONNECT BY level <= :enddate - :startdate + 1
)
SELECT 
    :activityid AS AFO_ACTIVITY_ID,
    :siteid AS AFO_SITE_ID,
    CTE.this_year AS AFO_FORECAST_DATE,
    P.AFO_SERVICE_ID,
    MAX(CASE WHEN P.AFO_FORECAST_DATE = this_year
      THEN P.AFO_PLANNED_CUSTOMERS END) AS AFO_PLANNED_CUSTOMERS,
    MAX(CASE WHEN P.AFO_FORECAST_DATE = this_year
      THEN P.AFO_REALIZED_CUSTOMERS END) AS AFO_REALIZED_CUSTOMERS,
    MAX(CASE WHEN P.AFO_FORECAST_DATE = this_year
      THEN P.AFO_GROUP_CUSTOMERS END) AS AFO_GROUP_CUSTOMERS,
    MAX(CASE WHEN P.AFO_FORECAST_DATE = this_year
      THEN P.AFO_PLANNED_TURNOVER END) AS AFO_PLANNED_TURNOVER,
    MAX(CASE WHEN P.AFO_FORECAST_DATE = this_year
      THEN P.AFO_REALIZED_TURNOVER END) AS AFO_REALIZED_TURNOVER,
    MAX(CASE WHEN P.AFO_FORECAST_DATE = this_year
      THEN P.AFO_GROUP_TURNOVER END) AS AFO_GROUP_TURNOVER,
    MAX(CASE WHEN P.AFO_FORECAST_DATE = this_year
      THEN P.AFO_NB_EMPLOYEES END) AS AFO_NB_EMPLOYEES,
    MAX(CASE WHEN P.AFO_FORECAST_DATE = this_year
      THEN P.AFO_TURNOVER_EMPLOYEES END) AS AFO_TURNOVER_EMPLOYEES,
    MAX(CASE WHEN P.AFO_FORECAST_DATE = this_year
      THEN P.AFO_ID END) AS AFO_ID,
    MAX(CASE WHEN P.AFO_FORECAST_DATE = last_year
      THEN P.AFO_REALIZED_CUSTOMERS END) AS REAL_CUSTOMERS_PREVIOUS_YEAR,
    MAX(CASE WHEN P.AFO_FORECAST_DATE = last_year
      THEN P.AFO_REALIZED_TURNOVER END) AS REAL_TURNOVER_PREVIOUS_YEAR
FROM
    CTE
JOIN
    ACT_FORECAST P
ON
    P.AFO_FORECAST_DATE = CTE.this_year
OR
    P.AFO_FORECAST_DATE = CTE.last_year
WHERE 
    P.AFO_SITE_ID = :siteid
    AND P.AFO_ACTIVITY_ID = :activityid
GROUP BY
    :activityid,
    :siteid,
    CTE.this_year,
    P.AFO_SERVICE_ID;

而不是尝试外部联接(以任何方式),它都查找两个年份中的特定日期,并且执行一种手动枢轴操作以在右侧列中显示相关年份的值。请注意,最后两列的表达式使用last_year,其余的使用this_year(分组的表达式除外)。

对于SERVICE_ID,如果今年没有值,则可能希望合并以获取去年的值。如果您不想看到空值,也可以将其他值合并为零。


如果您不想使用WITH子句,则可以使用内联视图:

SELECT
...
FROM
    (
        SELECT :startdate + level - 1 AS this_year,
               :startdate + level - 365 AS last_year
        FROM dual
        CONNECT BY level <= :enddate - :startdate + 1
    ) CTE
JOIN
    ACT_FORECAST P
...

尽管您可能想要一个比CTE(使用任一版本...)更具描述性的名称


将示例数据作为CTE重做,对于未在问题中显示的值则为空:

with act_forecast (afo_activity_id, afo_site_id, afo_forecast_date, afo_service_id,
  afo_planned_customers, afo_realized_customers, afo_group_customers,
  afo_planned_turnover, afo_realized_turnover, afo_group_turnover,
  afo_nb_employees, afo_turnover_employees, afo_id)
as (
            select 1, 58, date '2018-02-01', 1, 0, 0, 0, 0, 11, 0, 0, 11, 2953773 from dual
  union all select 1, 58, date '2017-02-02', 1, null, 0, null, null, 39.85, null, null, null, null from dual
  union all select 1, 58, date '2018-02-01', 3, 345, 398, 0, 3467.25, 4052.01, 0, 0, 0, 2953774 from dual
  union all select 1, 58, date '2017-02-02', 3, null, 328, null, null, 3291.53, null, null, null, null from dual
  union all select 1, 58, date '2018-02-01', 5, 10, 16, 0, 140, 194.04, 0, 16, 60.9, 2953775 from dual
  union all select 1, 58, date '2017-02-02', 5, null, 7, null, null, 91.12, null, null, null, null from dual
  union all select 1, 58, date '2018-02-01', 7, 125, 151, 0, 1200, 1413.93, 0, 0, 0, 2953776 from dual
  union all select 1, 58, date '2017-02-02', 7, null, 112, null, null, 1063.89, null, null, null, null from dual
)

然后在2018年给出的范围如下:

AFO_ACTIVITY_ID AFO_SITE_ID AFO_FORECA AFO_SERVICE_ID AFO_PLANNED_CUSTOMERS AFO_REALIZED_CUSTOMERS AFO_GROUP_CUSTOMERS AFO_PLANNED_TURNOVER AFO_REALIZED_TURNOVER AFO_GROUP_TURNOVER AFO_NB_EMPLOYEES AFO_TURNOVER_EMPLOYEES     AFO_ID REAL_CUSTOMERS_PREVIOUS_YEAR REAL_TURNOVER_PREVIOUS_YEAR
--------------- ----------- ---------- -------------- --------------------- ---------------------- ------------------- -------------------- --------------------- ------------------ ---------------- ---------------------- ---------- ---------------------------- ---------------------------
              1          58 2018-02-01              3                   345                    398                   0              3467.25               4052.01                  0                0                      0    2953774                          328                     3291.53
              1          58 2018-02-01              7                   125                    151                   0                 1200               1413.93                  0                0                      0    2953776                          112                     1063.89
              1          58 2018-02-01              1                     0                      0                   0                    0                    11                  0                0                     11    2953773                            0                       39.85
              1          58 2018-02-01              5                    10                     16                   0                  140                194.04                  0               16                   60.9    2953775                            7                       91.12

,并在2019年提供以下范围:

AFO_ACTIVITY_ID AFO_SITE_ID AFO_FORECA AFO_SERVICE_ID AFO_PLANNED_CUSTOMERS AFO_REALIZED_CUSTOMERS AFO_GROUP_CUSTOMERS AFO_PLANNED_TURNOVER AFO_REALIZED_TURNOVER AFO_GROUP_TURNOVER AFO_NB_EMPLOYEES AFO_TURNOVER_EMPLOYEES     AFO_ID REAL_CUSTOMERS_PREVIOUS_YEAR REAL_TURNOVER_PREVIOUS_YEAR
--------------- ----------- ---------- -------------- --------------------- ---------------------- ------------------- -------------------- --------------------- ------------------ ---------------- ---------------------- ---------- ---------------------------- ---------------------------
              1          58 2019-01-31              3                                                                                                                                                                                                            398                     4052.01
              1          58 2019-01-31              5                                                                                                                                                                                                             16                      194.04
              1          58 2019-01-31              1                                                                                                                                                                                                              0                          11
              1          58 2019-01-31              7                                                                                                                                                                                                            151                     1413.93

答案 1 :(得分:2)

如果我理解正确,则可以使用FULL OUTER JOIN

SELECT 
    COALESCE(P.AFO_ACTIVITY_ID, P2.AFO_ACTIVITY_ID) as AFO_ACTIVITY_ID,
    COALESCE(P.AFO_SITE_ID, P2.AFO_SITE_ID) as AFO_SITE_ID, 
    P.AFO_FORECAST_DATE,
    P.AFO_SERVICE_ID,
    P.AFO_PLANNED_CUSTOMERS,
    P.AFO_REALIZED_CUSTOMERS,
    P.AFO_GROUP_CUSTOMERS,
    P.AFO_PLANNED_TURNOVER,
    P.AFO_REALIZED_TURNOVER,
    P.AFO_GROUP_TURNOVER,
    P.AFO_NB_EMPLOYEES,
    P.AFO_TURNOVER_EMPLOYEES,
    P.AFO_ID,
    p2.AFO_REALIZED_CUSTOMERS AS REAL_CUSTOMERS_PREVIOUS_YEAR,
    p2.AFO_REALIZED_TURNOVER  AS REAL_TURNOVER_PREVIOUS_YEAR
FROM (SELECT P.*
      FROM ACT_FORECAST P
      WHERE P.AFO_SITE_ID = :siteid AND
            P.AFO_ACTIVITY_ID = :activityid AND
            P.AFO_FORECAST_DATE BETWEEN :startdate AND :enddate
     ) P FULL JOIN
     (SELECT p2.*
      FROM act_forecast p2
      WHERE P2.AFO_SITE_ID = :siteid AND
            P2.AFO_ACTIVITY_ID = :activityid AND
            P2.AFO_FORECAST_DATE BETWEEN :startdate - 364 AND :enddate - 364
     ) p2
     ON p2.AFO_FORECAST_DATE = p.AFO_FORECAST_DATE - 364 AND
        p2.AFO_SITE_ID = p.AFO_SITE_ID AND
        p2.AFO_ACTIVITY_ID = p.AFO_ACTIVITY_ID AND
        p2.AFO_SERVICE_ID = p.AFO_SERVICE_ID

答案 2 :(得分:0)

我很惊讶地看到- 364(我的专业知识可能少于您的专业知识)并且期望:

p.AFO_FORECAST_DATE - INTERVAL 364 DAY

也许这被解释为(微)秒?

(典型的调试方法是忽略掉AFO_FORECAST_DATE的部分条件。)

答案 3 :(得分:0)

使用类似下面的东西:

    SELECT P.AFO_ACTIVITY_ID,
    P.AFO_SITE_ID,
    P.AFO_FORECAST_DATE,
    P.AFO_SERVICE_ID,
    P.AFO_PLANNED_CUSTOMERS,
    P.AFO_REALIZED_CUSTOMERS,
    P.AFO_GROUP_CUSTOMERS,
    P.AFO_PLANNED_TURNOVER,
    P.AFO_REALIZED_TURNOVER,
    P.AFO_GROUP_TURNOVER,
    P.AFO_NB_EMPLOYEES,
    P.AFO_TURNOVER_EMPLOYEES,
    P.AFO_ID,
    p2.AFO_REALIZED_CUSTOMERS AS REAL_CUSTOMERS_PREVIOUS_YEAR,
    p2.AFO_REALIZED_TURNOVER  AS REAL_TURNOVER_PREVIOUS_YEAR
    FROM ACT_FORECAST P
    LEFT OUTER JOIN(SELECT DISTINCT p2.AFO_FORECAST_DATE 
        FROM ACT_FORECAST P2)
    ON p2.AFO_FORECAST_DATE = p.AFO_FORECAST_DATE - 364
                    AND p2.AFO_SITE_ID = p.AFO_SITE_ID
                    AND p2.AFO_ACTIVITY_ID = p.AFO_ACTIVITY_ID
                    AND p2.AFO_SERVICE_ID = p.AFO_SERVICE_ID
WHERE 
    P.AFO_SITE_ID = :siteid
    AND P.AFO_ACTIVITY_ID = :activityid
    AND P.AFO_FORECAST_DATE BETWEEN :startdate AND :enddate