Complicated MS Access Greatest-N-Per-Group problem

2020-05-07 04:37发布

问题:

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

回答1:

How about:

Select  scouting.astroLoc, 
        scouting.galaxy, 
        scouting.jumpGate, 
        scouting.ownerGuild 
From    galaxy Inner Join 
        scouting On galaxy.[galaxy_ID] = scouting.galaxy 
WHERE   (scouting.ID) In (
             Select Top 3 scouting.ID
             From scouting 
             Where scouting.galaxy = galaxy.[galaxy_ID] 
             And scouting.ownerGuild = 'SWARM' 
             Order By scouting.jumpGate Desc) 
Order By    scouting.astroLoc Desc, 
                scouting.jumpGate Desc

Otherwise it seems likely that the top 3 may include some

  where scouting.ownerGuild <> 'SWARM'