开发者

Using Subquery results as a query variable

I'm trying to make the result of one of my subqueries be used in another subquery but it doesn't work.

This is the query:

SELECT t.TenantName, 
    (SELECT SUM(Amount) 
     FROM tblTransaction 
     WHERE Amount > 0 
        AND TransactionDate >= '12/01/09' 
        AND TransactionDate <= '12/31/09'
        AND TenantID = t.ID 
        AND TransactionCode = trans.TransactionCode) AmountPaid, 
    (SELECT SUM(Amount) 
     FROM tblTransaction 
     WHERE tblTransaction.TransactionCode = trans.TransactionCode 
        AND tblTransaction.TenantID = t.ID) - AmountPaid AmountOwedTotal, 
    (SELECT SUM(Amount) 
     FROM tblTransaction 
     WHERE  tblTransaction.TransactionCode = trans.TransactionCode 
        AND tblTransaction.TenantID = t.ID
       AND Amount < 0 AND TransactionDate >= '12/01/09' 
        AND TransactionDate <= '12/31/09') AmountOwedThisMonth, 
   code.Description, trans.TransactionDate 
FROM tblTransaction trans 
   LEFT JOIN tblTenantTransCode code 
      ON code.ID = trans.TransactionCode
   LEFT JOIN tblTenant t 
      ON t.ID = trans.TenantID
WHERE trans.开发者_运维百科TransactionDate >= '12/01/09' 
   AND trans.TransactionDate <= '12/31/09' 
   AND trans.Amount > 0

Sorry it's so complicated.

Where I do (subquery) - AmountPaid the SqlServer complains AmountPaid isn't a valid column name. How can I get access to the result of it's subquery?


SELECT  TenantName,
        Description,
        TransactionDate,
        AmountPaid,
        AmountRequired - AmountPaid AS AmountOwedTotal,
        AmountOwedThisMonth
FROM    (
        SELECT  t.TenantName,
                code.Description,
                trans.TransactionDate,
                (
                SELECT  SUM(Amount)
                FROM    tblTransaction
                WHERE   Amount > 0
                        AND    TransactionDate >= '12/01/09'
                        AND    TransactionDate <= '12/31/09'
                        AND    TenantID = t.ID
                        AND    TransactionCode = trans.TransactionCode
                ) AS AmountPaid,
                (
                SELECT  SUM(Amount)
                FROM    tblTransaction
                WHERE   tblTransaction.TransactionCode = trans.TransactionCode
                        AND    tblTransaction.TenantID = t.ID
                ) AS AmountRequired,
                (
                SELECT SUM(Amount)
                FROM   tblTransaction
                WHERE  tblTransaction.TransactionCode = trans.TransactionCode
                        AND    tblTransaction.TenantID = t.ID
                        AND    Amount < 0
                AND    TransactionDate >= '12/01/09'
                AND    TransactionDate <= '12/31/09'
                ) AS AmountOwedThisMonth,
        FROM    tblTransaction trans
        LEFT JOIN
                tblTenantTransCode code
        ON      code.ID = trans.TransactionCode
        LEFT JOIN       
                tblTenant t
        ON      t.ID = trans.TenantID
        WHERE   trans.TransactionDate >= '12/01/09'
                AND trans.TransactionDate <= '12/31/09'
                AND trans.Amount > 0
        ) q


try this:

   Select t.TenantName, c.Description, tx.TransactionDate, 
     Sum(Case When TransactionDate Between '12/01/09' AND '12/31/09'
                   And Amount > 0
              Then tx.Amount Else 0 End) AmountPaid,
     Sum(tx.Amount) Total,
     Sum(tx.Amount) -  
     Sum(Case When TransactionDate Between '12/01/09' AND '12/31/09'
                   And Amount > 0
              Then tx.Amount Else 0 End) AnountOwed,
     Sum(Case When TransactionDate Between '12/01/09' AND '12/31/09'
                   And Amount < 0
              Then tx.Amount Else 0 End) AnountOwedThisMonth          
    FROM tblTransaction tx    
       LEFT JOIN tblTenantTransCode c 
          ON c.ID = tx.TransactionCode
       LEFT JOIN tblTenant t
           ON t.ID = tx.TenantID
    Group By t.TenantName, c.Description
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