复杂的NOT EXISTS或NOT查询

时间:2015-04-27 22:31:05

标签: sql-server not-exists

我有三张主要表格。此查询返回我想要的子集

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

返回

下面的子集
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

我有一些不存在而不是查询,但因为我的生活无法得到我所需要的。

1 个答案:

答案 0 :(得分:2)

我认为你不需要NOT EXISTS;它应该足以重新检查你的连接。由于您需要存款类型和会计年度类型的每种组合的存款摘要,因此请考虑两个类型表的CROSS JOIN,然后是存款表的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

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