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

  • Home
  • SEARCH
  • 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 8976173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:04:07+00:00 2026-06-15T19:04:07+00:00

I am using Self Join to retrieve data from SQLSERVER Database and displaying the

  • 0

I am using Self Join to retrieve data from SQLSERVER Database and displaying the data in GridView.

Data in DepartmentMst table:

DepartmentID      DepartmentName       IsActive DepartmentParentID
3                    Admin Panel       1              0
4                    Human Resource    1              0
5               Information Technology 1              0
6                     Operational      1              0
21                    TestDepartment   1              3
22                       Logistics     1              3

From above data you can see that AdminPanel Department has its SubDepartment TestDepartment and Logistics with both DepartmentParentID=3 which belongs to main Department i.e AdminPanel.

Now want to display the Data in grid view which will show the DepartmentName and its SubDepartments.

My Query:

SELECT  dpt1.DepartmentID as DepartmentID
         ,dpt1.DepartmentName as DepartmentName
        ,dpt1.DepartmentParentID as ParentDepartmentID
        ,dpt2.DepartmentName as ChildDepartment
        ,dpt1.IsActive
  FROM DepartmentMst dpt1,DepartmentMst dpt2
  where (dpt1.DepartmentID=dpt2.DepartmentParentID) and dpt2.IsActive=1
  order by DepartmentID

O/P:

DepartmentID    DepartmentName  ParentDepartmentID  ChildDepartment  IsActive
3                   Admin Panel    0                   TestDepartment    1
3                   Admin Panel    0                     Logistics      1

But, I need all the DepartmentName with SubDepartmentName. If any Department has no Parent i.e DepartmentParentID=0, then it must be Shown as MainDepartment else its Parent Department Name

Eg:

DepartmentID    DepartmentName  IsActive    DepartmentParentID  ParentDeptName
3                   Admin Panel     1             0                MainDept
4                 Human Resource    1             0                MainDept
5          Information Technology   1             0                MainDept
6                     Operational   1             0                MainDept
21                 TestDepartment   1             3              AdminPanel
22                  Logistics       1             3              AdminPanel

How to get the above out put?
Help 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-15T19:04:09+00:00Added an answer on June 15, 2026 at 7:04 pm

    Use a LEFT JOIN:

    SELECT
        dpt1.DepartmentID as DepartmentID
        ,dpt1.DepartmentName as DepartmentName
        ,dpt2.DepartmentParentID as ParentDepartmentID
        ,ISNULL(dpt2.DepartmentName, 'MainDept') as ParentDepartment
        ,dpt1.IsActive
    FROM DepartmentMst dpt1
        LEFT JOIN DepartmentMst dpt2
            ON (dpt2.DepartmentID = dpt1.DepartmentParentID) and dpt2.IsActive=1
    ORDER BY DepartmentID
    

    If the join condition has no match then all columns from dpt2 will come back as NULL so you can test for that.

    Edit: I tried to fix the columns in your query as they didn’t seem correct. dpt1 is your “current” table and dpt2 is where the parent will be (if any)

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

Sidebar

Related Questions

Using the answer from previous Question? MYSQL SQL (self) join? Using views is unworkable.
i'm using cursor to loop through a self join table. the query that i
I am using a self-join to get year-to-date totals. This works: SELECT tc.EmployeeID, tc.TimeCardNum,
I am trying to join 2 strings using this code: def __get_temp(self): return float(self.ask('RS'))
I'm trying to put together a Self Join using a Group By clause to
I need to do self join using Symfony 1.4/Propel 1.4. My tables/db are too
I'm just wondering from design, efficiency and space perspectives, what's a better choice? Self-join
I have a table with navigation that joins back on its self using ParentId.
I'm using Self Tracking Entities that implements IObjectWithChangeTracker with the last Entity Framework RC
this is the code I'm using: self::$DB->prepare($query, $types); when the $query and types are:

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.