I have a problem with my SQL queries (I know I'm very bad at this task :( )
Using SLQ Server 2014, I've got a table that stores bank days containing the name of the day and a datetime.
On the other hand there's a table with technicians that has worked over a project. They could work regular days or even those bank days.
I get a query in order to generate payrolls that get all of those technicians that have worked getting techcode, name, project id and date but I need to get another list (UNION) for those all tehcnicians that have not worked during bank holidays because they also need to be inlcuded in the report for payroll (bank days are payed as well but need to be differentiate).
I tried with joins to get that list that should contain tech code, name, project id and date from Holidays if they didn't work that day (that info is retrieved in the first query because is stored in the Sessions table. The only value that have both tables in common is the date.
I'll give you my queries so far:
-- To get real work hours / days within date range
select
distinct s.TechnicianCode, t.Name, s.JobProjectId as jobId,
s.SignInDate as [date], 'w'
from
JobSession s
inner join
Technician t on t.TechnicianCode = s.TechnicianCode
inner join
JobAssignedTech jt on jt.TechCode = s.TechnicianCode
left join
LeaveDate ld on ld.TechnicianCode = s.TechnicianCode
where
(cast(s.SignInDate as date) >= cast(@DateFrom as date)
and cast(s.SignInDate as date) <= cast(@DateTo as date))
and jt.IsActive = 1
Holidays contains:
HolidayID HolidayDate Name.....
3 2015-08-19 00:00:00.000 August Bank Holiday ...
5 2015-08-20 00:00:00.000 August Bank Holiday ...
For example, for a query like this
select
distinct s.TechnicianCode, t.Name, s.JobProjectId, s.SignInDate
from
JobSession s
inner join
Technician t on s.TechnicianCode = t.TechnicianCode
where
cast(s.SignInDate as date) between cast('2015/08/17' as date) and cast('2015/08/24' as date)
I get
TechnicianCode Name JobProjectId SignInDate
282 Patrick Flood TEST JOB CHQ E2443 2015-08-17
332 Anthony Farrell TEST JOB CHQ E2443 2015-08-17
342 Ciaran Kimmage TEST JOB CHQ E2443 2015-08-17
343 Anthony Clinton TEST JOB CHQ E2443 2015-08-17
440 Darragh Byrne TEST JOB CHQ E2443 2015-08-17
440 Darragh Byrne TEST JOB CHQ E2443 2015-08-20
489 Thomas Cunningham TEST JOB CHQ E2443 2015-08-17
491 Aidan Lee TEST JOB CHQ E2443 2015-08-17
497 Brian Canavan TEST JOB CHQ E2443 2015-08-17
TE Test Sandyford site 07.15 2015-08-17
So I would need to get a list showing those technicians but date should be bank holidays date and only if they didn't work during those days so, for example, technician with code 440 must not be in the list twice because he worked on the 20th of August and it should looks like this:
TechnicianCode Name JobProjectId SignInDate
282 Patrick Flood TEST JOB CHQ E2443 2015-08-19
282 Patrick Flood TEST JOB CHQ E2443 2015-08-20
332 Anthony Farrell TEST JOB CHQ E2443 2015-08-19
332 Anthony Farrell TEST JOB CHQ E2443 2015-08-20
342 Ciaran Kimmage TEST JOB CHQ E2443 2015-08-19
342 Ciaran Kimmage TEST JOB CHQ E2443 2015-08-20
343 Anthony Clinton TEST JOB CHQ E2443 2015-08-19
343 Anthony Clinton TEST JOB CHQ E2443 2015-08-20
440 Darragh Byrne TEST JOB CHQ E2443 2015-08-19
489 Thomas Cunningham TEST JOB CHQ E2443 2015-08-19
489 Thomas Cunningham TEST JOB CHQ E2443 2015-08-20
491 Aidan Lee TEST JOB CHQ E2443 2015-08-19
491 Aidan Lee TEST JOB CHQ E2443 2015-08-20
497 Brian Canavan TEST JOB CHQ E2443 2015-08-19
497 Brian Canavan TEST JOB CHQ E2443 2015-08-19
TE Test Sandyford site 07.15 2015-08-19
TE Test Sandyford site 07.15 2015-08-20
I have tried with UNION, SUB QUERIES, EXIST, IN, INNER and so on I get nothing like that :-/
A little help will be so thankful.