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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:46:55+00:00 2026-05-31T11:46:55+00:00

I am using MS-SQL and I am trying to write a query which fetches

  • 0

I am using MS-SQL and I am trying to write a query which fetches rows from the master table and related rows from the detail table. Now, the thing I want is that it must only fetch the first row from the master table and related field from the detail tables should be blank in that first row, now if there are related rows found in the detail tables, they must be shown in the separate rows. I have been trying using the following query but it is not giving the desired result.

SELECT 
      DISTINCT 
      ProductMaster.ProductMasterID, ProductMaster.ProductName,
      ProductMaster.SubCategoryID, SubCategory.SubCategoryName,
      ProductBrandAndType.ProductBranAndTypeID, ProductBrandAndType.ProductType,
      ProductBrandAndType.Brand, ProductMaster.ProductDesc, 
      ProductMaster.ReOrderLevel 
FROM 
   ProductBrandAndType 
RIGHT OUTER JOIN 
   Inward 
ON ProductBrandAndType.ProductBranAndTypeID = Inward.ProductBrandAndTypeID 
RIGHT OUTER JOIN 
   ProductMaster 
ON Inward.ProductID = ProductMaster.ProductMasterID 
LEFT OUTER JOIN 
   SubCategory 
ON ProductMaster.SubCategoryID = SubCategory.SubCategoryID 
ORDER BY 
      ProductMaster.ProductName, 
      ProductBrandAndType.ProductType, 
      ProductBrandAndType.Brand;

Can anyone help me on this?

Regards

Sikandar

  • 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-31T11:46:57+00:00Added an answer on May 31, 2026 at 11:46 am

    Following query worked.

    SELECT     dbo.ProductMaster.ProductMasterID, dbo.ProductMaster.ProductName, dbo.ProductMaster.SubCategoryID, dbo.ProductMaster.ProductDesc, 
                          dbo.ProductMaster.ReOrderLevel, null as ProductBrandAndTypeID,  
                          null AS Type, null as Brand
    FROM         dbo.ProductBrandAndType RIGHT OUTER JOIN
                          dbo.Inward ON dbo.ProductBrandAndType.ProductBranAndTypeID = dbo.Inward.ProductBrandAndTypeID RIGHT OUTER JOIN
                          dbo.ProductMaster ON dbo.Inward.ProductID = dbo.ProductMaster.ProductMasterID LEFT OUTER JOIN
                          dbo.SubCategory ON dbo.ProductMaster.SubCategoryID = dbo.SubCategory.SubCategoryID
    UNION 
    SELECT     dbo.ProductMaster.ProductMasterID, dbo.ProductMaster.ProductName, dbo.ProductMaster.SubCategoryID, dbo.ProductMaster.ProductDesc, 
                          dbo.ProductMaster.ReOrderLevel, dbo.ProductBrandAndType.ProductBranAndTypeID,  
                          dbo.ProductBrandAndType.ProductType, dbo.ProductBrandAndType.Brand
    FROM         dbo.ProductBrandAndType RIGHT OUTER JOIN
                          dbo.Inward ON dbo.ProductBrandAndType.ProductBranAndTypeID = dbo.Inward.ProductBrandAndTypeID RIGHT OUTER JOIN
                          dbo.ProductMaster ON dbo.Inward.ProductID = dbo.ProductMaster.ProductMasterID LEFT OUTER JOIN
                          dbo.SubCategory ON dbo.ProductMaster.SubCategoryID = dbo.SubCategory.SubCategoryID                      
    ORDER BY ProductName;   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a sql query which fetches all the tags related
I am trying to write a SQL query to fetch rows whose fields contain
I'm trying to write a dynamic Linq 2 Sql query using Expressions trees but
I am trying to write a select query from one single table in SQLite
I'm trying to write some code using pure SQL using ASP.NET MVC. I assume
I am trying to learn SQL query optimization using SQL Server 2005. However, I
I am trying to query the windows desktop search API using SQL. I have
I am trying to insert certain values into the table using SQL server 2005.
I'm trying to write a Microsoft SQL Server 2005 query that counts a total
OK trying to write a SQL query to a text file. I have tried

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.