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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:21:26+00:00 2026-06-15T22:21:26+00:00

Possible Duplicate: Pivot data in T-SQL please make needed changes in the query to

  • 0

Possible Duplicate:
Pivot data in T-SQL

please make needed changes in the query to create a pivot form display …

select COUNT(employeeid) as count_of_employees,Title 
from HumanResources.Employee 
group by Title order by no_of_employees desc

this query is returning the following result

count_of_employees  Title
26                  Production Technician - WC50
26                  Production Technician - WC60
26                  Production Technician - WC40
25                  Production Technician - WC30
22                  Production Technician - WC20
17                  Production Technician - WC10
15                  Production Technician - WC45
14                  Sales Representative
9                   Buyer
5                   Marketing Specialist
4                   Scheduling Assistant

i need the desired result

Production Technician-WC50 |  Production Technician-WC60  |   Production Technician-WC40
           26              |            26                |                26
  • 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-15T22:21:27+00:00Added an answer on June 15, 2026 at 10:21 pm

    To pivot the rows into columns the way you described, you have to use the PIVOT table operator. Something like so:

    SELECT *
    FROM
    (
      SELECT [count_of_employees], [Title]
      FROM YourQuery
      ) t
    PIVOT
    (
      MAX(count_of_employees)
      FOR Title IN([Production Technician - WC50], 
                   [Production Technician - WC60], 
                   [Production Technician - WC40],
                   [Production Technician - WC30], 
                   [Production Technician - WC20], 
                   [Production Technician - WC10], 
                   [Production Technician - WC45], 
                   [Sales Representative], 
                   [Buyer], 
                   [Marketing Specialist], 
                   [Scheduling Assistant])
     ) p;
    

    SQL Fiddle Demo


    But this is ugly, since you have to write these list of title. However, it is better to do this dynamically, by getting the list of titles dynamically like so:

    DECLARE @cols AS NVARCHAR(MAX);
    DECLARE @query AS NVARCHAR(MAX);
    
    SELECT @cols = STUFF((SELECT distinct ',' +  QUOTENAME (title)
                   FROM YourQuery
                   FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)')  ,1,1,'');
    
    SET @query = 'SELECT ' + @cols + ' FROM
    (
       SELECT [count_of_employees], [Title] FROM YourQuery
    ) t
    PIVOT
    (
       MAX(count_of_employees)
       FOR title IN (' + @cols + ')
                 ) p ';
    
    EXECUTE(@query);
    

    SQL Fiddle Demo

    This should give you:

    | BUYER | MARKETING SPECIALIST | PRODUCTION TECHNICIAN - WC10 | PRODUCTION TECHNICIAN - WC20 | PRODUCTION TECHNICIAN - WC30 | PRODUCTION TECHNICIAN - WC40 | PRODUCTION TECHNICIAN - WC45 | PRODUCTION TECHNICIAN - WC50 | PRODUCTION TECHNICIAN - WC60 | SALES REPRESENTATIVE | SCHEDULING ASSISTANT |
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    |     9 |                    5 |                           17 |                           22 |                           25 |                           26 |                           15 |                           26 |                           26 |                   14 |                    4 |
    

    This is nicer, but you might need to be careful with dynamic SQL, reed this for more details:

    • The Curse and Blessings of Dynamic SQL

    Note that: the table YourQuery I used in my answer here, has to be replaced with your query:

    select COUNT(employeeid) as count_of_employees,Title 
    from HumanResources.Employee 
    group by Title order by no_of_employees desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: SQL Server dynamic PIVOT query? Is it possible to execute a query
Possible Duplicate: SQL Server dynamic PIVOT query? I have temporary table with following structure:
Possible Duplicate: SQL Server dynamic PIVOT query? I have a dataset that has the
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: mysql pivot/crosstab query I'm using MySQL and PHP, I have this table
Possible Duplicate: dynamic sql pivot in sql server How to alter this stored procedure
Possible Duplicate: Is there a way to create a SQL Server function to "join"
Possible Duplicate: Is there a way to create a SQL Server function to "join"
Possible Duplicate: dynamic sql pivot in sql server I have a table called Col_values
Possible Duplicate: T-SQL Pivot? Possibility of creating table columns from row values I've been

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.