0

如何将当前在WHERE子句中的联接移动AND subMainTable.ID = MainTable.IDFROM 子句中。

由于它的别名方式,我似乎无法将它移到那里。

这是一个有效的查询,我已重命名并删除了与该问题无关的部分。

SELECT  
      ID
     ,DETAILS
     ,ENTRYDATE
     ,ISNULL(LastBalance, 0) + (
           SELECT SUM(VAL)
           FROM tempData subMainTable
                LEFT OUTER JOIN FinanceTable f ON subMainTable.ID = f.accId
                                                AND subMainTable.TRANS_ID = f.txnID
           WHERE subMainTable.TRANS_ID <= MainTable.TRANS_ID
                     AND subMainTable.ID = MainTable.ID
                     AND f.txnID IS NULL
          ) AS Balance
FROM 
     (SELECT MainTable.*
      FROM tempData MainTable LEFT JOIN FinanceTable FinTable ON 
                   (MainTable.ID = FinTable.accId
                    AND MainTable.TRANS_ID = FinTable.txnID )
      WHERE  (FinTable.accId IS NULL AND FinTable.txnID IS NULL )
                ) AS MainTable ...
4

2 回答 2

1

Try something like :

SELECT  MainTable.ID,MainTable.DETAILS,MainTable.ENTRYDATE,ISNULL(MainTable.LastBalance, 0) + ( SUM(subMainTable.VAL)) AS Balance
FROM    
(
    SELECT MainTable.*
    FROM   tempData MainTable
    LEFT JOIN FinanceTable FinTable ON (MainTable.ID = FinTable.accId
    AND MainTable.TRANS_ID = FinTable.txnID
    )
    WHERE  (FinTable.accId IS NULL  AND FinTable.txnID IS NULL  )
) AS MainTable 
left outer join
(
    SELECT TRANS_ID ,ID , SUM(VAL) 
    FROM 
    tempData subMainTable LEFT OUTER JOIN FinanceTable f 
    ON 
    subMainTable.ID = f.accId AND 
    subMainTable.TRANS_ID = f.txnID
    WHERE 
    f.txnID IS NULL
) as subMainTable on subMainTable.TRANS_ID <= MainTable.TRANS_ID AND subMainTable.ID = MainTable.ID
group by MainTable.ID,MainTable.DETAILS,MainTable.ENTRYDATE,ISNULL(MainTable.LastBalance, 0)

I did't tested it but i think it will work .

于 2013-07-13T13:29:50.233 回答
1

这是其中的一部分

  SELECT  ID
         ,DETAILS
         ,ENTRYDATE
         ,ISNULL(LastBalance, 0) + (
                SELECT SUM(VAL)
                  FROM tempData subMainTable
                  LEFT OUTER JOIN FinanceTable f 
                    ON subMainTable.ID = f.accId
                   AND subMainTable.TRANS_ID = f.txnID
                   AND subMainTable.TRANS_ID <= MainTable.TRANS_ID
                   AND subMainTable.ID = MainTable.ID
                 WHERE f.txnID IS NULL
                ) AS Balance
    FROM tempData MainTable 
    LEFT JOIN FinanceTable FinTable 
      ON MainTable.ID = FinTable.accId
     AND MainTable.TRANS_ID = FinTable.txnID
   WHERE FinTable.accId IS NULL 
     AND FinTable.txnID IS NULL

这是一个尝试

  SELECT  ID
         ,DETAILS
         ,ENTRYDATE
         ,ISNULL(LastBalance, 0) 
         ,SUM(VAL) AS DeltaBalance
    FROM tempData MainTable 
    LEFT JOIN FinanceTable FinTable 
      ON MainTable.ID = FinTable.accId
     AND MainTable.TRANS_ID = FinTable.txnID
    LEFT JOIN subMainTable 
      ON subMainTable.ID = MainTable.ID
     AND subMainTable.TRANS_ID <= MainTable.TRANS_ID  
    LEFT JOIN FinanceTable f 
      ON subMainTable.ID = f.accId
     AND subMainTable.TRANS_ID = f.txnID
   WHERE FinTable.accId IS NULL 
     AND FinTable.txnID IS NULL
     AND f.txnID IS NULL
   GROUP BY ID, DETAILS,ENTRYDATE,LastBalance
于 2013-07-13T13:31:07.183 回答