views:

146

answers:

1

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

A: 

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'
Remou
I think I might have tried this constraint positions here too. Will let you know how I get on. Thanks!
jtromans
By running the ownerGuild constraint here in this position within the query, it actually still returns values where scouting.ownerGuild = anything, any not just SWARM.
jtromans
Is scouting.jumpGate a unique field?
Remou
When I run the constraints in the position I placed it in within the question, I do indeed MISS some records, which is consistent with your point that "Otherwise it seems likely that the top 3 may include some ...where scouting.ownerGuild <> 'SWARM'" - Although I don't know why this is the case.
jtromans
no, it is not. the unique field in scouting tables is ID. jumpgate can be a tied value.
jtromans
... you need this part to match on a unique ID.
Remou
So Scouting.ID IN (SELECT TOP 3 Scouting.ID .... )
Remou
in place of scouting.jumpGate ?
jtromans
Yes. char char c
Remou
Looks very promising. Will manually check this but looks to be working perfectly!
jtromans
OK, this works great. Would you like to edit your answer accordingly and I'll tick it :) ?
jtromans
Done :) char ch
Remou