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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:44:40+00:00 2026-06-02T15:44:40+00:00

I have a data set that looks something like this: Gender | Age |

  • 0

I have a data set that looks something like this:

   Gender | Age | Name
    Male  | 30  | Bill
  Female  | 27  | Jenny
  Female  | 27  | Debby 
   Male   | 44  | Frank

And I’m trying to display this as specially formatted HTML code:

    <ul>
      <li>Male
        <ul>
          <li>30
            <ul>
              <li>Bill</li>
            </ul>
          </li>
          <li>44
            <ul>
              <li>Frank</li>
            </ul>
          </li>
        </ul>  
      </li>
    </ul>

    <ul>
      <li>Female
        <ul>
          <li>27
            <ul>
              <li>Jenny</li>
              <li>Debby</li>
            </ul>
          </li>
        </ul>  
      </li>
    </ul>

I tried using FOR XML but that didn’t give the results I was looking for. It didn’t remove the multiple Gender and Age fields returned. As you can see in this HTML it is compounding it all and only giving duplicates at the end node.

Aaron Bertrand provided an excellent method here Return Select Statement as formatted HTML that seems to work perfectly in SQL 2008, but I was looking for something that worked in 2005 as well, besides the little stuff, like += operators and setting default DECLARE values, just doesn’t display in 2005.

How would something like this be achieved in SQL Server 2005?

  • 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-02T15:44:41+00:00Added an answer on June 2, 2026 at 3:44 pm
    declare @T table
    (
      Gender varchar(10),
      Age int,
      Name varchar(10)
    )
    
    insert into @T values ('Male',   30,   'Bill')
    insert into @T values ('Female', 27,   'Jenny')
    insert into @T values ('Female', 27,   'Debby') 
    insert into @T values ('Male',   44,   'Frank')
    
    select Gender as 'li',
           (select T2.Age as 'li',
                   (select T3.Name as 'li'
                    from @T as T3
                    where T2.Age = T3.Age and
                          T1.Gender = T3.Gender
                    for xml path(''), root('ul'), type) as 'li'
            from @T as T2
            where T1.Gender = T2.Gender
            group by T2.Age
            for xml path(''), root('ul'), type) as 'li'
    from @T as T1
    group by Gender
    for xml path('ul')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data set that looks something like this: Gender | Age |
I have a KnockoutJS template that looks something like this: <div data-bind=template: {name: 'testTemplate',
I have data that always looks something like this: alt text http://michaelfogleman.com/static/images/chart.png I need
I have a huge data set. The structure looks something like this: K_Field1, K_Field2,
I have a base Color class that looks something like this. The class is
I have html that looks something like this: <div> <table> <tr onclick=show();> <td class=cell>Click
I have an Oracle table with data that looks like this: ID BATCH STATUS
Let's say I have a manager that looks something like this: public class CustomerManager
I have a data set that looks like the below (the input). IR# CR#
I have a table code_prices that looks something like this: CODE | DATE |

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.