将具有同一列的2个表联接起来,并将同一列显示为一个列

时间:2019-05-10 07:40:42

标签: sql-server tsql

我有3张桌子:

  1. DATA_IZIN_BODY
  2. DATA_IZIN_DETAIL
  3. DATA_IZIN_DETAILPC

以下是示例数据:

DATA_IZIN_BODY //更新

|ID_B|NIK  |PERMIT TYPE|REASON       |NAME  |SUBMISSION DATE  |STATUS  |
|----|-----|-----------|-------------|------|-----------------|--------|
|N1  |1070 |ABSENT     |SICK         |John  |9/5/2019         |PENDING |
|N2  |1088 |LEAVE      |LATE         |Laura |8/6/2019         |APPROVED|
|N3  |1009 |ABSENT     |CANNT ATTND  |Emmet |8/8/2019         |APPROVED|

DATA_IZIN_DETAIL * //更新***

|ID  |ID_B |NIK  |DETAIL DATE  |DETAIL HOUR|STATUS  |FLAG|
|----|-----|-----|-------------|-----------|--------|----|
|001 |N1   |1070 |10/5/2019    |08.00      |NULL    |1   |
|002 |N1   |1070 |11/5/2019    |07.00      |NULL    |1   |
|003 |N1   |1070 |12/6/2019    |08.00      |NULL    |1   |
|004 |N3   |1009 |9/8/2019     |09.00      |NULL    |1   |
|005 |N3   |1088 |10/6/2019    |10.00      |NULL    |1   |
|006 |N3   |1009 |11/8/2019    |11.00      |NULL    |1   |

DATA_IZIN_DETAILPC * //更新***

|ID  |ID_B|NIK  |DETAIL DATE  |STATUS  |FLAG  |
|----|----|-----|-------------|--------|------|
|001 |N1  |1070 |13/5/2019    |NULL    |2     |
|002 |N1  |1070 |14/6/2019    |NULL    |2     |
|003 |N3  |1009 |12/8/2019    |NULL    |2     |

目标

|ID  |ID_B|NIK  |NAME  |PERMIT TYPE|REASON     |DETAIL DATE  |STATUS  |FLAG|
|----|----|-----|------|-----------|-----------|-------------|--------|----|
|001 |N1  |1070 |John  |ABSENT     |SICK       |13/5/2019    |NULL    |2   |
|002 |N1  |1070 |John  |ABSENT     |SICK       |14/6/2019    |NULL    |2   |
|003 |N3  |1009 |Emmet |ABSENT     |CANNT ATTND|12/8/2019    |NULL    |2   |
|001 |N1  |1070 |John  |ABSENT     |SICK       |10/5/2019    |NULL    |1   |
|002 |N1  |1070 |John  |ABSENT     |SICK       |11/5/2019    |NULL    |1   |
|003 |N1  |1070 |John  |ABSENT     |SICK       |12/6/2019    |NULL    |1   |
|004 |N3  |1009 |Emmet |ABSENT     |CANNT ATTND|9/8/2019     |NULL    |1   |
|005 |N3  |1088 |Emmet |ABSENT     |CANNT ATTND|10/6/2019    |NULL    |1   |
|006 |N3  |1009 |Emmet |ABSENT     |CANNT ATTND|11/8/2019    |NULL    |1   |

这是我的查询,尝试使用大小写,但无法按预期工作:

select 
    b.izin_id, b.IZIN_NIK
    /*case 
        when pc.Flag = 2 then 'PC'
        when d.Flag = 1 then 'DT'end
        as Flag*/
from DATA_IZIN_BODY b
inner join DATA_IZIN_DETAIL d on d.IZIN_ID = b.IZIN_ID
inner join DATA_IZIN_DETAILPC pc on pc.IZIN_ID = b.IZIN_ID

但是我们如何结合:

  • DATA_IZIN_DETAIL.izin_id和DATA_IZIN_DETAILPC.izin_id分为1列作为ID?
  • DATA_IZIN_DETAIL.Flag和DATA_IZIN_DETAILPC.Flag插入1列作为标志吗?

更新

ON GOAL : COLUMN ID & FLAG CONTAIN VALUE OF DATA_IZIN_DETAILPC.ID & DATA_IZIN_DETAIL.ID, DATA_IZIN_DETAILPC.FLAG & DATA_IZIN_DETAIL.FLAG

更新2

Data_izin_body 

 1. ID_B : nvarchar 
 2. NIK : nvarchar 
 3. Permit_Type : nvarchar
 4. Reason : nvarchar 
 5. Name : nvarchar 
 6. Submission_Date : smalldatetime
 7. Status : nvarchar

Data_izin_detail

 1. ID : nvarchar
 2. ID_B : nvarchar 
 2. NIK : nvarchar 
 3. Detail_Date : smalldatetime
 4. Detail_Hour : nvarchar
 5. Status : nvarchar
 6. Flag : int

Data_izin_detailpc

 1. ID : nvarchar
 2. ID_B : nvarchar 
 2. NIK : nvarchar 
 3. Detail_Date : smalldatetime
 4. Status : nvarchar
 5. Flag : int

2 个答案:

答案 0 :(得分:0)

DATA_IZIN_DETAILDATA_IZIN_DETAILPC似乎具有相同的字段结构。如果需要,可以对两个SELECT查询执行UNION:

SELECT [ID], [ID_B], [NIK], [NAME], [DETAIL DATE], [STATUS], [FLAG] FROM [DATA_IZIN_DETAILPC]
UNION ALL
SELECT [ID], [ID_B], [NIK], [NAME], [DETAIL DATE], [STATUS], [FLAG] FROM [DATA_IZIN_DETAIL]

编辑:

根据您更新的问题中的最新信息,我建议使用以下查询来获得所需的结果:

SELECT
    D.[ID],
    D.[ID_B],
    D.[NIK],
    B.[Name],
    B.[Permit_Type],
    B.[Reason],
    D.[Detail_Date],
    D.[Status],
    D.[Flag]
FROM
    [DATA_IZIN_DETAILPC] AS D
    INNER JOIN [DATA_IZIN_BODY] AS B ON B.[ID_B] = D.[ID_B] --AND B.[NIK] = D.[NIK]
UNION ALL
SELECT
    D.[ID],
    D.[ID_B],
    D.[NIK],
    B.[Name],
    B.[Permit_Type],
    B.[Reason],
    D.[Detail_Date],
    D.[Status],
    D.[Flag]
FROM
    [DATA_IZIN_DETAIL] AS D
    INNER JOIN [DATA_IZIN_BODY] AS B ON B.[ID_B] = D.[ID_B] --AND B.[NIK] = D.[NIK]

答案 1 :(得分:0)

因此,UNION ALL合并了两个辅助表。然后,主表将联接到该表。

 SELECT Details.ID,
     b.ID_B,
     b.IZIN_NIK AS NIK,
     b.Name,
     b.[Permit Type],
     b.Reason,
     Details.[Detail Date],
     Details.Status,
     Details.Source,
     Details.Flag
   FROM DATA_IZIN_BODY b
   JOIN 
          (SELECT ID, ID_B, [DETAIL DATE], STATUS, 'DT' AS Source, FLAG
           FROM DATA_IZIN_DETAIL

           UNION ALL

           SELECT ID, ID_B, [DETAIL DATE], STATUS, 'PC' AS Source, FLAG
           FROM DATA_IZIN_DETAILPC) AS Details
   ON b.ID_B = Details.ID_B 
   ORDER BY b.NAME, 
         Details.[DETAIL DATE]

如果您有Source列,则不确定是否仍需要FLAG列...