请帮我把这个输出。
我的输入:
-----------------------------------------------------
EMP_Id | EMP_Name | Date_Worked | Enc_No
---------------------------------------------------
100 | Aj | 2016-10-10 00:00:00.000 | 123
100 | Aj | 2016-10-10 00:00:00.000 | 124
101 | Siv | 2016-10-10 00:00:00.000 | 125
101 | Siv | 2016-10-11 00:00:00.000 | 126
102 | Nav | 2016-10-10 00:00:00.000 | 127
102 | Nav | 2016-10-11 00:00:00.000 | 128
--------------------------------------------------------
我需要这样的输出
---------------------------------------------------------
EmplyeeId | EmplyeeName | 10/10/2016 | 11/10/2016
---------------------------------------------------------
100 | Aj | 2 | 0
101 | Siv | 1 | 1
102 | Nav | 1 | 1
---------------------------------------------------------
答案 0 :(得分:1)
答案 1 :(得分:0)
我认为你在谈论动态支点。 我在我的项目中实现了动态数据透视。我根据你的输入提供了示例脚本。请检查它。我相信这会对你有所帮助。
此处EmployeeWork是TableName。
DECLARE @FactorText varchar(max)=null
SELECT @FactorText = COALESCE(@FactorText + '], ', '') + CAST('[' + FactorText AS VARCHAR(50))
FROM (SELECT DISTINCT CONVERT(varchar, Date_Worked, 103) as FactorText FROM EmployeeWork) AS EmployeeWork
SET @FactorText = @FactorText + ']'
DECLARE @DynamicPIVOT AS VARCHAR(MAX)
SELECT @DynamicPIVOT = 'SELECT EMP_Id,emp,' + @FactorText + ' FROM ( select EMP_Id , EMP_Name as emp, EMP_Name
as AppliedValue, CONVERT(varchar, Date_Worked, 103) as FactorText FROM EmployeeWork
) EmployeeWorkData
PIVOT
(
count(AppliedValue) FOR FactorText IN (' + @FactorText + ')
) Result;'
EXEC (@DynamicPIVOT)
如果有任何查询告诉我。根据您的要求,它运作正常。
谢谢。