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

  • Home
  • SEARCH
  • 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 7942585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:01:06+00:00 2026-06-04T00:01:06+00:00

How to display the date by column wise Table1 ID Date Value 001 01/01/2012

  • 0

How to display the date by column wise

Table1

ID Date Value

001 01/01/2012 100
001 02/01/2012 200
...
...
001 31/01/2012 250
002 01/01/2012 050
002 02/01/2012 100
...
002 31/01/2012 075
....

I want to display the value row by date wise row group by id riw

Expected output

ID 01/01/2012 02/01/2012 ... 31/01/2012

001  100 200 .... 250
002  050 100 .... 075
.....

How to do this with a SQL query?

  • 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-04T00:01:08+00:00Added an answer on June 4, 2026 at 12:01 am

    Try this

    DECLARE @cols NVARCHAR(2000)
    DECLARE @query NVARCHAR(4000)
    
    SELECT  @cols = STUFF(( SELECT DISTINCT 
                                    '],[' + `Date`
                            FROM    Table1
                            ORDER BY '],[' + `Date` FOR XML PATH('')
                          ), 1, 2, '') + ']';
    
    
    SET @query = N'SELECT ID, '+
    @cols +'
    FROM
    (SELECT  ID
    FROM    Table1
    PIVOT
    (
    SUM([`Value`])
    FOR `Date` IN
    ( '+
    @cols +' )
    ) AS pvt
    ORDER BY ID;'
    
    
    EXECUTE(@query)
    

    SQL2000 version

    DECLARE @date Date,@sql nvarchar(MAX)
    DECLARE date_cursor CURSOR
      FOR SELECT DISTINCT [Date] FROM table_one ORDER BY [Date]
      SET @sql = ''
      OPEN date_cursor
      FETCH NEXT FROM date_cursor INTO @date
      WHILE @@FETCH_STATUS = 0
         BEGIN
             SET @sql = @sql+ ',MAX(CASE CONVERT(nvarchar(10),[Date],103) WHEN '''+CONVERT(nvarchar(10),@date,103)+''' THEN [Value] END) AS ['+CONVERT(nvarchar(10),@date,103)+']'
             FETCH NEXT FROM date_cursor INTO @date
         END 
    CLOSE date_cursor
    DEALLOCATE date_cursor
    
    EXEC('SELECT ID'+@sql+' FROM table_one GROUP BY ID')
    

    Or

    DECLARE @loop int,@date Date,@sql nvarchar(4000)
    DECLARE @TempTable TABLE
    (
      [Date] DATE
    )
    INSERT INTO @TempTable SELECT DISTINCT [Date] FROM table1 ORDER BY [Date]
      SET @sql = ''
      SET @loop = 1
      WHILE (@loop<=31)
        BEGIN
            IF EXISTS(SELECT * FROM @TempTable WHERE DAY([Date])=@loop) 
            BEGIN
                SET @date = (SELECT [Date] FROM @TempTable WHERE DAY([Date])=@loop)
                SET @sql = @sql+ ',MAX(CASE CONVERT(nvarchar(10),[Date],103) WHEN '''+CONVERT(nvarchar(10),@date,103)+''' THEN [Value] END) AS [DATE'+CONVERT(nvarchar(2),@loop)+']'
             END
             ELSE
                SET @sql = @sql+ ', NULL AS [DATE'+CONVERT(nvarchar(2),@loop)+']'
             SET @loop = @loop+1
        END 
    
    EXEC('SELECT ID'+@sql+' FROM table1 GROUP BY ID')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Server 2000 Table1 ID Date Value1 Value2 001 01/01/2012 100 0 001
I want to display a date/time submitted value much the same way as Stack
i want to display the current date in this format Wednesday march 28,2012 ,
I have a column date in MYSQL, i want to display date in day
I want to display $row->depositdate in dd-mm-yyyy format. If the date column in database
I want to display date like this in my website Wednesday, March 03, 2010
Using VB.NET I want to display the date in datetimepicker from the database. Code.
I just want to display Start or End Error Message on my msg column
Using SQL Server 2000 Having Two Tables Table1 Date ID 20090101 001 20090102 001
I'm using a datagrid to display a column of date ranges and several columns

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.