0

ReportingMonth に A/R、売上、預金などを表示する 4 つのビューに参加しています。しかし、私はいくつかの月に重複しています。下の方、特に 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結合するのは注意が必要です。2 番目と 3 番目の結合では、結合しようとしている列の一部がNULL(1 番目または 2 番目の外部結合のために) ある可能性があります。

問題を解決する可能性のあるものは次のとおりです。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 に答える