如何分组并包括零和

时间:2013-09-17 19:44:31

标签: c# sql ms-access-2007

我正在尝试编写一个查询,该查询返回用户在我的应用程序中创建的每个类别中的事务总和。我当前的查询工作正常,但如果尚未在该类别下进行任何交易,则不显示类别。我希望我的查询显示每个类别,即使所有内容的总和都是0。

我查了一下怎么做,并建议使用LEFT / RIGHT OUTER JOIN,但它没有做我想要的。

我通过Windows C#Application访问Microsoft Access 2007数据库。这是我目前的查询:

SELECT Categories.CategoryName ,
       SUM( Transactions.TransactionCredit ) as CreditSum ,
       SUM( Transactions.TransactionDebit  ) as DebitSum  ,
       SUM( Transactions.TransactionCredit ) -
       SUM( Transactions.TransactionDebit  ) as Difference ,
       SUM( Transactions.TransactionDebit  ) /
       ( SELECT SUM(TransactionDebit)
         FROM Transactions
         WHERE AccountID = {currentAccountId}
       ) as 'Percent of Total Debit'
FROM Categories
LEFT JOIN Transactions on Categories.CategoryName = Transactions.CategoryName
WHERE Transactions.AccountID = {currentAccountId}
  AND TransactionDate >= {startDate}
  AND TransactionDate <= {endDate}
GROUP BY Categories.CategoryName

这是我在c#app中编码的方式:

queries.returnQuery("SELECT Categories.CategoryName, " +
"SUM(Transactions.TransactionCredit) as CreditSum, " +
"SUM(Transactions.TransactionDebit) as DebitSum, " +
"SUM(Transactions.TransactionCredit) - SUM(Transactions.TransactionDebit) as Difference, " +
"SUM(Transactions.TransactionDebit) / (SELECT SUM(TransactionDebit) FROM Transactions WHERE AccountID = " + currentAccountID + ") as 'Percent of Total Debit' " +
"FROM Categories LEFT JOIN Transactions on Categories.CategoryName = Transactions.CategoryName " +
"WHERE Transactions.AccountID = " + currentAccountID +
(enableDateFilter ? " AND Transactions.TransactionDate >= #" + startDate + "# AND Transactions.TransactionDate <= #" + endDate + "# " : " ") +
" GROUP BY Categories.CategoryName");

3 个答案:

答案 0 :(得分:1)

如果没有双引号,您的查询会更容易。在任何情况下,问题是where子句引用Transactions表,撤消left outer join。将这些条件移到on子句中。在您的情况下,通过将where更改为and

,可以轻松完成此操作
"SELECT Categories.CategoryName, " +
"SUM(Transactions.TransactionCredit) as CreditSum, " +
"SUM(Transactions.TransactionDebit) as DebitSum, " +
"SUM(Transactions.TransactionCredit) - SUM(Transactions.TransactionDebit) as Difference, " +
"SUM(Transactions.TransactionDebit) / (SELECT SUM(TransactionDebit) FROM Transactions WHERE AccountID = " + currentAccountID + ") as 'Percent of Total Debit' " +
"FROM Categories LEFT JOIN Transactions on Categories.CategoryName = Transactions.CategoryName " +
"and Transactions.AccountID = " + currentAccountID +
(enableDateFilter? " AND TransactionDate >= #" + startDate + "# AND TransactionDate <= #" + endDate + "# " : " ") +
"GROUP BY Categories.CategoryName"

答案 1 :(得分:0)

当您在LEFT JOINCategories之间使用Transactions时,您应该在事务表上预期NULL值。在JOIN子句中包含您的AccountID过滤器可以解决这个问题:

DECLARE @currentAccountId INT
SELECT  @currentAccountId = 1

;WITH   Transactions AS
(
    SELECT  *
    FROM    (   
    VALUES
                (1, 1000., 2000., 'Category B'),
                (2, 2000., 1000., 'Category B'),
                (1, 1000., 1000., 'Category C')
            )
            T ( AccountID, TransactionCredit, TransactionDebit, CategoryName)
),      Categories AS
(
    SELECT  *
    FROM    (
    VALUES
                ('Category A'),
                ('Category B'),
                ('Category C')
            )
            C (CategoryName)
)
SELECT      Categories.CategoryName ,
            SUM( Transactions.TransactionCredit ) as CreditSum ,
            SUM( Transactions.TransactionDebit  ) as DebitSum  ,
            SUM( Transactions.TransactionCredit ) -
            SUM( Transactions.TransactionDebit  ) as Difference ,
            SUM( Transactions.TransactionDebit  ) /
            (
                SELECT  SUM(TransactionDebit)
                FROM    Transactions
                WHERE   AccountID = @currentAccountId
            ) AS 'Percent of Total Debit'
FROM        Categories
LEFT JOIN   Transactions
    ON      Categories.CategoryName = Transactions.CategoryName
            AND Transactions.AccountID = @currentAccountId
            --AND TransactionDate >= {startDate}
            --AND TransactionDate <= {endDate}
GROUP BY    Categories.CategoryName

结果

CategoryName    CreditSum   DebitSum    Difference  Percent of Total Debit
---------------------------------------------------------------------------------
Category A      NULL        NULL        NULL        NULL
Category B      1000        2000        -1000       0.666666
Category C      1000        1000        0           0.333333

答案 2 :(得分:0)

我遇到的问题是,当使用JOIN时,您必须使用将两个表进行比较的条件。在我的情况下,我没有这样做。我使用的是用户在c#应用程序中选择的参数,因此我使用SUBQUERY代替Transactions表并获得了预期的结果。请参阅下面的解决方案:

C#中的解决方案:

queries.returnQuery("SELECT Categories.CategoryName, " +
                           "SUM(SUBQUERY.TransactionCredit) as CreditSum, " +
                           "SUM(SUBQUERY.TransactionDebit) as DebitSum, " +
                           "SUM(SUBQUERY.TransactionCredit) - SUM(SUBQUERY.TransactionDebit) as Difference " +
                           "FROM Categories LEFT JOIN " +
                               "(SELECT CategoryName, TransactionCredit, TransactionDebit FROM Transactions WHERE AccountID = " + currentAccountID + 
                               (enableDateFilter ? " AND TransactionDate >= #" + startDate + "# AND TransactionDate <= #" + endDate + "# " : " ") + 
                               ") AS SUBQUERY " + 
                           "ON Categories.CategoryName = SUBQUERY.CategoryName " +
                           "GROUP BY Categories.CategoryName");

解决方案不在C#中:

SELECT Categories.CategoryName ,
       SUM( SUBQUERY.TransactionCredit ) as CreditSum ,
       SUM( SUBQUERY.TransactionDebit  ) as DebitSum  ,
       SUM( SUBQUERY.TransactionCredit ) -
       SUM( SUBQUERY.TransactionDebit  ) as Difference ,
       SUM( SUBQUERY.TransactionDebit  ) /
       ( SELECT SUM(TransactionDebit)
         FROM Transactions
         WHERE AccountID = {currentAccountId}
       ) as 'Percent of Total Debit'
FROM Categories
LEFT JOIN 
(
    SELECT CategoryName, 
           TransactionCredit, 
           TransactionDebit 
    FROM Transactions 
    WHERE AccountID = {currentAccountID}
      AND TransactionDate >= {startDate}
      AND TransactionDate <= {endDate}
) as SUBQUERY 
ON Categories.CategoryName = SUBQUERY.CategoryName
GROUP BY Categories.CategoryName