Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在子查询中查找具有重复列的行?_Sql_Sql Server - Fatal编程技术网

Sql 如何在子查询中查找具有重复列的行?

Sql 如何在子查询中查找具有重复列的行?,sql,sql-server,Sql,Sql Server,我正在处理和MS SQL Server Express中的数据 转储包含一个mapDenormalize表,其中包含名为solarSystemID和typeID的列,这两个列都是int(并且都不是键)。我正试图找到solarSystemID的值,这些值使用不同的typeID组合多次出现 我有一个类似的问题 -- all systems that have a plasma or temperate planet select distinct D.solarSystemID, D.typeID

我正在处理和MS SQL Server Express中的数据

转储包含一个mapDenormalize表,其中包含名为solarSystemID和typeID的列,这两个列都是int(并且都不是键)。我正试图找到solarSystemID的值,这些值使用不同的typeID组合多次出现

我有一个类似的问题

-- all systems that have a plasma or temperate planet
select distinct D.solarSystemID, D.typeID from mapDenormalize D 
  join invTypes T on D.typeID = T.typeID 
    where T.typeName in ('Planet (Plasma)', 'Planet (Temperate)') 
    order by solarSystemID
对于每个具有等离子行星的solarSystemID,返回一行;对于具有温带行星的solarSystemID,返回一行。我正试图找出如何将其作为子查询来查找两种行星都有但却空手而归的太阳系星体

我开始想我会做一些像

select solarSystemID from ( the above query ) where count(solarSystemID) > 1
但这并不是解析。正确的方法是什么

select D.solarSystemID, count(D.solarSystemID) as counts
from mapDenormalize D 
     join invTypes T 
     on D.typeID = T.typeID 
where T.typeName in ('Planet (Plasma)', 'Planet (Temperate)')
group by D.solarSystemID
having count(D.solarSystemID) > 1;

将子查询命名为AS,这样子查询中的列就可以用作参数进行计数和分组

-- all systems that have at least 1 each plasma and temperate planet
select distinct Foo.solarSystemID from 
    (select D.solarSystemID, D.typeID from mapDenormalize D join 
    invTypes T on D.typeID = T.typeID 
        where T.typeName in ('Planet (Plasma)', 'Planet (Temperate)')) 
    as Foo group by Foo.solarSystemID having COUNT(Foo.solarSystemID) > 1 

感谢@Michael指出了相关问题。

可能重复的@Michael是正确的,他的链接中的答案包含了一个关键点:用“AS”命名子查询,这样子查询中的列就可以命名了。