我有以下表格结构。我想要总休假天数,尤其是月份和年份。我也提到了我的预期输出。
Table : 1
tblAbsent
Id Code LeaveDate Leave LeaveDay
934 002DSK 2018-08-10 EL 1
934 002DSK 2018-08-11 EL 1
934 002DSK 2018-08-12 EL 1
934 002DSK 2018-09-10 EL 1
934 002DSK 2018-10-12 EL 1
934 002DSK 2018-10-13 EL 1
934 002DSK 2018-10-14 EL 1
934 002DSK 2018-10-15 EL 1
Table : 2 tblEmpdetails
Id Code tblAbsentID Month Year
1172 002DSK 934 4 2018
1259 002DSK 934 5 2018
1335 002DSK 934 6 2018
1411 002DSK 934 7 2018
1487 002DSK 934 8 2018
1563 002DSK 934 9 2018
1639 002DSK 934 10 2018
1715 002DSK 934 11 2018
1791 002DSK 934 12 2018
Table : 3 LeaveOpening
Id tblAbsentId Leave LeaveOpen
80 934 EL 24
我试图用以下结果:
SELECT DISTINCT
et.EmployeeId
,Month AS [Monthnumber]
,DATENAME(mm, DATEADD(mm, Month, -1)) + ' - ' + CONVERT(NVARCHAR(20), Year, 103) AS [Calendar Year Of Service]
,CONCAT('1-', et.MD) AS [Wages during From...To...]
,et.PD AS [No. Of Days Work Performed],
--sum(lb.HFDay) as EL,
lo.LeaveOpening
,(SELECT
SUM(lb.HFDay)
FROM dbo.tblAbsent lb
WHERE lb.EmployeeId = 934 and lb.Leave='EL' --and lb.LeaveDate >='2018-08-01' and lb.LeaveDate <= '2018-08-30'
)
AS EL
,(SELECT
SUM(lb.HFDay)
FROM dbo.tblAbsent lb
WHERE lb.EmployeeId = 934 and lb.Leave='ML'
) as MaternityLeave
,et.PD [Total Of Cols 4 To 7]
,ed.FName [Employee Name]
,ed.FatherName [Father/Spouses Name]
,CONVERT(VARCHAR(15), ed.DateOfJoining, 103) [Date Of Joining Service]
FROM dbo.tblEmpdetails et
INNER JOIN dbo.EmployeeDetail ed
ON et.EmployeeId = ed.Id
INNER JOIN dbo.LeaveOpening lo
ON lo.EmployeeId = et.EmployeeId
inner join dbo.tblAbsent lb on
lb.EmployeeId = et.EmployeeId
WHERE et.EmployeeId = 934
AND et.CompanyId = 1
AND Category IN (1, 2, 3, 4)
AND et.ProcessDate >= '2018-01-01'
AND et.ProcessDate <= '2018-12-31' and lb.LeaveDate >='2018-08-01' and lb.LeaveDate <= '2018-08-30'
GROUP BY et.EmployeeId
,Month
,Year
,MD
,Gross
,PD
,ed.FName
,ed.FatherName
,ed.DateOfJoining
,LeaveOpen
根据表2,我无法按月分组。我希望在tblAbsent表中,实际输出为Sum(LeaveDay)为特定月份的EL。
Expected Output:
tblAbsentId Monthnumber Calendar Year Of Service LeaveOpen EL
934 4 April - 2018 24 0
934 5 May - 2018 24 0
934 6 June - 2018 24 0
934 7 July - 2018 24 0
934 8 August - 2018 24 3
934 9 September - 2018 24 1
934 10 October - 2018 24 4
934 11 November - 2018 24 0
934 12 December - 2018 24 0
答案 0 :(得分:2)
我在上面的问题中创建了您显示的表格,并在表格中插入了相同的数据。我可以使用以下查询生成您期望的相同输出:
SELECT TE.tblAbsentId, TE.Month, TE.Year, LO.LeaveOpen,
SUM(ISNULL(TA.LeaveDay, 0)) TotalLeave
FROM tblEmpdetails TE
INNER JOIN LeaveOpening LO ON TE.tblAbsentId = LO.tblAbsentId
LEFT JOIN tblAbsent TA ON TE.tblAbsentId = TA.Id AND TE.Month =
MONTH(TA.LeaveDate) AND TE.Year = YEAR(TA.LeaveDate)
GROUP BY TE.tblAbsentId, TE.Month, TE.Year, LO.LeaveOpen
上述查询的输出如下所示:
答案 1 :(得分:0)
您可以使用下面的查询
的 See live demo 强>
select T.tblAbsentId,
MonthNumber=T.month,
[Calendar year of service]=DATENAME(mm, DATEADD(mm, T.Month, -1)) + ' - ' + CONVERT(NVARCHAR(20), Year, 103),
Leaveopen,
EL= COALESCE(s,0)
from tblEmpdetails T
left join
(
select
id,
m=month(leavedate),
leave,
s=sum(leaveday)
from tblAbsent
group by id, month(leavedate),leave
) A
on T.tblAbsentId=A.id and T.Month=A.m
left join
LeaveOpening L
on L.tblAbsentid=T.tblAbsentId