Ok. I saw how to post a question. Here is my datatable.
Than I launch the query (i will not create a table, just the query for extract records):
SELECT log.id, Convert(varchar, log.DataLog,103) as Data,o.nome, log.NomePc,
log.IpPc, log.Descrizione, log.idoperatore, log.Overtime, DataLog,
CONVERT (datetime,log.DataLog,120 ) as datafunk, m.operatore,
orario as
CASE
WHEN Descrizione LIKE 'Avvio%' THEN 1
WHEN Descrizione LIKE 'Inattivi%' THEN 2
WHEN Descrizione LIKE 'Ripresa%' THEN 3
WHEN Descrizione LIKE 'Stop%' THEN 4 END as azione
from dbo.lst_activitiLog log
left join dbo.lst_activitiMachine m on log.IpPc=m.Ip
inner join main.dbo.operatori o on convert(varchar,o.idoperatore)=convert(varchar,m.operatore)
left join Main.dbo.Un_Op_Gruppi uog on uog.IdOperatore = o.idoperatore
where log.descrizione
not like 'Inattivit%' and m.operatore = '1002408' and m.operatore = log.idoperatore
order by Nome,DataLog
Translation for you:
Data = Date
nome = user (name and surname)
NamePc = NomePc
IpPc = IpPc
Descrizione = Description
Idoperatore = idUser
Overtime = overtime
DataLog = Datalog (the exact hour user press start or stop)
Datafunk = Datafunk (i use this record for php code)
Operatore = operatore (the person)
Azione = Action (1 - Start 4 - Stop)
When the user press start, recording the datalog in the table. Take the case 23/06/2015
23/06/2015 Action 1 = Time: 15.05.00
23/06/2015 Action 1 = Time: 16.29.28
23/06/2015 Action 1 = Time: 16.32.13
23/06/2015 Action 1 = Time: 17.59.42
23/06/2015 Action 4 = Time: 21.32.39
I want to extract only the first action 1 and the last action 4, I thought using min and max for the datalog was ok, but I have problem with the group by, error with the syntax.
I tried also the OVER instruction but I have error:
Incorrect syntax near the keyword 'over'.
the code is
SELECT log.id, Convert(varchar, log.DataLog,103) as Data,o.nome, log.NomePc,
log.IpPc, log.Descrizione, log.idoperatore, log.Overtime, DataLog, uog.idgruppolavorazione,
CONVERT (datetime,log.DataLog,120 ) as datafunk, m.operatore,
min(DataLog) over (partition by m.operatore) AS minima,
max(datalog) over (partition by m.operatore) AS massima,
CASE
WHEN Descrizione LIKE 'Avvio%' THEN 1
WHEN Descrizione LIKE 'Inattivi%' THEN 2
WHEN Descrizione LIKE 'Ripresa%' THEN 3
WHEN Descrizione LIKE 'Stop%' THEN 4 END as azione,
CASE
WHEN idgruppolavorazione ='66' THEN 1
WHEN idgruppolavorazione ='79' THEN 2 END as gruppo
from dbo.lst_activitiLog log
left join dbo.lst_activitiMachine m on log.IpPc=m.Ip
inner join main.dbo.operatori o on convert(varchar,o.idoperatore)=convert(varchar,m.operatore)
left join Main.dbo.Un_Op_Gruppi uog on uog.IdOperatore = o.idoperatore
where log.descrizione
not like 'Inattivit%' and m.Operatore='1002408'
order by Nome,DataLog
Now I see the manual for the OVER, PARTITION and CAST.
Meanwhile I hope you understand my question.
Ok. the basic information. I'm usgin MSSQL, theresn't OVER or DATE function, I must use CONVERT.