Only one expression can be specified in the select list when the subquery is not introduced with EXISTS Only one expression can be specified in the select list when the subquery is not introduced with EXISTS sql-server sql-server

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS


You can't return two (or multiple) columns in your subquery to do the comparison in the WHERE A_ID IN (subquery) clause - which column is it supposed to compare A_ID to? Your subquery must only return the one column needed for the comparison to the column on the other side of the IN. So the query needs to be of the form:

SELECT * From ThisTable WHERE ThisColumn IN (SELECT ThatColumn FROM ThatTable)

You also want to add sorting so you can select just from the top rows, but you don't need to return the COUNT as a column in order to do your sort; sorting in the ORDER clause is independent of the columns returned by the query.

Try something like this:

select count(distinct dNum) from myDB.dbo.AQ where A_ID in    (SELECT DISTINCT TOP (0.1) PERCENT A_ID    FROM myDB.dbo.AQ     WHERE M > 1 and B = 0    GROUP BY A_ID     ORDER BY COUNT(DISTINCT dNum) DESC)


You should return only one column and one row in the where query where you assign the returned value to a variable. Example:

select * from table1 where Date in (select * from Dates) -- Wrongselect * from table1 where Date in (select Column1,Column2 from Dates) -- Wrongselect * from table1 where Date in (select Column1 from Dates) -- OK


It's complaining about

COUNT(DISTINCT dNum) AS ud 

inside the subquery. Only one column can be returned from the subquery unless you are performing an exists query. I'm not sure why you want to do a count on the same column twice, superficially it looks redundant to what you are doing. The subquery here is only a filter it is not the same as a join. i.e. you use it to restrict data, not to specify what columns to get back.