Join table rows as column values sql

smiller48

In this situation I have two tables that I have joined, and it creates multiple duplicates due to each physician being able to have multiple licenses. I would like to pivot and make the multiple license state and numbers additional columns to table 1 as shown below.

This will need to be dynamically done since it can be an substantial amount of licenses assigned to one physician.

Table 1 looks like this.

assignid  physician_name  profession 
-------------------------------
    1        bob          md        
    2        travis       do        
    3        ryan         md        
    4        pete         pa        
    5        susan        np         
    6        ashley       cnp     

Table 2

assignid  license_state license_num
-------------------------------
     1       oh        561
     2       mi        987
     3       ca        785
     4       ny        965
     4       mi        125
     5       oh        369
     5       ca        541

Joined

assignid  physician_name  profession license_state license_num
----------------------------------------------------------------
    1        bob          md             oh           561
    2        travis       do             mi           987
    3        ryan         md             ca           785
    4        pete         pa             ny           965
    4        pete         pa             mi           125
    5        susan        np             oh           369
    5        susan        np             ca           541

I want to dynamically change the joined table to look like this.

assignid  physician_name  profession license_state1 license_num1 license_state2 license_num2
--------------------------------------------------------------------------------------------------
   1        bob          md             oh           561
   2        travis       do             mi           987
   3        ryan         md             ca           785
   4        pete         pa             ny           965             mi       125
   5        susan        np             oh           369             ca       541

I attempted this route, but this gives me states as column header.

WITH pivotdata AS (
SELECT assignid,physician_name, profession, license_state,license_num
FROM dbo.Physicians p JOIN dbo.Licenses l ON p.AssignID = l.AssignID
)

SELECT *
FROM
   pivotdata
   PIVOT (MAX(license_num) FOR license_state IN ([oh], [mi], [ca],[ny])) TT;

Results

physician_name  profession   oh  mi  ca  ny
-------------------------------------------
    bob          md          561
    travis       do             987
    ryan         md                  785
    pete         pa             125     965
    susan        np          369     541
Sean Lange

You can solve this using a dynamic cross tab. I learned this technique from Jeff Moden and his article here. http://www.sqlservercentral.com/articles/Crosstab/65048/

if OBJECT_ID('tempdb..#Physicians') is not null
    drop table #Physicians

create table #Physicians
(
    AssignID int
    , PhysicianName varchar(20)
    , Profession varchar(10)
)

insert #Physicians values
(1, 'bob', 'md')
, (2, 'travis', 'do')
, (3, 'ryan', 'md')
, (4, 'pete', 'pa')
, (5, 'susan', 'np')
, (6, 'ashley', 'cnp')

if OBJECT_ID('tempdb..#Licenses') is not null
    drop table #Licenses

create table #Licenses
(
    AssignID int
    , LicenseState char(2)
    , LicenseNum int
)

insert #Licenses values
(1, 'oh', 561)
, (2, 'mi', 987)
, (3, 'ca', 785)
, (4, 'ny', 965)
, (4, 'mi', 125)
, (5, 'oh', 369)
, (5, 'ca', 541)

declare @StaticPortion nvarchar(2000) = 
    'with OrderedResults as
    (
        SELECT p.AssignID
            , p.PhysicianName
            , p.Profession
            , l.LicenseState
            , l.LicenseNum
            , ROW_NUMBER() over(partition by p.AssignID order by l.LicenseState) as RowNum
        FROM #Physicians p 
        JOIN #Licenses l ON p.AssignID = l.AssignID
    )
    select AssignID';

declare @DynamicPortion nvarchar(max) = '';
declare @FinalStaticPortion nvarchar(2000) = ' from OrderedResults Group by AssignID order by AssignID';

--the following cte is a tally table (another trick I learned from Jeff Moden)
with E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
cteTally(N) AS 
(
    SELECT  ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E2
)

select @DynamicPortion = @DynamicPortion + 
    ', MAX(Case when RowNum = ' + CAST(N as varchar(6)) + ' then LicenseState end) as LicenseState' + CAST(N as varchar(6)) + CHAR(10)
    + ', MAX(Case when RowNum = ' + CAST(N as varchar(6)) + ' then LicenseNum end) as LicenseNum' + CAST(N as varchar(6)) + CHAR(10)
