This may not be a newbie question, but I consider myself a newbie.

  • I use this query in 'Query Analyzer':

    SELECT TOP 100 PERCENT dbo.customer.username, SUM(dbo.iheader.total) - SUM(dbo.iheader.paid) AS balance

    FROM dbo.customer INNER JOIN

    dbo.iheader ON dbo.customer.id = dbo.iheader.guarantor

    WHERE (dbo.customer.storeid = 1) AND (dbo.customer.active = 'n') and balance = 29.31

    GROUP BY dbo.customer.username

    But it says:

    Server: Msg 207, Level 16, State 3, Line 1

    Invalid column name 'balance'.

    Server: Msg 207, Level 16, State 1, Line 1

    Invalid column name 'balance'.

    I have done something similar before why is it not working now?

  • SELECT TOP 100 PERCENT dbo.customer.username, SUM(dbo.iheader.total) - SUM(dbo.iheader.paid) AS balance

    FROM dbo.customer INNER JOIN

    dbo.iheader ON dbo.customer.id = dbo.iheader.guarantor

    WHERE (dbo.customer.storeid = 1) AND (dbo.customer.active = 'n')

    GROUP BY dbo.customer.username

    having SUM(dbo.iheader.total) - SUM(dbo.iheader.paid) = 29.31

    Also this seems more understandable to me (doesn't make it better or more right) >> SUM(dbo.iheader.total - dbo.iheader.paid) AS balance

  • Also the TOP 100 Percent is useless without an order by. You either add the order by if required or drop the TOP 100 PERCENT.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply