复杂的 NOT EXISTS 或 NOT IN Query

Complex NOT EXISTS or NOT IN Query

我正在使用三个主要表格。此查询 returns 我想要的子集

SELECT
    dt.DepositTypeID,
    dt.DepositTypeName AS DepositType, 
    'Total Revenues' AS TransactionGroupType,
    d.FiscalYearTypeID,
    SUM(d.DepositAmount) AS Amount
FROM
    Deposit AS d 
    INNER JOIN DepositType AS dt ON d.DepositTypeID = dt.DepositTypeID 
    INNER JOIN FiscalYearType AS fyt ON d.FiscalYearTypeID = fyt.FiscalYearTypeID
WHERE
    dt.DepositTypeID IN(1,2,4) 
    AND d.TransactionStatusTypeId = 3 --Must be approved
    AND d.IsProjectedDeposit = 0 --Must not be projected deposit
GROUP BY
    dt.DepositTypeName,
    d.FiscalYearTypeID,
    dt.DepositTypeID
ORDER BY
    dt.DepositTypeID,
    d.FiscalYearTypeID

Returns下面的子集

DepositTypeID | DepositType | TransactionGroupType | FiscalYearTypeID | Amount
-------------------------------------------------------------------------------
      1           Auction        Total Revenues           1             3434
      1           Auction        Total Revenues           3             52152
      1           Auction        Total Revenues           4             12859
      1           Auction        Total Revenues           5             542863
      1           SMIF Interest  Total Revenues           5             524586

现在,为了完成我的查询,我需要提取其中不存在的所有 DepositTypes 和 FiscalYearTypes,并用零填充 Amount。

DepositTypeID | DepositType | TransactionGroupType | FiscalYearTypeID | Amount
-------------------------------------------------------------------------------
      1           Auction        Total Revenues           1             3434
      2           Reserve Sale   Total Revenues           1             0
      4           SMIF Interest  Total Revenues           1             0

      1           Auction        Total Revenues           2             0
      2           Reserve Sale   Total Revenues           2             0
      4           SMIF Interest  Total Revenues           2             0

      1           Auction        Total Revenues           3             52152
      2           Reserve Sale   Total Revenues           3             0
      4           SMIF Interest  Total Revenues           3             0

      1           Auction        Total Revenues           4             12859
      2           Reserve Sale   Total Revenues           4             0
      4           SMIF Interest  Total Revenues           4             0

      1           Auction        Total Revenues           5             542863
      2           Reserve Sale   Total Revenues           5             0
      4           SMIF Interest  Total Revenues           5             524586

我得到了一些 NOT EXISTS 和 NOT IN 查询,但我一生都无法得到我需要的东西。

我认为你不需要NOT EXISTS;重新检查您的连接就足够了。由于您想要存款类型和会计年度类型的每种组合的存款摘要,请考虑对两种类型 table 进行 CROSS JOIN,然后对存款 table 进行 LEFT OUTER JOIN ].这可能是这样的:

-- Sample data inferred from the question:
declare @Deposit table 
(
    DepositID bigint, 
    FiscalYearTypeID bigint, 
    DepositTypeID bigint, 
    TransactionStatusTypeId bigint, 
    IsProjectedDeposit bit, 
    DepositAmount money
);
insert @Deposit values
    (1, 1, 1, 3, 0, 3434),
    (2, 3, 1, 3, 0, 52152),
    (3, 4, 1, 3, 0, 12859),
    (4, 5, 1, 3, 0, 542863),
    (5, 5, 4, 3, 0, 524586),
    -- EDIT: Added this last line to test the TransactionStatusTypeId/IsProjectedDeposit restrictions.
    (6, 2, 4, 1, 0, 9000);

declare @DepositType table 
(
    DepositTypeID bigint, 
    DepositTypeName varchar(32)
);
insert @DepositType values 
    (1, 'Auction'), 
    (2, 'Reserve Sale'), 
    (4, 'SMIF Interest');

declare @FiscalYearType table (FiscalYearTypeID bigint);
insert @FiscalYearType values (1), (2), (3), (4), (5);

-- The query:
select
    dt.DepositTypeID,
    dt.DepositTypeName AS DepositType, 
    'Total Revenues' AS TransactionGroupType,
    fyt.FiscalYearTypeID,
    coalesce(sum(d.DepositAmount), 0) AS Amount
from
    @DepositType dt
    cross join @FiscalYearType fyt
    -- EDIT: Now applying the deposit checks as part of the join rather than in the WHERE clause.
    left join @Deposit d on 
        dt.DepositTypeID = d.DepositTypeID and
        fyt.FiscalYearTypeID = d.FiscalYearTypeID and
        d.TransactionStatusTypeId = 3 and
        d.IsProjectedDeposit = 0
where
    dt.DepositTypeID in (1, 2, 4)
group by
    dt.DepositTypeName,
    fyt.FiscalYearTypeID,
    dt.DepositTypeID
order by
    fyt.FiscalYearTypeID,
    dt.DepositTypeID;

结果:

DepositTypeID    DepositType    TransactionGroupType    FiscalYearTypeID    Amount
1                Auction        Total Revenues          1                   3434.00
2                Reserve Sale   Total Revenues          1                   0.00
4                SMIF Interest  Total Revenues          1                   0.00
1                Auction        Total Revenues          2                   0.00
2                Reserve Sale   Total Revenues          2                   0.00
4                SMIF Interest  Total Revenues          2                   0.00
1                Auction        Total Revenues          3                   52152.00
2                Reserve Sale   Total Revenues          3                   0.00
4                SMIF Interest  Total Revenues          3                   0.00
1                Auction        Total Revenues          4                   12859.00
2                Reserve Sale   Total Revenues          4                   0.00
4                SMIF Interest  Total Revenues          4                   0.00
1                Auction        Total Revenues          5                   542863.00
2                Reserve Sale   Total Revenues          5                   0.00
4                SMIF Interest  Total Revenues          5                   524586.00

(已编辑以更正答案原始版本中的错误。)