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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:03:21+00:00 2026-05-29T04:03:21+00:00

Given a table like this: [Last Name] [First Name] [DepartmentID] ————————————— Doe John 1

  • 0

Given a table like this:

[Last Name] [First Name] [DepartmentID]
---------------------------------------
Doe         John         1
Doe         John         2
Black       Frank        3

, I’d like a result like this:

[Last Name] [First Name] [Accounting] [Management] [Development]
----------------------------------------------------------------
Doe         John         X            X
Black       Frank                                  X

Thus far, I have a query like this:

SELECT [Last Name], [First Name], [1], [2], [3]
FROM Employees
PIVOT(SUM(DepartmentID) FOR DepartmentID IN ([1], [2], [3])
GROUP BY [Last Name], [First Name], [1], [2], [3]

Which gives me:

[Last Name] [First Name] [1] [2] [3]
----------------------------------------------------------------
Doe         John         1
Doe         John             2
Black       Frank                3

Several problems:

  • the column name comes from elsewhere, i.e. it should be derived from a function or an subquery. Can this be done?
  • I assume I’ll have to use dynamic SQL to get the possible departments, i.e. I can’t tell PIVOT to simply determine all possibilities?
  • the column values should be just ‘X’ (or similar), not contain the real value. There shouldn’t be multiple lines per person per possible department, which means I can’t group by the pivoted columns. How do I get one line per person?

I don’t really want or need to aggregate anything; I just wish to show, per department, whether the employee resides in that department. Is PIVOT a red herring for this? I could possibly solve this in a more complicated manner with an EXISTS statement per department?

  • 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-05-29T04:03:22+00:00Added an answer on May 29, 2026 at 4:03 am

    Maybe this will help:

    First some test data:

    CREATE TABLE tbl(LastName VARCHAR(100),FirstName VARCHAR(100),DepartmentID INT)
    CREATE TABLE tblDepartment(DepartmentID INT,Name VARCHAR(100))
    
    INSERT INTO tbl
    SELECT 'Doe','John',1 UNION ALL
    SELECT 'Doe','John',2 UNION ALL
    SELECT 'Black','Frank',3
    
    INSERT INTO tblDepartment
    SELECT 1,'Accounting' UNION ALL
    SELECT 2,'Management' UNION ALL
    SELECT 3,'Development'
    

    The concating the columns:

    DECLARE @cols VARCHAR(MAX)
    SELECT  @cols = COALESCE(@cols + ','+QUOTENAME(Name),
                         QUOTENAME(Name))
    FROM 
        tblDepartment
    ORDER BY 
        Name
    

    Or you might want to concat the columns with just the existing ones like this:

    DECLARE @cols VARCHAR(MAX)
    SELECT  @cols = COALESCE(@cols + ','+QUOTENAME(Name),
                         QUOTENAME(Name))
    FROM 
        tblDepartment
    WHERE EXISTS
            (
                SELECT 
                    NULL 
                FROM 
                    tbl AS tbl2 
                WHERE 
                    tblDepartment.DepartmentID=tbl2.DepartmentID) 
    ORDER BY 
        Name
    

    Then execute the dynamic sql:

    DECLARE @query NVARCHAR(4000)=
    N'SELECT
        *
    FROM
    (
        SELECT
            tbl.LastName,
            tbl.FirstName,
            Department.Name,
            ''X'' as test
        FROM
            tbl AS tbl
            JOIN tblDepartment AS Department
                ON Department.DepartmentID=tbl.DepartmentID
    )AS p
    PIVOT
    (
        MAX(test) FOR Name IN ('+@cols+')
    ) As Pvt'
    
    EXECUTE(@query)
    

    And in my case dropping the created tables:

    DROP TABLE tbl
    DROP TABLE tblDepartment
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a table like this: CREATE TABLE #TEMP(RecordDate datetime, First VARCHAR(255), Last
Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
Given a table named person (in a MySQL database/schema), kind of like this one:
table a (t_a): id name last first email state country 0 sklass klass steve
This is the source example table: UserID UserNameType UserName 1 First Name FN1 1
Given a table or a temp table, I'd like to run a procedure that
Given a couple of simple tables like so: create table R(foo text); create table
Let's say I have SELECT name FROM table which gives me something like foo
Given the following xaml code, I would like to create this xaml code the
I have a table like this (basic example, not the real thing): FKEY |

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.