Can anybody see if this query can be written better?
SELECT a.AccountNbr, a.AccountName,
(SELECT SUM(b.DebitAmt) FROM b.Transactions as b WHERE b.acctid = a.acctid
AND DatePart(yyyy, b.TransactionDate) = 2004) as Debit,
(SELECT SUM(b.CreditAmt) FROM Transactions as b WHERE b.acctid = a.acctid
AND DatePart(yyyy, b.TransactionDate) = 2004) as Credit
FROM Account as a
WHERE a.EntityId = 1
Is there a way to get the two subqueries be built as one since their WHERE clause is the same?
Thanks in advance.
SELECT a.AccountNbr, a.AccountName,
(SELECT SUM(b.DebitAmt) FROM b.Transactions as b WHERE b.acctid = a.acctid
AND DatePart(yyyy, b.TransactionDate) = 2004) as Debit,
(SELECT SUM(b.CreditAmt) FROM Transactions as b WHERE b.acctid = a.acctid
AND DatePart(yyyy, b.TransactionDate) = 2004) as Credit
FROM Account as a
WHERE a.EntityId = 1
Is there a way to get the two subqueries be built as one since their WHERE clause is the same?
Thanks in advance.