Query SQL Server
20/11/2020
0
Alguém poderia me dar uma luz com a query abaixo?
Eu preciso ter como resultado
select FORMAT(E.TIMESTAMP , ''''MM-dd-yyyy HH:mm:ss'''') as ''''Date/Time'''',
Event = Case E.EVENTID When 66 Then ''''arrival''''
When 67 Then ''''departure'''' End,
Location = Case E.LOCATION When 11 Then ''''"""Code 11"" - C4 Production"''''
When 12 Then ''''"""Code 12"" - C4 Production"''''
When 17 Then ''''"""Code 17"" - C4 Production"''''
When 18 Then ''''"""Code 18"" - C4 Production"''''
When 27 Then ''''"""Code 27"" - C4 Office"''''
When 36 Then ''''"""Code 36"" - C2 Warehouse"'''' End,
U.ID as ''''ID''''
from dbo.EVENTS as E
Join dbo.USERS as U on E.USERNO = U.NO
where e.EVENTID<>'''''''' and e.EVENTID<>72 and U.ID<>'''''''' and e.TIMESTAMP >= DATEADD(day, -1, GETDATE())
and U.COMPANY in (''''C4 Production'''',''''C4 Office'''',''''C2 Warehouse'''')
order by e.TIMESTAMP
Eu preciso adiconar mais um JOIN ( Campo Company da tabela User = Campo Location da tabela Events):
--Join dbo.USERS as U on E.LOCATION = U.COMPANY
O problema é que na tabela Events o campo Location é númerico e na tabela user é String.
Alguém tem uma ideia para resolver esse problema?
Muito obrigado desde já!
Abraço
Bruno Banfi
Posts
20/11/2020
Alex William
Existe uma função no SQL Server chamada "convert" que você pode usar:
No seu JOIN coloque:
select FORMAT(E.TIMESTAMP , ''''MM-dd-yyyy HH:mm:ss'''') as ''''Date/Time'''', Event = Case E.EVENTID When 66 Then ''''arrival'''' When 67 Then ''''departure'''' End, Location = Case E.LOCATION When 11 Then ''''"""Code 11"" - C4 Production"'''' When 12 Then ''''"""Code 12"" - C4 Production"'''' When 17 Then ''''"""Code 17"" - C4 Production"'''' When 18 Then ''''"""Code 18"" - C4 Production"'''' When 27 Then ''''"""Code 27"" - C4 Office"'''' When 36 Then ''''"""Code 36"" - C2 Warehouse"'''' End, U.ID as ''''ID'''' from dbo.EVENTS as E Join dbo.USERS as U on E.USERNO = U.NO AND E.LOCATION = CONVERT(int, U.COMPANY) where e.EVENTID<>'''''''' and e.EVENTID<>72 and U.ID<>'''''''' and e.TIMESTAMP >= DATEADD(day, -1, GETDATE()) and U.COMPANY in (''''C4 Production'''',''''C4 Office'''',''''C2 Warehouse'''') order by e.TIMESTAMP
Poste depois se funcionou.
Espero ter ajudado. :D
20/11/2020
Bruno Banfi
Primeiramente obrigado por responder.
O resultado que espero é trazer as marcações somente dos relogios que o colaborador é cadastrado pela Company.
Exemplo: Se o colaborador A está cadastrado no Location 27, caso ele realize a marcação na Location 36, essa segunda será desconsiderada.
Tenho a tabela User que tem o cadastro daCampany:
NO Company
215 C4 Office
216 C4 Office
217 C4 Office
218 C4 Office
219 C4 Office
E tenho a tabela Events que tem o número do Location:
USERNO LOCATION
215 27
Caso a location seja igual a 27 a COMPANY deve ser C4 OFFICE.
Espero ter deixado mais claro.
Muito obrigado desde já!
20/11/2020
Bruno Banfi
select FORMAT(E.TIMESTAMP , 'MM-dd-yyyy HH:mm:ss') as 'Date/Time',
Event = Case E.EVENTID When 66 Then 'arrival'
When 67 Then 'departure' End,
Location = Case E.LOCATION When 11 Then '"""Code 11"" - C4 Production"'
When 12 Then '"""Code 12"" - C4 Production"'
When 17 Then '"""Code 17"" - C4 Production"'
When 18 Then '"""Code 18"" - C4 Production"'
When 27 Then '"""Code 27"" - C4 Office"'
When 36 Then '"""Code 36"" - C2 Warehouse"' End,
U.ID as 'ID'
from dbo.EVENTS as E
Join dbo.USERS as U on E.USERNO = U.NO
where e.EVENTID<>'' and e.EVENTID<>72 and U.ID<>'' and e.TIMESTAMP >= DATEADD(day, -1, GETDATE())
and U.COMPANY in ('C4 Production','C4 Office','C2 Warehouse')
and ((case when E.LOCATION=11 or E.LOCATION=12 or E.LOCATION=17 or E.LOCATION=18
then 'C4 Production' end)=U.COMPANY or (case when E.LOCATION=27 then 'C4 Office' end)=U.COMPANY
or (case when E.LOCATION=36 then 'C2 Warehouse' end)=U.COMPANY)
order by e.TIMESTAMP
Obrigado
20/11/2020
Emerson Nascimento
select FORMAT(E.TIMESTAMP , 'MM-dd-yyyy HH:mm:ss') 'Date/Time', (Case E.EVENTID When 66 Then 'Arrival' When 67 Then 'Departure' End) NmEvent, (Case E.LOCATION When 11 Then 'Code 11 - C4 Production' When 12 Then 'Code 12 - C4 Production' When 17 Then 'Code 17 - C4 Production' When 18 Then 'Code 18 - C4 Production' When 27 Then 'Code 27 - C4 Office' When 36 Then 'Code 36 - C2 Warehouse' End) NmLocation, U.ID from dbo.EVENTS E Join dbo.USERS U on U.NO = E.USERNO AND U.COMPANY = (CASE E.LOCATION WHEN 11 THEN 'C4 Production' WHEN 12 THEN 'C4 Production' WHEN 17 THEN 'C4 Production' WHEN 18 THEN 'C4 Production' WHEN 27 THEN 'C4 Office' WHEN 36 THEN 'C2 Warehouse' ELSE 'Undefined' END) where e.EVENTID<>'' and e.EVENTID<>72 and U.ID<>'' and e.TIMESTAMP >= DATEADD(day, -1, GETDATE()) and U.COMPANY in ('C4 Production','C4 Office','C2 Warehouse') order by e.TIMESTAMP
Clique aqui para fazer login e interagir na Comunidade :)