简体   繁体   中英

how to select Where column_name in (subquery)

I'd like to select all the rows of a table where the value of a column is in the results of a query. conceptually the code below seems like it should work but I'm not sure whether it's a syntax error


SELECT * FROM generated.existing_conditions ec 
WHERE st_to IN (
    SELECT st_to FROM 
        (SELECT st_to AS st_to, COUNT(*) AS total_count 
         FROM generated.existing_conditions ec GROUP BY st_to) AS source_1
    WHERE total_count > 1
) source_2
;

Is this simply not allowed? can I rewrite it as a WITH query AS ?

My goal is to select the unique id's of any row with an attribute value that is repeated as this is likely to be an error in the data.

Your query is fine, although the two levels of subquery are not needed. You could rewrite it as:

SELECT *
FROM generated.existing_conditions ec 
WHERE st_to IN (SELECT ec2.st_to FROM 
                FROM generated.existing_conditions ec2
                GROUP BY ec2.st_to
                HAVING COUNT(*) > 1
               ) ;

However, I would suggest window functions instead:

select ec.*
from (select ec.*, count(*) over (partition by st_to) as cnt
      from generated.existing_conditions ec 
     ) ec
where cnt > 1;

Both the subqueries could be written using CTEs if you prefer CTEs.

compared to what I posted, the error is fixed below by removing the source_2 alias.

SELECT * FROM generated.existing_conditions ec 
WHERE st_to IN (
    SELECT st_to FROM 
        (SELECT st_to AS st_to, COUNT(*) AS total_count FROM generated.existing_conditions ec GROUP BY st_to) AS source_1
    WHERE total_count > 1
); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM