Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining multiple columns in one table to a single column in another table

Tags:

sql

I am looking to create a view that pulls data from two tables "Schedule" and "Reference".

Schedule has 50+ columns (it's almost completely denormalized -- not my design), most of which contain a value that could be joined to a column in the Reference table.

How do I write the SQL statement to correctly join each column in Schedules to the single column in Reference?

The Schedule table is defined as:

    CREATE TABLE [dbo].[Schedule](
    [ID] [int] NOT NULL,
    [SCHEDULEWEEK] [datetime] NOT NULL,
    [EMPNO] [numeric](10, 0) NOT NULL,
    [EMPLNAME] [varchar](32) NULL,
    [EMPFNAME] [varchar](32) NULL,
    [EMPSENDATE] [datetime] NULL,
    [EMPHIREDATE] [datetime] NULL,
    [EMPTYPE] [char](1) NULL,
    [EMPSTATUS] [char](1) NULL,
    [SNREFUSALS] [tinyint] NULL,
    [QUALSTRING] [varchar](128) NULL,
    [JOBOVERSHIFTTYPE] [bit] NULL,
    [SHORTNOTICE] [bit] NULL,
    [SHORTNOTICEWAP] [bit] NULL,
    [SHORTNOTICEPHONE] [varchar](32) NULL,
    [LEADHAND] [bit] NULL,
    [DUALCURRENCY] [bit] NULL,
    [MIN100WINDOW] [bit] NULL,
    [STATHOLIDAY] [bit] NULL,
    [AREAOVERHOURS] [bit] NULL,
    [DOUBLEINTERZONES] [bit] NULL,
    [MAXDAYSPERWEEK] [tinyint] NULL,
    [MAXHOURSPERWEEK] [numeric](10, 2) NULL,
    [MAXHOURSPERSHIFT] [numeric](10, 2) NULL,
    [MAXDOUBLESPERWEEK] [tinyint] NULL,
    [ASSIGNEDDAYS] [tinyint] NULL,
    [ASSIGNEDHOURS] [numeric](10, 2) NULL,
    [ASSIGNEDDOUBLES] [tinyint] NULL,
    [ASSIGNEDLOAHOURS] [numeric](10, 2) NULL,
    [SHIFTNO1] [int] NULL,
    [TEXT1_1] [varchar](64) NULL,
    [TEXT2_1] [varchar](64) NULL,
    [DAYFLAG1] [bit] NULL,
    [COMMENT1] [text] NULL,
    [SHIFTNO2] [int] NULL,
    [TEXT1_2] [varchar](64) NULL,
    [TEXT2_2] [varchar](64) NULL,
    [DAYFLAG2] [bit] NULL,
    [COMMENT2] [text] NULL,
    [SHIFTNO3] [int] NULL,
    [TEXT1_3] [varchar](64) NULL,
    [TEXT2_3] [varchar](64) NULL,
    [DAYFLAG3] [bit] NULL,
    [COMMENT3] [text] NULL,
    [SHIFTNO4] [int] NULL,
    [TEXT1_4] [varchar](64) NULL,
    [TEXT2_4] [varchar](64) NULL,
    [DAYFLAG4] [bit] NULL,
    [COMMENT4] [text] NULL,
    [SHIFTNO5] [int] NULL,
    [TEXT1_5] [varchar](64) NULL,
    [TEXT2_5] [varchar](64) NULL,
    [DAYFLAG5] [bit] NULL,
    [COMMENT5] [text] NULL,
    [SHIFTNO6] [int] NULL,
    [TEXT1_6] [varchar](64) NULL,
    [TEXT2_6] [varchar](64) NULL,
    [DAYFLAG6] [bit] NULL,
    [COMMENT6] [text] NULL
-- Snip
) ON [PRIMARY]

And the Reference table is defined as:

CREATE TABLE [dbo].[Reference](
    [ID] [int] NOT NULL,
    [CODE] [varchar](21) NOT NULL,
    [LOCATIONCODE] [varchar](4) NOT NULL,
    [SCHAREACODE] [varchar](16) NOT NULL,
    [LOCATIONNAME] [varchar](32) NOT NULL,
    [FLTAREACODE] [varchar](16) NOT NULL
) ON [PRIMARY]

I am trying to join each [TEXT1_]/[TEXT2_] column in Schedule to the [SCHAREACODE] column in reference. All the reference table contains is a list of areas where the employee could work.

like image 275
Steve Syfuhs Avatar asked Apr 20 '09 20:04

Steve Syfuhs


People also ask

How do I join one column to another table?

SELECT * FROM table1 JOIN table2 ON table1. column_name = table2. column_name; The INNER JOIN in SQL joins two tables according to the matching of a certain criteria using a comparison operator.

Can you join a table on multiple columns?

If you'd like to get data stored in tables joined by a compound key that's a primary key in one table and a foreign key in another table, simply use a join condition on multiple columns. In one joined table (in our example, enrollment ), we have a primary key built from two columns ( student_id and course_code ).

What are the 4 different table joining types?

Four types of joins: left, right, inner, and outer.


2 Answers

I think he means to join on the Reference table multiple times:

SELECT *
  FROM Schedule AS S
 INNER JOIN Reference AS R1 
         ON R1.ID = S.FirstID 
 INNER JOIN Reference AS R2 
         ON R2.ID = S.SecondID 
 INNER JOIN Reference AS R3 
         ON R3.ID = S.ThirdID 
 INNER JOIN Reference AS R4 
         ON R4.ID = S.ForthID 
like image 123
TheSoftwareJedi Avatar answered Sep 21 '22 23:09

TheSoftwareJedi


Your description is a bit lacking, so I'm going to assume that

Schedule has 50+ columns (it's almost completely denormalized -- not my design), most of which contain a value that could be joined to a column in the Reference table.

means that 1 of the 50+ columns in Schedule is a ReferenceId. So, given a table design like:

Schedule ( MaybeReferenceId1, MaybeReferenceId2, MaybeReferenceId3, ... )
Reference ( ReferenceId )

Something like:

SELECT *
FROM Schedule
JOIN Reference ON
     Schedule.MaybeReferenceId1 = Reference.ReferenceId
     OR Schedule.MaybeReferenceId2 = Reference.ReferenceId
     OR Schedule.MaybeReferenceId3 = Reference.ReferenceId
     OR Schedule.MaybeReferenceId4 = Reference.ReferenceId
     ...

would work. You could simplify it by using IN if your RDBMS supports it:

SELECT *
FROM Schedule
JOIN Reference ON
     Reference.ReferenceId IN (
        Schedule.MaybeReferenceId1,
        Schedule.MaybeReferenceId2,
        Schedule.MaybeReferenceId3,
        Schedule.MaybeReferenceId4,
        ...
     )
like image 32
Mark Brackett Avatar answered Sep 21 '22 23:09

Mark Brackett