Question : SQL sub query help needed

So I was working on this query which will sum up all the dollars that are due from each of the invoices on a given calendar day.  The tables that are in play are tblInvoice, tblInvoice_Detail, tblPaymentTerms and tblInvoice_Status.

I'm able to get 90% of the way there but have a curious problem.  Here's my attempt so far:

SELECT     CONVERT(varchar, DATEADD(d, tblPaymentTerms.LateAfterDays, tblInvoice.DateSubmitted), 111) AS DueDate, COALESCE (details.SubTotal, 0)
                      - COALESCE (payments.Total, 0) + COALESCE (tblInvoice.Shipping, 0) AS BalanceDue
FROM         tblPaymentTerms INNER JOIN
                      tblInvoice ON tblPaymentTerms.ID = tblInvoice.fkPaymentTerms LEFT OUTER JOIN
                          (SELECT     fkInvoice, SUM(Amount) AS Total
                            FROM          tblInvoicePayment
                            GROUP BY fkInvoice) AS payments ON payments.fkInvoice = tblInvoice.ID LEFT OUTER JOIN
                          (SELECT     d.fkInvoice, SUM((d.Quantity * d.UnitPrice) * (1 + CASE d .Taxable WHEN 1 THEN i.SalesTaxRate ELSE 0 END)) AS SubTotal
                            FROM          tblInvoiceDetail AS d INNER JOIN
                                                   tblInvoice AS i ON i.ID = d.fkInvoice
                            GROUP BY d.fkInvoice) AS details ON details.fkInvoice = tblInvoice.ID
WHERE     (tblInvoice.fkInvoiceStatus IN (2, 3, 4, 5, 6, 7))
ORDER BY duedate

This gives me a list of invoices where each row returned includes the duedate and balance due on each invoice.  What I need is to SUM(BalanceDue) and GROUP BY DueDate.  However when I try the following query I get 'invalid column name duedate':

SELECT     CONVERT(varchar, DATEADD(d, tblPaymentTerms.LateAfterDays, tblInvoice.DateSubmitted), 111) AS DueDate, SUM(COALESCE (details.SubTotal, 0)
                      - COALESCE (payments.Total, 0) + COALESCE (tblInvoice.Shipping, 0)) AS BalanceDue
FROM         tblPaymentTerms INNER JOIN
                      tblInvoice ON tblPaymentTerms.ID = tblInvoice.fkPaymentTerms LEFT OUTER JOIN
                          (SELECT     fkInvoice, SUM(Amount) AS Total
                            FROM          tblInvoicePayment
                            GROUP BY fkInvoice) AS payments ON payments.fkInvoice = tblInvoice.ID LEFT OUTER JOIN
                          (SELECT     d.fkInvoice, SUM((d.Quantity * d.UnitPrice) * (1 + CASE d .Taxable WHEN 1 THEN i.SalesTaxRate ELSE 0 END)) AS SubTotal
                            FROM          tblInvoiceDetail AS d INNER JOIN
                                                   tblInvoice AS i ON i.ID = d.fkInvoice
                            GROUP BY d.fkInvoice) AS details ON details.fkInvoice = tblInvoice.ID
WHERE     (tblInvoice.fkInvoiceStatus IN (2, 3, 4, 5, 6, 7))
GROUP BY duedate
ORDER BY duedate

Can someone clue me into my mistake?  Thanks!

Answer : SQL sub query help needed

Try the below code.. you cannot group by on derived columns
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
SELECT     CONVERT(varchar, DATEADD(d, tblPaymentTerms.LateAfterDays, tblInvoice.DateSubmitted), 111) AS DueDate, SUM(COALESCE (details.SubTotal, 0) 
                      - COALESCE (payments.Total, 0) + COALESCE (tblInvoice.Shipping, 0)) AS BalanceDue
FROM         tblPaymentTerms INNER JOIN
                      tblInvoice ON tblPaymentTerms.ID = tblInvoice.fkPaymentTerms LEFT OUTER JOIN
                          (SELECT     fkInvoice, SUM(Amount) AS Total
                            FROM          tblInvoicePayment
                            GROUP BY fkInvoice) AS payments ON payments.fkInvoice = tblInvoice.ID LEFT OUTER JOIN
                          (SELECT     d.fkInvoice, SUM((d.Quantity * d.UnitPrice) * (1 + CASE d .Taxable WHEN 1 THEN i.SalesTaxRate ELSE 0 END)) AS SubTotal
                            FROM          tblInvoiceDetail AS d INNER JOIN
                                                   tblInvoice AS i ON i.ID = d.fkInvoice
                            GROUP BY d.fkInvoice) AS details ON details.fkInvoice = tblInvoice.ID
WHERE     (tblInvoice.fkInvoiceStatus IN (2, 3, 4, 5, 6, 7))
GROUP BY CONVERT(varchar, DATEADD(d, tblPaymentTerms.LateAfterDays, tblInvoice.DateSubmitted), 111)
ORDER BY duedate
Random Solutions  
 
programming4us programming4us