-2

I am working with the data in the EVE static dump and MS SQL Server Express.

The dump contains a table mapDenormalize, having columns named solarSystemID and typeID, both of which are ints (and neither of which is a key). I'm trying to find values of solarSystemID that appear more than once, with various combinations of typeID.

I have a query like

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

which returns 1 row for each solarSystemID that has a Plasma planet and 1 row for each with a Temperate planet. I am trying to figure out how to use this as a subquery to find solarSystemIDs that have both kinds of planets, but have come up empty-handed.

I started out thinking that I would do something like

select solarSystemID from ( the above query ) where count(solarSystemID) > 1

but that doesn't parse. What is the correct approach?

wades
  • 927
  • 9
  • 24
  • possible duplicate of [How can I remove duplicate rows?](http://stackoverflow.com/questions/18932/how-can-i-remove-duplicate-rows) – Michael Oct 07 '13 at 17:54
  • 1
    @Michael is correct, the answer at his link contains the key: naming the subquery with "AS" so the columns in the subquery can be named. – wades Oct 07 '13 at 18:04

2 Answers2

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

The sqlfiddle

MrSimpleMind
  • 7,890
  • 3
  • 40
  • 45
-1

Name the subquery with AS, so the columns in it can be used as arguments to COUNT and GROUP BY

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

thanks to @Michael for pointing out the relevant question.

wades
  • 927
  • 9
  • 24