from cteTally t
where t.N <= 
(
    select top 1 Count(p.AssignID)
    FROM #Physicians p 
    JOIN #Licenses l ON p.AssignID = l.AssignID
    group by p.AssignID
    order by COUNT(*) desc
)


declare @SqlToExecute nvarchar(max) = @StaticPortion + @DynamicPortion + @FinalStaticPortion;

--you can comment the following. it is here for now so you can view the dynamic sql before it executes
select @SqlToExecute

--Once you are satisfied that the dynamic sql generated is correct simply uncomment the below line
--exec sp_executesql @SqlToExecute

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Select rows with column values that match column values in different table

From Dev

SQL Select rows with column values that match column values in different table

From Dev

Random rows from sql table with a percent from column values

From Dev

SQL Server : how to select the rows in a table with the same value on a column but some exact values on another column for the grouped rows

From Dev

SQL Oracle - sum values in columns in first table using column names that are stored as values (rows) in second table

From Dev

SQL Oracle - sum values in columns in first table using column names that are stored as values (rows) in second table

From Dev

SQL Join table and SUM values

From Dev

sql-left-outer-join-with rows-based on column value from right table

From Dev

SQL Join a row table with a column table

From Dev

SQL join allowing multiple rows for given column

From Dev

SQL: default values for rows not corresponding to a JOIN criterion?

From Dev

SQL query to join rows with same values

From Dev

Join Rows from one huge SQL Table

From Dev

SQL Left Join - Multiple Rows in Right Table

From Dev

Display and join table using column with null values

From Dev

Left join rows and right table rows in same column

From Dev

Get rows with matching values in column in same table

From Dev

Filter a table with column values represented as rows

From Dev

SQL: Select all values from one table in a join while not knowing all column names?

From Dev

SQL Server inserting huge number of rows to a table with default values and identity column in it

From Dev

SQL Server compare values of two rows of same table and get not matching column names

From Dev

Copy rows from and within same oracle SQL table and changing some column values

From Dev

SQL JOIN match NULL column values

From Dev

SQL join column within 1 table

From Dev

SQL Join Constraint Column on Another Joined Table

From Dev

Join the result of a Temp column to a table SQL Server

From Dev

SQL comparison condition column to choose the table to JOIN

From Dev

SQL Adding a Column to table, based on a inner join

From Dev

SQL counting unique true values on join table

Related Related

  1. 1

    SQL Select rows with column values that match column values in different table

  2. 2

    SQL Select rows with column values that match column values in different table

  3. 3

    Random rows from sql table with a percent from column values

  4. 4

    SQL Server : how to select the rows in a table with the same value on a column but some exact values on another column for the grouped rows

  5. 5

    SQL Oracle - sum values in columns in first table using column names that are stored as values (rows) in second table

  6. 6

    SQL Oracle - sum values in columns in first table using column names that are stored as values (rows) in second table

  7. 7

    SQL Join table and SUM values

  8. 8

    sql-left-outer-join-with rows-based on column value from right table

  9. 9

    SQL Join a row table with a column table

  10. 10

    SQL join allowing multiple rows for given column

  11. 11

    SQL: default values for rows not corresponding to a JOIN criterion?

  12. 12

    SQL query to join rows with same values

  13. 13

    Join Rows from one huge SQL Table

  14. 14

    SQL Left Join - Multiple Rows in Right Table

  15. 15

    Display and join table using column with null values

  16. 16

    Left join rows and right table rows in same column

  17. 17

    Get rows with matching values in column in same table

  18. 18

    Filter a table with column values represented as rows

  19. 19

    SQL: Select all values from one table in a join while not knowing all column names?

  20. 20

    SQL Server inserting huge number of rows to a table with default values and identity column in it

  21. 21

    SQL Server compare values of two rows of same table and get not matching column names

  22. 22

    Copy rows from and within same oracle SQL table and changing some column values

  23. 23

    SQL JOIN match NULL column values

  24. 24

    SQL join column within 1 table

  25. 25

    SQL Join Constraint Column on Another Joined Table

  26. 26

    Join the result of a Temp column to a table SQL Server

  27. 27

    SQL comparison condition column to choose the table to JOIN

  28. 28

    SQL Adding a Column to table, based on a inner join

  29. 29

    SQL counting unique true values on join table

HotTag

Archive