I get the following error
Msg 102, Level 15, State 1, Line 11
Incorrect syntax near ')'.
when I try to run the following query
SELECT COUNT(*)
FROM tbl_usgGroupXref
WHERE GroupID = 478
AND accountNo IN (
SELECT TOP 5000 accountNo
FROM (
SELECT DISTINCT accountNo
FROM tbl_usgGroupXref
WHERE GroupID = 478
)
)
Currently, my query consists of a series of nested queries from a single table tbl_usgGroupXref. The table consists of multiple months of records for each account. The innermost query is to get the distinct accounts. The next outer query is to get any 5000 of those accounts (I couldn't seem to combine TOP with DISTINCT). And the outermost query is to get the count of the actual records represented by the 5000 accounts.
Could anyone shed some light on the error or revise this sql so that it works?
SELECT DISTINCT TOP 5000 accountNo