简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM