使用Pivot将两个动态查询合并到一个动态查询中

时间:2016-12-26 10:44:29

标签: sql sql-server sql-server-2008

我想将两个查询合并到一个查询中,但希望确保查询已优化

以下是第一个查询

DECLARE @colNames VARCHAR(MAX)= ( STUFF ( ( SELECT DISTINCT ',' + QUOTENAME(CustomeFieldName)
                                            FROM [dbo].CustomeFieldDetail
                                            WHERE companyid=1
                                            FOR XML PATH('') ),1,1,'' ));

DECLARE @EmpcolNames VARCHAR(MAX)= ( STUFF ( ( SELECT DISTINCT ',' + (FieldName)
                                               FROM [dbo].EmployeeConfig
                                               WHERE companyid=1                                                    
                                                 FOR XML PATH('') ),1,1,'' ));



DECLARE @command VARCHAR(MAX)= 'SELECT Distinct Id '+ @EmpcolNames+','+@colnames +'  FROM
                                (
                                    SELECT e.Id,e.Fname,e.Lname,'+ @EmpcolNames+',ec.EmployeeId,ec.CustomeFieldName,ec.FieldValue,ec.CompanyId
                                    FROM [dbo].EmployeeDetail AS e
                                    INNER JOIN [dbo].CustomeFieldDetail AS ec ON e.ID=ec.EmployeeId
                                    where e.CompanyId=1 and ec.CompanyId=1
                                ) AS tbl
                                PIVOT
                                (
                                    MAX(FieldValue) FOR CustomeFieldName IN(' + @colnames + ')
                                ) AS p ';



EXEC (@command);

以上查询的输出

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

查询2:

DECLARE @EmpEarnings VARCHAR(MAX)= ( STUFF ( (  SELECT DISTINCT ',' + '''' + (FieldName)+ ''''
                                               FROM [dbo].EarningConfig
                                               WHERE companyid=1
                                                 FOR XML PATH('') ),1,1,'' ))



DECLARE @EmpDeduction VARCHAR(MAX)= ( STUFF ( ( SELECT DISTINCT ',' + '''' + (FieldName)+ ''''
                                               FROM [dbo].DeductionConfig
                                               WHERE companyid=1
                                                 FOR XML PATH('') ),1,1,'' ));
DECLARE @Transactioncommand VARCHAR(MAX)='SELECT t.EmployeeId,
                                       x.[FieldName],
                                       x.[Value]
                                FROM Aquara7bc772839.EmpTransaction t
                                CROSS APPLY (
                                    SELECT t.c.value(''(@Name)[1]'', ''VARCHAR(1000)'') AS [FieldName],
                                           t.c.value(''(@Value)[1]'', ''FLOAT'') AS [Value]
                                    FROM TransactionFieldDetails.nodes(''//PayDetails/Column'') t(c)
                                ) x
                                    WHERE [FieldName] in('+@EmpDeduction+','+@EmpEarnings+' )
                                    AND CompanyId = 1
                                    AND ProcessDate = CONVERT(DATETIME,''01/09/2016 12:00:00 AM'',103)' ;



EXEC (@Transactioncommand );

以下是第二次查询的输出

EmpID |  FieldName |  Value
--    |----        | ------
1     | FH         | 1000  
1     | FB         | 1220  
2     | FHRA       | 3000  
2     | FB         | 3000  
3     | FB         | 3000  

我想根据我尝试过使用数据透视的员工ID将上述两个查询合并为一个,但是没有给出正确的结果

需要输出

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   

1 个答案:

答案 0 :(得分:1)

select t1.EmpID, t1.Code, t1.Name,
 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 ( /* query 1 */) t1
left join ( /* query 2 */) t2
on t1.empid = t2.empid
group by t1.EmpID, t1.Code, t1.Name,
 t1.Fld1, t1.Fld2, t1.Fld3, t1.Fld4;