Hi @Сергей Ходор
Try this query:
SELECT P.ID,P.name,P.SurName
,DATEDIFF(day,date_of_employment,GETDATE())AS Employment
,Business_trip
,Sick
,Vacation
FROM Personal_Card P
LEFT JOIN (SELECT UserID,SUM(DATEDIFF(day,First_day_business_trip,Last_day_business_trip))AS Business_trip FROM Business_trip GROUP BY UserID) B
ON P.ID=B.UserID
LEFT JOIN (SELECT UserID,SUM(DATEDIFF(day,First_day_Sick,Last_day_Sick))AS Sick FROM Sick_leave GROUP BY UserID) S
ON P.ID=S.UserID
LEFT JOIN (SELECT UserID,SUM(DATEDIFF(day,First_day_Vacation,Last_day_Vacation))AS Vacation FROM Vacation GROUP BY UserID) V
ON P.ID=V.UserID
Note:
- Please post your code by clicking this button: . Since we cannot copy your query code from an image.
- As Erland suggested, for this type of question, you help yourself a lot if you post CREATE TABLE statements for your tables, preferably simplified to focus on the actual problem. Together with INSERT statements with sample data and then the desired result given the sample. So that we’ll get a right direction and make some test. Even more so, if you also give a brief description of why you want that particular result.
Best regards,
LiHong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.