简体   繁体   English

如何在(子查询)中选择 Where column_name

[英]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 ?我可以将其重写为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.我的目标是选择具有重复属性值的任何行的唯一 ID,因为这可能是数据中的错误。

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.如果您更喜欢 CTE,则可以使用 CTE 编写两个子查询。

compared to what I posted, the error is fixed below by removing the source_2 alias.与我发布的内容相比,通过删除source_2别名修复了以下错误。

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
); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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