Reputation: 6398
I want to join two tables and combine it into one but problem is one table is in horizontal format other is in vertical
Below are table structures Table 1 :
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
Table 2 :
EmpID | FieldName | Value
-- |---- | ------
1 | FH | 1000
1 | FB | 1220
2 | FHRA | 3000
2 | FB | 3000
3 | FB | 3000
Desired Output :
EmpID | Code | Name | Fld1 | Fld2 | Fld3 | Fld4 | FH | FB | FHRA
-- |---- | ------| --- | ---- |---- |---- | --- |--- | ----
1 | 1008M | ABC | temp1 | temp2 | temp3 | null |1000 |1210| 0
2 | 1039E | XYZ | temp1 | null | null | null |0 |3000| 3000
3 | 1040E | TYS | null | null | null | temp6|0 |3000| 0
I had tried using Pivot query but it is not working as expected.
Upvotes: 2
Views: 868
Reputation: 84
declare @temp table(empid int,fh int,fb int, fhra int)
insert into @temp
SELECT *
FROM (
SELECT
empid,fieldname as [field],value as val
FROM dbo.emp
) as s
PIVOT
(
min( val)
FOR [field] IN (fh,fb,fhra)
)AS pvt
select * from @temp join table1
Now join temporary table and table 1.
Upvotes: 1
Reputation: 5656
You have to use dynamic query as below and you can test is by adding more FieldName
s
CREATE TABLE #table1(EmpID INT,
Code VARCHAR(20),
Name VARCHAR(20),
Fld1 VARCHAR(20),
Fld2 VARCHAR(20),
Fld3 VARCHAR(20),
Fld4 VARCHAR(20))
INSERT INTO #table1 VALUES
(1, '1008M','ABC','temp1','temp2','temp3',NULL),
(2, '1039E','XYZ','temp1',NULL,NULL,null),
(3, '1040E','TYS',null,NULL,NULL,'temp6')
CREATE TABLE #table2(EmpID INT, FieldName VARCHAR(20), VALUE INT)
INSERT INTO #table2 VALUES
(1,'FH',1000),
(1,'FB',1220),
(2,'FHRA',3000),
(2,'FB',3000),
(3,'FB',3000)
DECLARE @col VARCHAR(MAX)
DECLARE @sql VARCHAR(MAX)
SELECT @col = COALESCE(@col + ', ','') + QUOTENAME(FieldName)
FROM #table2 GROUP BY FieldName
SELECT @col -- This gives: [FB], [FH], [FHRA]
-- Now setting this @col variable in the Dynamic SQL.
SET @sql = '
select EmpID, Code, Name,Fld1,Fld2,Fld3,Fld4, ' + @col + '
from (select a.EmpID, Code, Name,Fld1,Fld2,Fld3,Fld4, b.FieldName, b.value
from #table1 a
join #table2 b on a.empid=b.empid)p
PIVOT(MAX (VALUE) FOR FieldName IN ( ' + @col + ' )
) AS pvt
'
PRINT @sql
EXEC (@sql)
OUTPUT:
EmpID Code Name Fld1 Fld2 Fld3 Fld4 FB FH FHRA
1 1008M ABC temp1 temp2 temp3 NULL 1220 1000 NULL
2 1039E XYZ temp1 NULL NULL NULL 3000 NULL 3000
3 1040E TYS NULL NULL NULL temp6 3000 NULL NULL
Upvotes: 1
Reputation: 1455
try this working fine
;with demo1 as (
select * from Table_1
), a as
(
SELECT *
FROM Table_2
PIVOT(SUM(value)
FOR Fieldname IN (FH, FB,FHRA)) AS PVTTable
)select demo1.EmpID,demo1.Code,demo1.Name,demo1.Fld1,demo1.Fld2,demo1.Fld3,demo1.Fld4,a.FH,a.FB,a.FHRA
from a inner join demo1 on a.EmpID=demo1.EmpID
OutPut:
Upvotes: 1
Reputation: 39477
Try this:
select t1.empid,
t1.code,
t1.fld1,
t1.fld2,
t1.fld3,
t1.fld4,
sum(case when t2.fieldname = 'FH' then t2.value else 0 end) FH,
sum(case when t2.fieldname = 'FB' then t2.value else 0 end) FB,
sum(case when t2.fieldname = 'FHRA' then t2.value else 0 end) FHRA
from table1 t1
left outer join table2 t2
on t1.empid = t2.empid
group by t1.empid,
t1.code,
t1.fld1,
t1.fld2,
t1.fld3,
t1.fld4;
Upvotes: 0