SELECT FIO CreatedDate EL InvoiceKey EL Sernum Comment GuiltName Defec

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
SELECT T.FIO,
T.CreatedDate,
EL.InvoiceKey,
EL.Sernum,
T.Comment,
T.GuiltName,
T.DefectCategory
,T.DefectType
,T.ChangedSernum
,T.ChangedForSernum
,T.DocAttachKey
FROM Erp_Invoice.dbo.EventLog EL
RIGHT JOIN (
SELECT RC.[Id]
,RC.[GuiltyId]
,RC.[Comment]
,RC.[CreatedDate]
,RC.[p_no]
,RC.[InvoiceKey]
,RC.[Sernum]
,RG.ReasonName AS GuiltName
,Erp_Invoice.[dbo].[f_FIO](RC.[p_no], 1) AS FIO
,RCat.[Name] AS DefectCategory
,RT.[Name] AS DefectType
,RCD.Sernum AS ChangedSernum
,RCD.ChangedForSernum
,RA.Id AS DocAttachKey
, (SELECT TOP 1 UnitName
FROM Erp_Invoice.dbo.[OTK_Result]
WHERE RepiarCommentId = RC.Id) UnitName
FROM Erp_Invoice.[dbo].[RepiarComments] RC
JOIN Erp_Invoice.[dbo].[OSPSN_ListOfViolations] RG ON RC.GuiltyId = RG.Id
LEFT JOIN Erp_Invoice.[dbo].RepiarCommentDefects RCD ON RCD.RepiarCommentId = RC.Id
LEFT JOIN Erp_Invoice.[dbo].RepiarCategory RCat ON RCat.Id = RCD.DefectCategory
LEFT JOIN Erp_Invoice.[dbo].RepiarType RT ON RT.Id = RCD.DefectType
LEFT JOIN [Erp_Invoice].[dbo].[RepiarAttachRegistr] RA ON RA.RepiarCommentId = RC.Id
) T ON T.Sernum = EL.Sernum
WHERE EventId = 2
AND UnitId IN (54)
AND EL.CreatedDate BETWEEN '08/01/2019 08:00:00' AND GETDATE()
ORDER BY CreatedDate