简体   繁体   English

使用动态数据透视查询联接两个表

[英]Join Two tables using Dynamic Pivot Query

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 : 下面是表结构表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 : 表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. 我曾尝试使用数据透视查询,但无法正常工作。

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. 现在加入临时表和表1。

You have to use dynamic query as below and you can test is by adding more FieldName s 您必须使用以下动态查询,并且可以通过添加更多FieldName进行测试

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

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: 输出:

在此处输入图片说明

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM