0

我正在加入四个视图以在 ReportingMonth 上显示 A/R、销售额、存款等。但我在几个月里得到了重复。向下看,特别是 2014 年 1 月、2014 年 2 月、2012 年 12 月。

重复的月份

是)我有的:

SELECT ISNULL(ISNULL(ISNULL(outf.ReportingMonth, sales.ReportingMonth), dep.ReportingMonth), ar.ReportingMonth) AS ReportingMonth
    ,ar.AR_100_Percent AS EndOfMonthARBalance
    ,ar.AR_85_Percent AS MLOCCeiling
    ,sales.MonthlySales AS Sales
    ,dep.Deposits
    ,outf.Outflow
    ,dep.Deposits + outf.Outflow AS CashPerformance
FROM vCI_MLOC_MONTHLYAR ar
FULL OUTER JOIN vCI_MLOC_MONTHLYSALES sales
ON ar.ReportingMonth = sales.ReportingMonth
FULL OUTER JOIN vCI_MLOC_MONTHLYDEPOSITS dep
ON sales.ReportingMonth = dep.ReportingMonth
FULL OUTER JOIN vCI_MLOC_MONTHLYOUTF outf
ON dep.ReportingMonth = outf.ReportingMonth
GROUP BY outf.ReportingMonth
    ,dep.ReportingMonth
    ,ar.ReportingMonth
    ,sales.ReportingMonth
    ,ar.AR_100_Percent
    ,ar.AR_85_Percent
    ,sales.MonthlySales
    ,dep.Deposits
    ,outf.Outflow
4

3 回答 3

2

这样做时会遇到问题full outer joins,因为您最终会丢失关键列中的数据。

以下版本使用驱动程序表来获取所有月份,并对该表进行所有连接:

SELECT driver.ReportingMonth
    ,(other fields here)
FROM (select distinct ReportingMonth
      from ((select ReportingMonth
             from vCI_MLOC_MONTHLYAR
            ) union all
            (select ReportingMonth
             from vCI_MLOC_MONTHLYSALES
            ) union all
            (select ReportingMonth
             from vCI_MLOC_MONTHLYDEPOSITS
            ) union all
            (select ReportingMonth
             from vCI_MLOC_MONTHLYOUTF
            )
           ) t
     ) driver
FULL OUTER JOIN vCI_MLOC_MONTHLYAR ar on driver.ReportingMonth = ar.ReportingMonth
FULL OUTER JOIN vCI_MLOC_MONTHLYSALES sales
ON driver.ReportingMonth = sales.ReportingMonth
FULL OUTER JOIN vCI_MLOC_MONTHLYDEPOSITS dep
ON driver.ReportingMonth = dep.ReportingMonth
FULL OUTER JOIN vCI_MLOC_MONTHLYOUTF outf
ON driver.ReportingMonth = outf.ReportingMonth
于 2012-11-29T16:06:01.193 回答
1

用连接连接 4 个表(或视图,没关系)FULL很棘手。在第二次和第三次连接时,您尝试连接的某些列可能是NULL(由于第一次或第二次外连接)。

这是可能解决问题的方法。FROM将该子句替换为:

FROM vCI_MLOC_MONTHLYAR ar
  FULL OUTER JOIN vCI_MLOC_MONTHLYSALES sales
    ON ar.ReportingMonth = sales.ReportingMonth
  FULL OUTER JOIN vCI_MLOC_MONTHLYDEPOSITS dep
    ON COALESCE(ar.ReportingMonth, sales.ReportingMonth) 
       = dep.ReportingMonth
  FULL OUTER JOIN vCI_MLOC_MONTHLYOUTF outf
    ON COALESCE(ar.ReportingMonth, sales.ReportingMonth, dep.ReportingMonth) 
       = outf.ReportingMonth

或与:

FROM 
        vCI_MLOC_MONTHLYAR ar
      FULL OUTER JOIN vCI_MLOC_MONTHLYSALES sales
          ON ar.ReportingMonth = sales.ReportingMonth
  FULL OUTER JOIN 
        vCI_MLOC_MONTHLYDEPOSITS dep
      FULL OUTER JOIN vCI_MLOC_MONTHLYOUTF outf
          ON dep.ReportingMonth = outf.ReportingMonth
    ON COALESCE(ar.ReportingMonth, sales.ReportingMonth)
     = COALESCE(dep.ReportingMonth, out.ReportingMonth) 

另一种解决方案是有一个calendar带有ReportingMonth列的表,或者如果没有,则动态创建它(类似于@Gordon 的解决方案):

SELECT cal.ReportingMonth
       --- all the other columns do not change
FROM 
    ( SELECT ReportingMonth
      FROM vCI_MLOC_MONTHLYAR ar
    UNION 
      SELECT ReportingMonth
      FROM vCI_MLOC_MONTHLYSALES sales
    UNION 
      SELECT ReportingMonth
      FROM vCI_MLOC_MONTHLYDEPOSITS dep
    UNION 
      SELECT ReportingMonth
      FROM vCI_MLOC_MONTHLYOUTF outf
    ) AS cal
LEFT JOIN
    vCI_MLOC_MONTHLYAR ar
        ON ar   .ReportingMonth = cal.ReportingMonth
LEFT JOIN
    vCI_MLOC_MONTHLYSALES sales
        ON sales.ReportingMonth = cal.ReportingMonth
LEFT JOIN
    vCI_MLOC_MONTHLYDEPOSITS dep
        ON dep  .ReportingMonth = cal.ReportingMonth
LEFT JOIN
    vCI_MLOC_MONTHLYOUTF outf
        ON outf .ReportingMonth = cal.ReportingMonth
于 2012-11-29T16:04:53.967 回答
1

这也有效...

SELECT ReportingMonth
    ,SUM(EndOfMonthARBalance) AS EndOfMonthARBalance
    ,SUM(MLOCCeiling) AS MLOCCeiling
    ,SUM(Sales) AS Sales
    ,SUM(Deposits) AS Deposits
    ,SUM(Outflow) AS Outflow
    ,SUM(Deposits) + SUM(Outflow) AS CashPerformance

FROM(
    SELECT ReportingMonth AS ReportingMonth
         , NULL AS EndOfMonthARBalance
         , NULL AS MLOCCeiling
         , NULL AS Sales
         , NULL AS Deposits
         , Outflow AS Outflow
    FROM vCI_MLOC_MONTHLYOUTF

    UNION ALL
    SELECT ReportingMonth AS ReportingMonth
         , NULL AS EndOfMonthARBalance
         , NULL AS MLOCCeiling
         , MonthlySales AS Sales
         , NULL AS Deposits
         , NULL AS Outflow
    FROM vCI_MLOC_MONTHLYSALES

    UNION ALL
    SELECT ReportingMonth AS ReportingMonth
         , NULL AS EndOfMonthARBalance
         , NULL AS MLOCCeiling
         , NULL AS Sales
         , Deposits AS Deposits
         , NULL AS Outflow
    FROM vCI_MLOC_MONTHLYDEPOSITS

    UNION ALL  
    SELECT ReportingMonth AS ReportingMonth
         , AR_100_Percent AS EndOfMonthARBalance
         , AR_85_Percent AS MLOCCeiling
         , NULL AS Sales
         , NULL AS Deposits
         , NULL AS Outflow
    FROM vCI_MLOC_MONTHLYAR
) AS a    
GROUP BY ReportingMonth
于 2012-11-29T17:12:43.243 回答