I am looking to combine the following queries into one, where
- scouting.jumpGate is integer,
- scouting.astroLoc is a string,
- scouting.ownerguild is a string and
- scouting.galaxy is a integer
that cross-links to another table (and is my GROUP):
Select TOP 3
scouting.jumpGate,
scouting.astroLoc,
scouting.ownerGuild,
scouting.Galaxy
FROM scouting
WHERE scouting.Galaxy = 1
AND scouting.ownerGuild = 'TEST'
ORDER BY scouting.jumpGate DESC,
scouting.astroloc DESC;
and
SELECT TOP 3
scouting.jumpGate,
scouting.astroLoc,
scouting.ownerGuild,
scouting.Galaxy
FROM scouting
WHERE scouting.Galaxy = 2
AND scouting.ownerGuild = 'TEST'
ORDER BY scouting.jumpGate DESC,
scouting.astroloc DESC;
and
SELECT TOP 3
scouting.jumpGate,
scouting.astroLoc,
scouting.ownerGuild,
scouting.Galaxy
FROM scouting
WHERE scouting.Galaxy = 3
AND scouting.ownerGuild = 'TEST'
ORDER BY scouting.jumpGate DESC,
scouting.astroloc DESC;
continued until
SELECT TOP 3
scouting.jumpGate,
scouting.astroLoc,
scouting.ownerGuild,
scouting.Galaxy
FROM scouting
WHERE scouting.Galaxy = 79
AND scouting.ownerGuild = 'TEST'
ORDER BY scouting.jumpGate DESC,
scouting.astroloc DESC;
The code I have generated after reading on Microsoft's website for this Greatest N Per Group problem is as follows:
Select scouting.astroLoc,
scouting.galaxy,
scouting.jumpGate,
scouting.ownerGuild
From galaxy Inner Join
scouting On galaxy.[galaxy_ID] = scouting.galaxy
Where scouting.ownerGuild = 'SWARM'
AND (scouting.jumpGate) In (Select Top 3 scouting.jumpGate From scouting Where scouting.galaxy = galaxy.[galaxy_ID] Order By scouting.jumpGate Desc)
Order By scouting.astroLoc Desc,
scouting.jumpGate Desc
Basically, this is very close to what I would like. Everything seems to work. However, some of the GROUPS are not represented in the output even thought after eyeballing the data, each group has a record that satisfies the constraints of the query. Incidentally, if I take out the scouting.ownerGuild = 'SWARM' constraint, it works perfectly (but I need this constraint).