Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help writing a mssql 2000 script 1

Status
Not open for further replies.

tbscmgi

MIS
Sep 17, 2003
66
US
I have a number table with an account number and I want to find that account number in a balance table and when I find that account number I want the most recent one by date, if account not found I want to look in the previous month balance table.

Thank for any help I can get.
 
Ok,
Code:
JOIN (select b.accountno
        ,max(b.balancedate) as Date
         FROM #test_ANB a
         JOIN balance_2007jan b
         on a.number = b.accountno
        GROUP BY b.accountno) b

This is a derived table. It's name is b. the columns in your table are accountno and Date.
Do you see where you are trying to join c.balancedate to b.balancedate? That won't work because there is no field in b name balancedate. you need to change that to b.date. Make the same change in your select.



- Paul [batman]
- If at first you don't succeed, find out if the loser gets anything.
 
Paul-
This work fine.
Now I also want the account that not found in the balance table.

SELECT b.AccountNo, b.Date, c.OpenBalance
FROM balance_2007jan c
JOIN (select b.accountno, max(b.balancedate) as Date
FROM #test_ANB a
JOIN balance_2007jan b
ON a.number = b.accountno
GROUP BY b.accountno) b
ON c.accountno = b.accountno and c.balancedate = b.date
order by b.accountno


Thanks
Tony
 
Change the join to the derived table to left join.

- Paul [batman]
- If at first you don't succeed, find out if the loser gets anything.
 
Paul-
That work great.

Thank again for all of your help.

Thanks
Tony
 
no problem

- Paul [batman]
- If at first you don't succeed, find out if the loser gets anything.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top