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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:16:58+00:00 2026-06-11T20:16:58+00:00

I want to display rows to columns in Sql Server. I have seen the

  • 0

I want to display rows to columns in Sql Server. I have seen the other questions but those columns are hardcoded in the pivot but my columns will be dynamic. What I have achieved till now. As shown in the screenshot I am able to convert the rows into columns but few things I am not able to accomplish.. Need your guyz help

  1. Replacing NULL To 0 in All the Columns
  2. Need to Add 1 more column which will show the sum of all Columns except the companyID

My SQL code:

DECLARE @Columns VARCHAR(MAX)
DECLARE @Convert VARCHAR(MAX)

SELECT  @Columns = STUFF((
           SELECT '],[' + ErrClassfn
             from ArchimedesTables.dbo.PM_ErrClassificationSetup
            WHERE CONVERT(VARCHAR(10), ISNULL(EndDate, GETDATE()), 101) 
                          Between CONVERT(VARCHAR(10), GETDATE(), 101)
                          AND     CONVERT(VARCHAR(10), GETDATE(), 101)
            ORDER BY '],[' + CONVERT(VARCHAR(MAX), ID) ASC
              FOR
                  XML PATH('')
           ), 1, 2, '') + ']'

SET @Convert = 'SELECT  * INTO #mynewTable FROM
(
    SELECT COUNT(WQ.ErrClassfnID) as ErrorCount, UPPER(WQ.CompanyID) as CompanyID, 
           PME.ErrClassfn as ErrorName
      FROM Version25.dbo.WF_Quality AS WQ
      LEFT JOIN ArchimedesTables.dbo.PM_ErrClassificationSetup as PME
           ON WQ.ErrClassfnID = PME.ID
     GROUP BY
          UPPER(CompanyID), ErrClassfn
) Quality PIVOT ( SUM(ErrorCount)  For ErrorName IN (' + @Columns
    + ')) as PivotTable  SeLeCt * FROM #mynewTable'

EXEC(@Convert)

enter image description here

  • 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-11T20:17:00+00:00Added an answer on June 11, 2026 at 8:17 pm

    You can alter the columns names, etc for a Dynamic Pivot, similar to this:

    DECLARE @ColumnsNull VARCHAR(MAX)
    DECLARE @Columns VARCHAR(MAX)
    DECLARE @Convert VARCHAR(MAX)
    
    SELECT  @ColumnsNull = STUFF((SELECT ', IsNull(' + QUOTENAME(ErrClassfn) +', 0) as ['+ rtrim(ErrClassfn)+']' 
                       from ArchimedesTables.dbo.PM_ErrClassificationSetup
                       WHERE CONVERT(VARCHAR(10), ISNULL(EndDate, GETDATE()), 101) 
                                      Between CONVERT(VARCHAR(10), GETDATE(), 101)
                                      AND     CONVERT(VARCHAR(10), GETDATE(), 101)
                       ORDER BY ID ASC
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    SELECT  @Columns = STUFF((
               SELECT '],[' + ErrClassfn
                 from ArchimedesTables.dbo.PM_ErrClassificationSetup
                WHERE CONVERT(VARCHAR(10), ISNULL(EndDate, GETDATE()), 101) 
                              Between CONVERT(VARCHAR(10), GETDATE(), 101)
                              AND     CONVERT(VARCHAR(10), GETDATE(), 101)
                ORDER BY '],[' + CONVERT(VARCHAR(MAX), ID) ASC
                  FOR
                      XML PATH('')
               ), 1, 2, '') + ']'
    
    
    SET @Convert = 'SELECT  CompanyID, '+ @ColumnsNull + '
                    INTO #mynewTable 
                    FROM
                    (
                        SELECT COUNT(WQ.ErrClassfnID) as ErrorCount, UPPER(WQ.CompanyID) as CompanyID, 
                               PME.ErrClassfn as ErrorName
                          FROM Version25.dbo.WF_Quality AS WQ
                          LEFT JOIN ArchimedesTables.dbo.PM_ErrClassificationSetup as PME
                               ON WQ.ErrClassfnID = PME.ID
                         GROUP BY
                              UPPER(CompanyID), ErrClassfn
                    ) Quality PIVOT ( SUM(ErrorCount)  For ErrorName IN (' + @Columns
                        + ')) as PivotTable  SeLeCt * FROM #mynewTable'
    
    EXEC(@Convert)
    

    I would advise to write the query and get the columns working first, then add the data to a #temp table. It will be easier to debug that way.

    You can also create a SUM() field the same way, where you build it dynamically and then add it at in the final SELECT:

    So it could be something like this that you could add to the final SELECT:

    SELECT  @ColumnsTotal = STUFF((SELECT '+' + QUOTENAME(ErrClassfn) 
                       from ArchimedesTables.dbo.PM_ErrClassificationSetup
                       WHERE CONVERT(VARCHAR(10), ISNULL(EndDate, GETDATE()), 101) 
                                      Between CONVERT(VARCHAR(10), GETDATE(), 101)
                                      AND     CONVERT(VARCHAR(10), GETDATE(), 101)
                       ORDER BY ID ASC
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to display JQGrid with all column headers but without any data rows
I want to display a ListView in table format. The ListView should have rows
I have a gridview which displays rows and columns all linked to an sql
So I have table with only two, long-ish columns. I want the display the
i want to display two columns in datagrid view . first by sql-table second
I want to be able to display child rows upon load. Right now I
I have a simple quiz application and I want display a nice timer /
I have db with this table (TableToDo): http://goo.gl/NlTEk I want display all records in
I'm designing a Java application using SWT. I have some tables which will display
im using gridview to display records. Rows and columns are generated dynamically by using

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.