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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:23:23+00:00 2026-06-14T22:23:23+00:00

I have the following query in SQL Server 2005 that fetches the result that

  • 0

I have the following query in SQL Server 2005 that fetches the result that is mentioned below it.

select Distinct(date),id,
           sum(convert(float,Gross)),
           count(*) as no from Daily
    group by date,id 
    order by date desc

Date                     id          Gross          Count
2012-11-25 00:00:00.000  Client id1  1232.6140752   12
2012-11-25 00:00:00.000  Client id2  1183.75621528  88
2012-11-26 00:00:00.000  Client id3  4561.459086    67
2012-11-26 00:00:00.000  Client id4  6781.15660608  440

Now how do I get the result in the following format. This looks like a pivot query is reqd please help

id           Date1  Date2  Date3  Date4  Date5 Date6 Date7
Client id1   Gross       
Client id2   Gross
Client id3   Gross
  • 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-14T22:23:24+00:00Added an answer on June 14, 2026 at 10:23 pm

    There are two ways that you can PIVOT, either a static version where you hard-code all of the date values or a dynamic version which will generate the list of dates at run-time.

    Static Version:

    If you had a limited number of dates your query will be similar to this.

    select id, [yourDate1], [yourDate2], [yourDate3]
    from
    (
      select date, id, cast(gross as float) as gross
      from Daily
    ) src
    pivot
    (
      sum(gross)
      for date in ([yourDate1], [yourDate2], [yourDate3])
    ) piv;
    

    See SQL Fiddle with Demo

    Dynamic Version:

    This version generates dynamic SQL to get the list of dates at run-time. Your code will be similar to this:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(date) 
                        from Daily
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT id, ' + @cols + ' from 
                 (
                   select date, id, cast(gross as float) as gross
                   from Daily
                ) src
                pivot 
                (
                    sum(gross)
                    for date in (' + @cols + ')
                ) p '
    
    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

I have following query in sql server 2005 with a table select t1.id, CONVERT(VARCHAR,t1.dt,103)
HI, Using SQL server 2005 I have the following query: SELECT contact_id ,YEAR(date_created) AS
In sql server 2008, I have the following query: select c.title as categorytitle, s.title
I have the following SQL Server query: SELECT area FROM places WHERE REPLACE(area,'-',' ')
I am using MSSQL 2005 Server and I have the following SQL query. IF
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have the following SQL query: SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID, ra.BEZEICHNUNG
I have the following SQL query SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID, ra.BEZEICHNUNG
I currently have the following SQL query that lists all the names of all
Let's say I have the following SQL query SELECT id, name, title, description, time

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.