Declare @SQL varchar(max) = 'Select NOM_EMP as [X],PRENOM_EMP as [Y],'
+ (Select Stuff((Select ',[' +cast(N as varchar(25))+']='''''
From (Select Top (31) N=Row_Number() Over (Order By (Select null)) From master..spt_values n1) A
For XML Path
(''))
,1,1,'')
)
+ ' FROM EMPLOYE'
Exec(@SQL)
我在这里所做的是加载员工姓名+当月的天数(在本例中为31天)。上面的查询将导致如下所示:
employee表与(EMPLOYE_ID)
相关,例如Requests
表,每个请求都有一个start_date
和end_date
。
我要实现的是如果start_date
和end_date
的日号与单元格中的日号匹配,则写字母“ R”。
如果(第一位雇员)使用以下信息提出了请求: 2018年10月10日,并且 2018年8月14日
该表应如下所示:
这是现实的实现吗?如果是这样,我们将不胜感激。
编辑:
Declare @D1 datetime = '2018-08-01'
Declare @D2 datetime = '2018-08-31'
Declare @Cols varchar(max) = (Select Stuff((Select ',[' +cast(N as varchar(25))+']' From (Select Top (DateDiff(DAY,@D1,@D2)+1) N=Row_Number() Over (Order By (Select Null)) From master..spt_values n1) A For XML Path ('')),1,1,'') )
Declare @SQL varchar(max) = '
Declare @D1 date = '''+cast(@D1 as varchar(50))+'''
Declare @D2 date = '''+cast(@D2 as varchar(50))+'''
Select *
From (
Select firstName,LastName,
Item = day(d)
,Value = case when D between DEBUT_DRC and FIN_DRC then ''R'' else '''' end
From DEMANDE_RECUPERATION DC INNER JOIN EMPLOYE E ON DC.MAT_EMP = E.MAT_EMP
Cross Join (
Select Top (DateDiff(DAY,@D1,@D2)+1) D=DateAdd(DAY,-1+Row_Number() Over (Order By (Select Null)),@D1) From master..spt_values n1
) B
) src
Pivot (max(value) for Item in ('+@Cols+') ) pvt
'
Exec(@SQL)
输出:
答案 0 :(得分:1)
只需稍微扩展动态SQL
示例
Declare @D1 date = '2018-08-01'
Declare @D2 date = '2018-08-31'
Declare @Cols varchar(max) = (Select Stuff((Select ',[' +cast(N as varchar(25))+']' From (Select Top (DateDiff(DAY,@D1,@D2)+1) N=Row_Number() Over (Order By (Select Null)) From master..spt_values n1) A For XML Path ('')),1,1,'') )
Declare @SQL varchar(max) = '
Declare @D1 date = '''+cast(@D1 as varchar(50))+'''
Declare @D2 date = '''+cast(@D2 as varchar(50))+'''
Select *
From (
Select A.Employee
,Item = day(d)
,Value = case when D between start_date and end_date then ''R'' else '''' end
From YourRequestTable A
Cross Join (
Select Top (DateDiff(DAY,@D1,@D2)+1) D=DateAdd(DAY,-1+Row_Number() Over (Order By (Select Null)),@D1) From master..spt_values n1
) B
) src
Pivot (max(value) for Item in ('+@Cols+') ) pvt
'
Exec(@SQL)
返回
答案 1 :(得分:0)
或多或少:
WITH cteDates AS ( SELECT [calDt] = @dtStart
UNION ALL
SELECT [CalDt] = DATEADD(DAY, [calDt], 1) FROM cteDates WHERE [calDt] <= @dtEnd)
SELECT pvt.*
FROM ( SELECT e.EmployeeID, d.[calDt], [reqCount] = COUNT(*)
FROM cteDates d
CROSS JOIN Employee e
LEFT JOIN reqTable rt ON rt.EmployeeID = e.EmployeeID
AND rt.d1 >= d.[cteDt]
AND rt.d2 <= d.[cteDt]
GROUP BY e.EmployeeID, d.[calDt]
) unpvt
PIVOT ( SUM(reqCount) FOR [calDt] IN ( [1],[2],...[31] )
) AS pvt
无需动态SQL