Join Vertical & Horizontal table in SQL Server using Pivot
<p>I want to join two tables and combine into one but problem is one table is in Horizontal Format and other in Vertical format.</p>

<p>below is the table structure and join will be on Employeeid: </p>

<p>Table 1 : EmpDetail</p>

<pre><code>ID | CODE   | Name
-- |--------| ---
1  | 1008M  | ABC
2  | 1039E  | XYZ
3  | 1040E  | TYS
</code></pre>

<p>Table  2 : EmpCustomeDetail</p>

<pre><code>EmpID | FiledName   | FieldValue 
--    |--------     |  ---
1     | FlD1        | temp1
1     | FlD2        | temp2
1     | FlD3        | temp3
2     | FlD1        | temp1
3     | FLD4        | temp6
</code></pre>

<p>Desired Output Required : </p>

<pre><code>EmpID |  Code |  Name | Fld1  | Fld2  | Fld3  | Fld4 
--    |----   | ------|  ---  | ----  |----   |----  
1     | 1008M | ABC   | temp1 | temp2 | temp3 | null
2     | 1039E | XYZ   | temp1 | null  | null  | null
3     | 1040E | TYS   | null  | null  | null  | temp6
</code></pre>

<p>I had tried using Pivot Query but it is not giving exact output which i requried
Below is the query so far i have tried</p>

<pre><code>SELECT A.*
FROM (
    SELECT
        e.Id,
        e.code,
        e.Fname,        
    FROM EmpDetail e  
    LEFT JOIN (
        SELECT *
        FROM (
            SELECT
                  d.CustomeFieldName
                , c.ComboValue
                , d.EmployeeId              
            FROM EmpCustomeDetail d
        ) src 
        PIVOT (
            MAX(FieldValue)          
        ) src2
    ) c ON e.Id = c.EmployeeId       

) A
</code></pre>