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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:46:43+00:00 2026-06-10T21:46:43+00:00

Here is my Database structure : I want to display data like the following

  • 0

Here is my Database structure :

enter image description here

I want to display data like the following :

enter image description here

How can I do it ? Should I use pivot?

  • 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-10T21:46:45+00:00Added an answer on June 10, 2026 at 9:46 pm

    You can use PIVOT for this. There are two ways to do this with PIVOT, either a Static Pivot where you code the columns to transform or a Dynamic Pivot which determines the columns at execution.

    Static Pivot:

    select *
    from
    (
      select dt, username, product,
        'product' 
          + cast(row_number() 
                   over(partition by username order by product) as varchar(10)) productNum
      from yourtable
    ) x
    pivot
    (
      min(product)
      for productNum in ([product1], [product2], [product3])
    ) p
    order by dt
    

    See SQL Fiddle with Demo

    Dynamic Pivot:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = 
        STUFF((SELECT distinct ',' + Quotename('product' 
                  + cast(row_number() 
                   over(partition by username order by product) as varchar(10)))
                 from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query 
        = 'SELECT dt, username,' + @cols + ' from 
           (
            select dt, username, product,
               ''product''
                + cast(row_number() 
                   over(partition by username order by product) as varchar(10)) productNum
            from yourtable
           ) x
           pivot 
           (
              min(product)
              for productNum in (' + @cols + ')
           ) p 
            order by dt'
    
    execute(@query)
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

hope someone can help me out here. I have the following structure in my
I have the following database structure: Notice that the field fk_parent_group_id can either be
my database table structure is like following: wdt date teacherid 1 2011-01-11 1001 2
Here is my database structure. Relation many to many. I want to make a
Database is OracleXE and here is the problem: data gets entered in tables UPS
I have the following table structure in my database: create table Encargado( ID int
I have a database table structure as follow: datatype data mytable now datatype has
here is my database structure members table member_Id membertomshiptable membertomship_startdate membertomship_enddate member_id mshipstatustype_id membershipstatustypetable
Here an example my database structure.. uid email name ------------------------------- 1 john@gmail.com John 2
I have a database structure like this rowid deltaValue Applicable 1 r n/d 1

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.