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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:56:31+00:00 2026-06-13T02:56:31+00:00

I have following table ID | Group | Type | Product 1 Dairy Milk

  • 0

I have following table

ID | Group      | Type         | Product
1    Dairy         Milk           Fresh Milk
2    Dairy         Butter         Butter Cream
3    Beverage      Coke           Coca cola
4    Beverage      Diet           Dew
5    Beverage      Juice          Fresh Juice

I need following output/query result:

ID | Group      | Type         | Product
1    Dairy             
1                 Milk           Fresh Milk
2                 Butter         Butter Cream
2    Beverage      
1                Coke            Coca cola
2                Diet            Dew
3                Juice           Fresh Juice

For above sample a hard coded script can do the job but I look for a dynamic script for any number of groups. I do not have any idea how it can be done so, I do not have a sample query yet. I need ideas, examples that at least give me an idea. PIVOT looks a close option but does not looks to be fully fit for this case.

  • 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-13T02:56:32+00:00Added an answer on June 13, 2026 at 2:56 am

    Here’s a possible way. It basically unions the “Group-Headers” and the “Group-Items”. The difficulty was to order them correctly.

    WITH CTE AS
    (
      SELECT ID,[Group],Type,Product,
        ROW_NUMBER() OVER (PARTITION BY [Group] Order By ID)AS RN
      FROM Drink
    )
    SELECT ID,[Group],Type,Product
    FROM(
       SELECT RN AS ID,[Group],[Id]AS OriginalId,'' As Type,'' As Product, 0 AS RN, 'Group' As RowType
       FROM CTE WHERE RN = 1
       UNION ALL
       SELECT RN AS ID,'' AS [Group],[Id]AS OriginalId,Type,Product, RN, 'Item' As RowType
       FROM CTE
    )X
    ORDER BY  OriginalId ASC
    , CASE WHEN RowType='Group' THEN 0 ELSE 1 END ASC
    , RN ASC
    

    Here’s a demo-fiddle: http://sqlfiddle.com/#!6/ed6ca/2/0

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

Sidebar

Related Questions

I have the following type of query: SELECT * FROM TABLE GROUP BY Table.Table_ID
I have the following two table: -Groups- Id Name -Members- Id GroupId (Group.Id is
I have the following example in a SQL table Cust Group Sales A 1
I have the following code to generate a table listing brand/model/submodel/style for a group
I have a products table, with the fields product, category and cost, of type
I have a table with the following rows: id. user_id, type - link 1.
I have following mysql table: Item Name Listing Fee Listing Type watch $0.20 LISTED
I have the following table : group_id amount type 1 10 1 1 2
I have the following table but I havent been able to group it, by
I have the following table ID1 ID2 Type 1 x A 1 y A

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.