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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:32:45+00:00 2026-06-16T23:32:45+00:00

Table – T1 (id, Description) Table – T2 (id, fk_id,Dates, Amount) select d.Description,dt.Dates,dt.Amount from

  • 0
 Table - T1 (id, Description)
 Table - T2 (id, fk_id,Dates, Amount)

select d.Description,dt.Dates,dt.Amount
 from T2 as dt join T1 d
  on
 d.id = dt.fk_id
group by d.Description,dt.Dates,dt.Amount

running above query results shows below

Description       Dates           Amount
nutrition1      2012-01-01      100.00
nutrition1      2013-01-01      100.00
nutrition2      2012-01-01      100.00
nutrition2      2013-01-01      600.00
nutrition3      2012-01-01      300.00
nutrition3      2013-01-01      700.00
nutrition4      2012-01-01      400.00
nutrition4      2013-01-01      800.00
nutrition5      2012-01-01      300.00
nutrition5      2013-01-01      600.00

i want to show result as

Description    01-01-2012  01-01-2013
nutrition1       100          100
nutrition2       100          600
nutrition3       300          700
nutrition4       400          800
nutrition5       300          600

Dates will change

  • 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-16T23:32:47+00:00Added an answer on June 16, 2026 at 11:32 pm

    This type of transformation is a PIVOT. If you know the values ahead of time you can hard-code them similar to this:

    select *
    from
    (
      select d.Description,convert(char(10), dt.dates, 110) dates,dt.Amount
      from T2 as dt 
      join T1 d
        on d.id = dt.fk_id
    ) src
    pivot
    (
      sum(amount)
      for dates in ([01-01-2012], [01-01-2013])
    ) piv
    

    See SQL Fiddle with Demo

    If you have unknown values to transform, then you will want to use dynamic sql:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(convert(char(10), dates, 110)) 
                        from T2
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT Description, ' + @cols + ' from 
                 (
                    select d.Description,convert(char(10), dt.dates, 110) dates,dt.Amount
                    from T2 as dt 
                    join T1 d
                      on d.id = dt.fk_id
                ) x
                pivot 
                (
                    sum(amount)
                    for dates in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    Both versions produce the same result:

    | DESCRIPTION | 01-01-2012 | 01-01-2013 |
    -----------------------------------------
    |  nutrition1 |        100 |        100 |
    |  nutrition2 |        100 |        600 |
    |  nutrition3 |        300 |        700 |
    |  nutrition4 |        400 |        800 |
    |  nutrition5 |        300 |        600 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Table structure: country season points Current query: SELECT SUM(points) AS total, country FROM table
Table 1: Student StudentID, StudentName Table 2: Course CourseID, CourseName Table 3: Group GroupID,
Table view center cell always highlight like a picker. If I select the center
Table (ipvote) Table (pictures) What is the SQL query to retrieve all the images
Table: laterecords ----------- studentid - varchar latetime - datetime reason - varchar My Query:
Table structure: Locations: id, name, city I want to select all the duplicates by
I have an Oracle 10g table containing 2 date columns, DATE_VALID_FROM from and DATE_VALID_TO.
Table Trips TripId_PK StartLocationId_FK EndLocationId_FK Table Locations LocationId_PK Name How can I join the
table : id (integer primary key) data (blob) I use mysql and sqlalchemy. To
Table --------------------------------------------- | id | first_name | middle_name | last_name | --------------------------------------------- | 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.