У меня есть следующая структура таблицы.Я хочу общее количество дней отпуска, особенно месяц и год.Также я упомянул ожидаемый результат.
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. Я хочу фактическийвыводится сумма (LeaveDay) как EL для определенного месяца в таблице tblAbsent.
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