• crazy_new (2/12/2014)


    Hi, Could anyone please assist me with the following:

    SELECT InvNo,InDate,CaseNo,ClientRef,Curr,InvFullAmount,InvOutstAmount From DebtTran

    FROM DebtTran

    WHERE NameNo = (SELECT TOP 1 (NameNo) FROM DebtTran).

    How would this be written in MS Access.

    Thanks in advance.

    This query won't work in any DBMS as written. There are two FROM clauses.

    Why are you using a subquery here? Your subquery is the same table as the base query. The way you have this written it is possible to return more than 1 row. If you have two rows in your table with the NameNo that is returned by your subquery you will get two rows.

    Also, as Greg eluded to, you should not use a TOP without and ORDER BY. When you don't include an order by you have no way of knowing which row you will get back.

    You could greatly simplify this like this:

    SELECT TOP 1 InvNo, InDate, CaseNo, ClientRef, Curr, InvFullAmount, InvOutstAmount

    From DebtTran

    order by NameNo --or whatever column makes sense

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/