|
-
Oct 6th, 2008, 06:31 PM
#1
Thread Starter
Fanatic Member
[Access SQL] Missing Records with SubQuery
I created this subquery, which I thought would pull the Account Number, CustomerName, Status, OriginalBalanceDue, CurrentBalanceDue. However there are 4 accounts missing.
Could you look at my sql, and see if I am just missing something?
Code:
SELECT T1.AccountNumber,
(SELECT TOP 1 T2.FULLNAME FROM tblAKDCustomer AS T2 WHERE T2.AccountNumber=T1.AccountNumber ORDER BY T2.ImportDate DESC, T2.FullName) AS CustomerName,
tblAKDStatus.Name AS Status,
(SELECT TOP 1 T3.BalanceDue FROM tblAKDBalanceDue AS T3 WHERE T3.AccountNumber=T1.AccountNumber ORDER BY T3.BalanceDueDate, T3.ImportDate, T3.BalanceDue) AS OriginalBalanceDue,
(SELECT TOP 1 T3.BalanceDue FROM tblAKDBalanceDue AS T3 WHERE T3.AccountNumber=T1.AccountNumber ORDER BY T3.BalanceDueDate, T3.ImportDate DESC, T3.BalanceDue) AS BalDueToDate
FROM tblAKDStatus INNER JOIN tblAKDAccount AS T1 ON tblAKDStatus.StatusID=T1.StatusID
ORDER BY tblAKDStatus.Name, T1.AccountNumber;
Even if I remove most of the other information, I still end up missing 1-2 records. Its odd, I can run the same query over and over. And in the span of 15 seconds get 3 different totals back. Sometimes it returns all the accounts, sometimes it only returns some of them.
Code:
SELECT T1.AccountNumber,
(SELECT TOP 1 T2.FULLNAME FROM tblAKDCustomer AS T2 WHERE T2.AccountNumber=T1.AccountNumber ORDER BY T2.ImportDate DESC, T2.FullName) AS CustomerName,
tblAKDStatus.Name AS Status
FROM tblAKDStatus INNER JOIN tblAKDAccount AS T1 ON tblAKDStatus.StatusID=T1.StatusID
How can I better write my SubQuery so that it is not randomly getting less records?
Please RATE posts, click the RATE button to the left under the Users Name.
Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.
"As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|