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 9175147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:52:02+00:00 2026-06-17T16:52:02+00:00

Hey guys I’m trying to join together two queries and am having an issue,

  • 0

Hey guys I’m trying to join together two queries and am having an issue, maybe you can take a look, I have these two separate queries i’m trying to combine. There is a third join that will be in there to get a field that will count how many parents it also has which I haven’t started on.

SELECT
    e.baseName AS baseName, 
    s.baseName AS masterName
FROM dataSet e 
INNER JOIN dataSet s 
    ON s.id = e.entityId 
ORDER BY e.entityId

SELECT  
    e.baseName AS baseName, 
    b.baseName AS masterName, 
    COUNT(*)-1 AS siblingCount 
FROM dataSet e 
JOIN dataSet b 
    ON b.id=e.entityId 
GROUP BY b.id, b.baseName 

I came up with this, but am having issues with it

SELECT
    e.baseName AS baseName, 
    s.baseName AS masterName,
    COUNT(*)-1 AS siblingCount 
FROM dataSet e 
    JOIN dataSet b ON b.id = e.entityId
    INNER JOIN dataSet s ON s.id = e.entityId 
GROUP BY b.id, b.baseName 
ORDER BY e.entityId

Table structure is like so

+----+--------------------+----------+
| id | baseName           | entityId |
+----+--------------------+----------+
|  1 | 000000000000000001 |        1 |
|  2 | 000000000000000002 |        1 |
|  3 | 000000000000000003 |        1 |
|  4 | 000000000000000004 |        2 |
|  5 | 000000000000000005 |        2 |
|  6 | 000000000000000006 |        2 |
|  7 | 000000000000000007 |        2 |
|  8 | 000000000000000008 |        2 |
|  9 | 000000000000000009 |        3 |
| 10 | 000000000000000010 |        3 |
| 11 | 000000000000000011 |        3 |
| 12 | 000000000000000012 |        3 |
| 13 | 000000000000000013 |        3 |
| 14 | 000000000000000014 |        3 |
| 15 | 000000000000000015 |        3 |
| 16 | 000000000000000016 |        4 |
| 17 | 000000000000000017 |        4 |
| 18 | 000000000000000018 |        4 |
| 19 | 000000000000000019 |        4 |
| 20 | 000000000000000020 |        4 |
+----+--------------------+----------+

What I’m really looking for is this

+--------------------+--------------------+--------------+-------------+
|      baseName      |      masterName    | siblingCount | parentCount | 
+--------------------+--------------------+--------------+-------------+
| 000000000000000001 | 000000000000000001 |          999 |           0 |
| 000000000000000002 | 000000000000000001 |          998 |           1 |
| 000000000000000003 | 000000000000000001 |          998 |           1 |
| 000000000000000004 | 000000000000000002 |          997 |           2 |
| 000000000000000005 | 000000000000000002 |          997 |           2 |
| ....               | ....               |         .... |        .... |
+--------------------+--------------------+--------------+-------------+

Help would be appreciated

  • 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-06-17T16:52:03+00:00Added an answer on June 17, 2026 at 4:52 pm

    Okay, so my biggest hurdle was just understanding your data, but I think I got it after the “job”/”tree” analogy. Below is an SQL query that I think will get your desired output. There is also an sqlfiddle with the test data I used for you to play with. Hope this helps.

    SELECT
        e.baseName AS baseName, 
        s.baseName AS masterName,
        (SELECT COUNT(id) FROM dataSet WHERE entityId < e.entityId) as parentCount,
        (SELECT COUNT(id) FROM dataSet WHERE entityId > e.entityId) as siblingCount
      FROM 
        dataSet e 
        LEFT JOIN dataSet s ON s.id = e.entityId
      ORDER BY 
        e.entityId
    

    Good luck!

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

Sidebar

Related Questions

hey guys having this really simple problem but cant seem to figure out have
Hey guys am having a bit of problems with my jQuery, I have all
Hey guys I'm having a big problem. I've been trying to set up my
Hey guys, I have a problem (again). This time I am trying to use
hey guys hope you can help me out, I am having a weird problem.
Hey guys take a look at this program. /* The craps game, KN king
Hey guys I have a little issue with a function that retrieves data from
Hey guys I have a object sitting in my tile map for spawn point
Hey guys (and gals) I'm having a problem using Assembly GetExportedTypes() in .NET 4.0.
Hey guys got an issue with Cakephp validation.. I want to know why is

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.