Select rows into columns and show a flag in the column

Khrys

Trying to get an output like the below:

| UserFullName | JAVA   | DOTNET | C      | HTML5  |
|--------------|--------|--------|--------|--------|
|     Anne San |        |        |        |        |
|   John Khruf |      1 |      1 |        |      1 |
|    Mary Jane |      1 |        |        |      1 |
|  George Mich |        |        |        |        |

This shows the roles of a person. A person could have 0 or N roles. When a person has a role, I am showing a flag, like '1'.


Actually I have 2 blocks of code:

Block #1: The tables and a simple output which generates more than 1 rows per person.

SQL Fiddle

MS SQL Server 2008 Schema Setup:

CREATE TABLE AvailableRoles 
(
  id int identity primary key, 
  CodeID varchar(5), 
  Description varchar(500), 
);

INSERT INTO AvailableRoles
(CodeID, Description)
VALUES
('1', 'JAVA'),
('2', 'DOTNET'),
('3', 'C'),
('4', 'HTML5');

CREATE TABLE PersonalRoles 
(
  id int identity primary key, 
  UserID varchar(100), 
  RoleID varchar(5), 
);

INSERT INTO PersonalRoles
(UserID, RoleID)
VALUES
('John.Khruf', '1'),
('John.Khruf', '2'),
('Mary.Jane', '1'),
('Mary.Jane', '4'),
('John.Khruf', '4');


CREATE TABLE Users 
(
  UserID varchar(20), 
  EmployeeType varchar(1),
  EmployeeStatus varchar(1),
  UserFullName varchar(500), 
);

INSERT INTO Users
(UserID, EmployeeType, EmployeeStatus, UserFullName)
VALUES
('John.Khruf', 'E', 'A', 'John Khruf'),
('Mary.Jane', 'E', 'A', 'Mary Jane'),
('Anne.San', 'E', 'A', 'Anne San'),
('George.Mich', 'T', 'A', 'George Mich');

Query 1:

SELECT
  A.UserFullName,
  B.RoleID
FROM
  Users A
LEFT JOIN PersonalRoles B ON B.UserID = A.UserID
WHERE
  A.EmployeeStatus = 'A'
ORDER BY
  A.EmployeeType ASC,
  A.UserFullName ASC

Results:

| UserFullName | RoleID |
|--------------|--------|
|     Anne San | (null) |
|   John Khruf |      1 |
|   John Khruf |      2 |
|   John Khruf |      4 |
|    Mary Jane |      1 |
|    Mary Jane |      4 |
|  George Mich | (null) |

Block #2: An attempt to convert the rows into columns to be used in the final result

SQL Fiddle

MS SQL Server 2008 Schema Setup:

CREATE TABLE AvailableRoles 
(
  id int identity primary key, 
  CodeID varchar(5), 
  Description varchar(500), 
);

INSERT INTO AvailableRoles
(CodeID, Description)
VALUES
('1', 'JAVA'),
('2', 'DOTNET'),
('3', 'C'),
('4', 'HTML5');

Query 1:

SELECT
  *
FROM
(
  SELECT CodeID, Description
  FROM AvailableRoles
) d
PIVOT
(
  MAX(CodeID)
  FOR Description IN (Java, DOTNET, C, HTML5)
) piv

Results:

| Java   | DOTNET | C     | HTML5  |
|--------|--------|-------|--------|
|      1 |      2 |     3 |      4 |

Any help in mixing both blocks to show the top output will be welcome. Thanks.

Jorge Campos

Another option without PIVOT operator is:

select u.UserFullName,
       max(case when a.CodeID='1' then '1' else '' end) JAVA,
       max(case when a.CodeID='2' then '1' else '' end) DOTNET,
       max(case when a.CodeID='3' then '1' else '' end) C,
       max(case when a.CodeID='4' then '1' else '' end) HTML5
  from
   Users u 
   LEFT JOIN PersonalRoles p on (u.UserID = p.UserID)
   LEFT JOIN AvailableRoles a on (p.RoleID = a.CodeID)
group by u.UserFullName
order by u.UserFullName

SQLFiddle: http://sqlfiddle.com/#!3/630c3/19

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select rows with show flag but based on another table

From Dev

Select columns from another select as rows for each column

From Dev

SQL SELECT rows with MAX value on a column and returns all columns

From Dev

Excel Pivot Table select rows based on column, with all columns displayed

From Dev

SELECT no rows and no columns?

From Dev

Select Statement with rows as columns

From Dev

Select rows with GROUP BY excluding GROUP with flag ( MySQL )

From Dev

How to select rows with int flag in doctrine

From Dev

show all data only group by specific rows : Select * from table group by column having column = 'value'

From Dev

Select rows with same column

From Dev

Show data in rows and columns in IOS

From Dev

Show data in rows and columns in IOS

From Dev

SQL: Select rows in a table by filtering multiple columns from the same table by a 3 column select result

From Dev

SELECT statement to convert columns into rows

From Dev

Select SQL Columns as added rows

From Dev

Select a range of rows as various columns

From Dev

SELECT statement to show extra columns

From Dev

How to show table result column and show it on rows

From Dev

rows to columns based on first column

From Dev

Show column by max() other columns

From Dev

Show column by max() other columns

From Dev

Show column information for several columns

From Dev

SQLSERVER Select rows as columns even if there is no enough rows

From Dev

select columns into rows grouped by columns values

From Dev

Select rows from a DataFrame based on presence of null value in specific column or columns

From Dev

How to select rows from MySQL based on max value of a one column and grouping two other columns?

From Dev

SELECT sql to return sum of values in columns of 3 rows as second column value and so on

From Dev

Select non-null rows from a specific column in a DataFrame and take a sub-selection of other columns

From Dev

Select rows where column value appears multiple times while grouped on multiple columns

Related Related

  1. 1

    Select rows with show flag but based on another table

  2. 2

    Select columns from another select as rows for each column

  3. 3

    SQL SELECT rows with MAX value on a column and returns all columns

  4. 4

    Excel Pivot Table select rows based on column, with all columns displayed

  5. 5

    SELECT no rows and no columns?

  6. 6

    Select Statement with rows as columns

  7. 7

    Select rows with GROUP BY excluding GROUP with flag ( MySQL )

  8. 8

    How to select rows with int flag in doctrine

  9. 9

    show all data only group by specific rows : Select * from table group by column having column = 'value'

  10. 10

    Select rows with same column

  11. 11

    Show data in rows and columns in IOS

  12. 12

    Show data in rows and columns in IOS

  13. 13

    SQL: Select rows in a table by filtering multiple columns from the same table by a 3 column select result

  14. 14

    SELECT statement to convert columns into rows

  15. 15

    Select SQL Columns as added rows

  16. 16

    Select a range of rows as various columns

  17. 17

    SELECT statement to show extra columns

  18. 18

    How to show table result column and show it on rows

  19. 19

    rows to columns based on first column

  20. 20

    Show column by max() other columns

  21. 21

    Show column by max() other columns

  22. 22

    Show column information for several columns

  23. 23

    SQLSERVER Select rows as columns even if there is no enough rows

  24. 24

    select columns into rows grouped by columns values

  25. 25

    Select rows from a DataFrame based on presence of null value in specific column or columns

  26. 26

    How to select rows from MySQL based on max value of a one column and grouping two other columns?

  27. 27

    SELECT sql to return sum of values in columns of 3 rows as second column value and so on

  28. 28

    Select non-null rows from a specific column in a DataFrame and take a sub-selection of other columns

  29. 29

    Select rows where column value appears multiple times while grouped on multiple columns

HotTag

Archive