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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:55:50+00:00 2026-06-15T20:55:50+00:00

I have 2 tables in SQL Server: Table 1 : Department DeptId Dept Name

  • 0

I have 2 tables in SQL Server:

Table 1 : Department

DeptId   Dept Name
------------------
1        Software Development
2        Testing
3        Customization

Table 2 : Designation

DesigId  Desig Name  DeptId
---------------------------
 1        TL          1
 2        PL          1
 3        TestEngg    2
 4        SE          3

I want the following output which takes department as column heading and group designation under the corresponding department column,

  Software Development     Testing     Customization
        TL                 TestEngg        SE
        PL             

I tried with the below query but Im able to get only the Id’s

 DECLARE @deptcols AS VARCHAR(MAX);
 DECLARE @querystr  AS VARCHAR(MAX);


select @deptcols = STUFF((SELECT distinct ',' + QUOTENAME(Dept_Id) 
              FROM Designation 
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

 set @querystr = 'SELECT ' + @deptcols + ' from 
         (
             select Desig_Name, Dept_Id,Desig_Id
             from Designation
        ) p
        pivot 
        (
           count(Desig_Id) FOR Dept_Id in (' + @deptcols + ')
        ) pv '
 execute(@querystr)
  • 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-15T20:55:51+00:00Added an answer on June 15, 2026 at 8:55 pm

    Your code was so very close. My suggestion when working with PIVOT especially a dynamic version is to write the static version first, then convert it to dynamic sql.

    The static version, where you hard-code values is like this:

    SELECT [Software Development], [Testing], [Customization]
    from 
    (
       select d.[Dept Name], 
          s.[Desig Name],
          row_number() over(partition by s.deptid order by s.desigid) rn
       from Designation s
       left join department d
          on s.[DeptId] = d.[DeptId]
    ) p
    pivot 
    (
      max([Desig Name]) 
      FOR [Dept Name] in ([Software Development], [Testing], [Customization])
    ) pv
    

    See SQL Fiddle with Demo. The static version allows you to be sure that the syntax is correct and all values, columns etc are in the right places.

    Then once you have the syntax it is easy to convert to the dynamic SQL version:

    DECLARE @deptcols AS VARCHAR(MAX)
    DECLARE @querystr  AS VARCHAR(MAX)
    
    select @deptcols = STUFF((SELECT  ',' + QUOTENAME([Dept Name]) 
                  FROM Department 
                  GROUP BY [Dept Name], DeptId
                  ORDER BY DeptId
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')
    
    set @querystr = 
          'SELECT ' + @deptcols + ' from 
             (
                select d.[Dept Name], 
                    s.[Desig Name],
                  row_number() over(partition by s.deptid order by s.desigid) rn
                from Designation s
                left join department d
                   on s.[DeptId] = d.[DeptId]
            ) p
            pivot 
            (
               max([Desig Name]) 
               FOR [Dept Name] in (' + @deptcols + ')
            ) pv '
    
    execute(@querystr)
    

    See SQL Fiddle with Demo

    Both give the result:

    | SOFTWARE DEVELOPMENT |  TESTING | CUSTOMIZATION |
    ---------------------------------------------------
    |                   TL | TestEngg |            SE |
    |                   PL |   (null) |        (null) |
    

    You will notice that I added the line row_number() over(partition by s.deptid order by s.desigid) rn to the SELECT statement. This allows you to return more than one Desig Name for each Dept Name, without this you will only return one value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have four tables like below in sql server 2008 : TABLE 1 ->
I have 2 Identical tables in SQL Server 2008, one is the Live table
In SQL Server 2008 I have two tables. One table for users: id_user -
I have a table in SQL Server 2008 Table Name : tbl_device Table Structure:
I have a SQL Server 2008 database with two tables. The first table is
I have a SQL Server table with department names in it (I.e. Admissions &
I have two tables in sql server 2008 R2. one table has records as
I have two tables in SQL Server, tbl_disputes and tbl_disputetypes . The tbl_disputes table
In an SQL Server 2008 database, I have tables such as: CREATE TABLE t_DeviceType
i have 3 tables in sql-server 2008 table A , table B , Table

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.