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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:10:16+00:00 2026-06-14T07:10:16+00:00

I have the following basic SQL statement which uses the PIVOT command. SELECT *

  • 0

I have the following basic SQL statement which uses the PIVOT command.

SELECT *
FROM

(
--select statement that creates my dataset 
) s

PIVOT (Max(incidentcount) for dept in ([dept1],[dept2])) p

This does what I expect it to do, it gives me a count of incidents per reason with depts as my columns. My problem is the departments that I am using for my columns go from 1-60.
Is there anyway I can tell the query to use the column Department to populate the PIVOT in part. Obviously I want to avoid manually typing each department.

EDIT

This is the sql that creates my dataset that I use in the pivot…

SELECT Details, Department , count(*) NoIncidents
  FROM myincidentdb
  Group by Details,  Department

EDIT 2

This is what my sql query shows before I attempt to PIVOT it

  • 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-14T07:10:18+00:00Added an answer on June 14, 2026 at 7:10 am

    You will want to use dynamic sql to PIVOT this, your code will be similar to this:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(dept) 
                        from yourtablewithDepartments
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT *
                  from 
                  (
                    <your query goes here>
                  ) src
                  pivot 
                  (
                    max(incidentcount)
                    for dept in (' + @cols + ')
                  ) piv '
    
    execute(@query)
    

    If you post more details like table structure, etc then this can be refined to meet your needs.

    Edit, based on your current query, it looks like you can use the following:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX),
        @colsNull AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(department) 
                        from myincidentdb
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT  Details, ' + @cols + ' 
                  from 
                  (
                    select Details, Department
                    from myincidentdb 
                  ) x
                  pivot 
                  (
                    count(Department)
                    for Department 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 the following select SQL that does a basic select statement although it
I have the following SQL statement SELECT [Motor] ,[Time] FROM [logger].[dbo].[motor] WHERE day([Time]) =
I have a basic table that looks like the following: SQL> desc comments Name
Say I have the following basic if-statement: if (A ~= 0) % do something
I might have pretty basic question about regex. I have the following regex, which
I have a basic web service that returns the following object as JSON: public
I have the following simple MySQL query: SELECT SQL_NO_CACHE mainID FROM tableName WHERE otherID3=19
I have a basic recursive table setup in SQL 2008 with the following structure:
I have a SQL statement which works when executed in MS Server Management Studio
I am retrieving the following sql statement from a table and then using bind

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.