Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 478889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:44:14+00:00 2026-05-13T00:44:14+00:00

I want to combine Select top 2 scouting.* From scouting Where scouting.astroLoc Like ‘D01%’

  • 0

I want to combine

Select top 2 scouting.*
From scouting
Where scouting.astroLoc Like 'D01%' AND scouting.ownerGuild = 'SWARM'
Order By scouting.jumpGate Desc

with

Select top 2 scouting.*
From scouting
Where scouting.astroLoc Like 'D02%' scouting.ownerGuild = 'SWARM'
Order By scouting.jumpGate Desc

with

Select top 2 scouting.*
From scouting
Where scouting.astroLoc Like 'D03%' scouting.ownerGuild = 'SWARM'
Order By scouting.jumpGate Desc

continued until

Select top 2 scouting.*
From scouting
Where scouting.astroLoc Like 'D79%' scouting.ownerGuild = 'SWARM'
Order By scouting.jumpGate Desc

… into 1 SQL query whereby the TOP 3 records are grouped by scouting.astroLoc ascending.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:44:14+00:00Added an answer on May 13, 2026 at 12:44 am

    This is the “greatest-n-per-group” problem that is posted frequently on StackOverflow. Here’s a solution:

    SELECT s1.*
    FROM scouting s1
    LEFT OUTER JOIN scouting s2
      ON (s1.astroLoc = s2.astroLoc AND s1.jumpGate < s2.jumpGate)
    WHERE s1.astroLoc LIKE 'D[3-7][0-9]%' AND s1.astroLoc NOT LIKE 'D3[0-2]%'
    GROUP BY s1.* -- here you need to name all fields in the select-list
    HAVING COUNT(*) < 2;
    

    This works because the query tries to match a given row s1 to the set of rows s2 that have the same astroLoc and a greater jumpGate value. The HAVING clause restricts the result to s1 rows that match fewer than two, which means that the row would be in the top 2.

    This assumes rows are unique over [astroLoc, jumpGate]. If not, you may need to add another term to the join condition to resolve ties.


    Re your comment, try the following alteration:

    SELECT s1.*
    FROM scouting s1
    LEFT OUTER JOIN scouting s2
      ON (SUBSTRING(s1.astroLoc, 1, 3) = SUBSTRING(s2.astroLoc, 1, 3)
          AND (s1.jumpGate < s2.jumpGate OR (s1.jumpGate = s2.jumpGate AND s1.ID < s2.ID))
    WHERE s1.astroLoc LIKE 'D[3-7][0-9]%' AND s1.astroLoc NOT LIKE 'D3[0-2]%'
    GROUP BY s1.* -- here you need to name all fields in the select-list
    HAVING COUNT(*) < 2;
    

    This compares only the first three characters of astroLoc for purposes of testing a row is in the same “group” as the other, and it also resolves ties in jumpGate by using the primary key.


    Re your other answer with new requirements:

    where does the scouting.ownerGuild = ‘SWARM’ go?

    It’s hard to follow what you’re asking for, since I don’t know what are your table definitions or the meanings of columns. Do you want the outer query to be matched to the top three jumpgates that are owned by the SWARM guild?

    SELECT s1.astroLoc, g.[galaxy_aename], s1.jumpGate, s1.ownerGuild
    FROM galaxy g INNER JOIN scouting s1 ON g.[galaxy_ID] = s1.galaxy 
    WHERE s1.jumpGate IN (SELECT TOP 3 s2.jumpGate FROM scouting AS s2
            WHERE s2.galaxy = g.[galaxy_ID] AND s2.ownerGuild = 'SWARM'
            ORDER BY s2.jumpGate DESC)
    ORDER BY scouting.astroLoc DESC, scouting.jumpGate DESC
    

    That would be a different query from this one, which makes the outer query return the jumpgates owned by SWARM that match the top three jumpgates owned by anyone.

    SELECT s1.astroLoc, g.[galaxy_aename], s1.jumpGate, s1.ownerGuild
    FROM galaxy g INNER JOIN scouting s1 ON g.[galaxy_ID] = s1.galaxy 
    WHERE s1.jumpGate IN (SELECT TOP 3 s2.jumpGate FROM scouting AS s2
            WHERE s2.galaxy = g.[galaxy_ID]
            ORDER BY s2.jumpGate DESC)
      AND s1.ownerGuild = 'SWARM'
    ORDER BY scouting.astroLoc DESC, scouting.jumpGate DESC
    

    It’s possible the second query will return an empty result, if none of the SWARM jumpgates are in the top three.

    PS: It’s customary on StackOverflow to edit your original question post at the top, when you need to add more detail or followup questions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to combine to tables: the sql is: select name, extract(DAY from fechaNacimiento)
I want to combine the following two statements: select password from sys.user$ where name='SYSMAN';
I want to combine the following queries into a single query: SELECT val FROM
I want to do a select in MySql that combines several columns... something like
I have to select requests that i want to combine using UNION : Table
* revised, but still not solved...* I want to combine data from two files
I want to combine the data from a table and from a pipelined table
I have 2 separate arrays being pulled from MYSQL database, and want to combine
I want to insert top 20 ROWS from a table tbl_A in db_A to
How do you combine multiple select count(*) from different table into one return? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.