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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:37:37+00:00 2026-05-19T13:37:37+00:00

I have this table structure. YearPart, MonthPart and DatePart contain what they describe… EX:

  • 0

I have this table structure. YearPart, MonthPart and DatePart contain what they describe… EX: 2011, 1, 19 (respectively)

DECLARE @agenda AS TABLE    (
  PID INT IDENTITY(1,1) PRIMARY KEY,
  YearPart int,
  MonthPart int,
  DayPart int,
  lib_title nvarchar(200),
  [filename] nvarchar(255),
  meta_value nvarchar(2000)
)

Using this sample data:

INSERT INTO @agenda VALUES (2010, 12, 4, 'Test Record', '', '')
INSERT INTO @agenda VALUES (2011, 1, 3, 'Another Record', '', '')
INSERT INTO @agenda VALUES (2011, 1, 3, 'Fred Birthday', '', '')
INSERT INTO @agenda VALUES (2011, 1, 4, 'Work Day', '', '')
INSERT INTO @agenda VALUES (2011, 12, 6, '2nd Test Record', '', '')

What I want, is an XML output like this:

<root>
  <Year Year="2010">
    <Month Month="12">
      <Day Day="4">
        <Item RecordName="Test Record" RecordID="1" />
      </Day>
    </Month>
  </Year>
  <Year Year="2011">
    <Month Month="1">
      <Day Day="3">
        <Item RecordName="Another Record" RecordID="2" />
        <Item RecordName="Geoffrey Birthday" RecordID="3" />
      </Day> 
      <Day Day="4">
        <Item RecordName="Work Day" RecordID="4" />
      </Day>
    </Month>
    <Month Month="12">
      <Day Day="6">
        <Item RecordName="2nd Test Record" RecordID="5" />
      </Day>
    </Month>
  </Year>
</root>

So far, I haven’t been able to get the nesting to work right. I usually end up with the grouping off (for example, I get multiple Year=2011 elements, when there should only be one).

If this can’t be done, I can always create the XML on the .NET site…

  • 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-19T13:37:38+00:00Added an answer on May 19, 2026 at 1:37 pm

    It can be done.

    select 
      a1.YearPart as '@Year',
      ( select MonthPart as '@Month',
          (select DayPart as '@Day',
             (select
                lib_title as '@RecordName', 
                PID as '@RecordID'
              from @agenda as a4
              where a4.DayPart = a3.DayPart and
                    a4.MonthPart = a2.MonthPart and
                    a4.YearPart = a1.YearPart
              for xml path('Item'), type         
             )
           from @agenda as a3
           where a3.YearPart = a1.YearPart and
                 a3.MonthPart = a2.MonthPart
           group by a3.DayPart
           for xml path('Day'), type      
          )
        from @agenda as a2
        where a1.YearPart = a2.YearPart
        group by a2.MonthPart
        for xml path('Month'), type
      )  
    from @agenda as a1
    group by YearPart
    for xml path('Year'), root
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this table structure on a SQL Server 2008 R2 database: CREATE TABLE
I have this table structure and would like to map it using Fluent Hibernate
I have my Table structure like this :: ATT_Table : Fields - Act_ID, Assigned_To_ID,
I have an table structure like this mysql> SELECT id, name, parent_id FROM categories;
I have a table structure with columns like this [ID] [Name] [ParentId] [ParentName] The
I have a table structure like this: <table> <tr> <td> <ul> <li class=check></li> </ul>
I have a 'users' SQL table structure like this (the ID is randomly generated
I have a 'users' SQL table structure like this (the ID is randomly generated,
If I have a table structure like this: Transaction [TransID, ...] Document [DocID, TransID,
I have this database structure, SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO; CREATE TABLE IF NOT EXISTS `announces` (

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.