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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:26:11+00:00 2026-06-11T21:26:11+00:00

I am building pivot query to display data in report form as below. CREATE

  • 0

I am building pivot query to display data in report form as below.

CREATE TABLE #Table1 (ColId INT)
INSERT INTO #Table1 VALUES(29)
INSERT INTO #Table1 VALUES(49)
INSERT INTO #Table1 VALUES(59)


DECLARE @cols NVARCHAR(2000)
SELECT  @cols = COALESCE(@cols + ',[' + Convert(varchar(10), ColId)  + ']',
                         '[' + Convert(varchar(10), ColId) + ']')
FROM    #Table1
print (@cols)

Declare @final NVARCHAR(2000)
set @final=
'SELECT *
FROM (
SELECT tv
        , rv
        , dpv
FROM (

      ---Inner join query with multiple tables ----    

) As C
) P
PIVOT (
Max(dpv)
FOR tv IN (' + @cols + ')
) AS PVT'

exec(@final)


drop table #Table1

and result of above query as below.

enter image description here

I would like result as below

enter image description here

In this i need to append ‘C’ in tv so there would be 2 column 29C & 29 and under 29C i need to display 29C + dpv and under 29 i need to display summation of tv + dpv (i.e. 29+ 5=34) like that for all column.

How i can achieve it? How can we add multiple aggregate value?
thanks in advance.

  • 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-11T21:26:12+00:00Added an answer on June 11, 2026 at 9:26 pm

    Add this …

    DECLARE @c2 varchar(2000)
    select @c2 = ''
    SELECT  
            @c2 = @c2+ ', '''+ col + 'C+'' + CONVERT(varchar(5), ['+col+']) as ['+col+'C], ['+col+']+'+col+' as ['+col+'] '                             
    FROM    (select convert(varchar(10),colid) as col from #Table1) v
    
    
    declare @sql nvarchar(2000)
        select @sql = 'select rv ' + @c2 + ' from (' +@final + ') v'
    
    exec (@sql)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

does anyone know if there's any good online tutorial for building pivot tables using
I'm building a silverlight pivot viewer collection which contains html descriptions; however the html
I'm writing a report that needs to display a week's worth of receipts, grouped
Building my code (below) returns error 'imread' is not a member of 'cv' .
Building an app with the Facebook JavaScript API that will embedded into a page
Building on from a previous query regarding Class::Struct vs Object::Accessor, I'd like to find
Building a quick view that will display a list of all days but only
Every morning at 2:00am (or when ever all the pivots finish building) I run
Building an inventory system. I have lots of products and each product has three
Building a new Mobile Web Platform for Mobile Users to purchase & download content

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.