Fernandinha Posted September 22, 2011 Report Share Posted September 22, 2011 (edited) Olá pessoal...Estou com uma dúvida:Tenho uma tabela e preciso retornar todos os campos porém com a maior data de evidencia.Tenho essa tabela IdTurmaPQ DataAula Frequencia DtCadastro DtEvidencia11162 2011-08-01 00:00:00.000 Presente 2011-09-22 00:00:00.000 2011-09-01 11:08:53.40711162 2011-08-01 00:00:00.000 Justificada 2011-09-22 00:00:00.000 2011-09-22 10:14:02.75011162 2011-08-02 00:00:00.000 Presente 2011-09-21 00:00:00.000 2011-08-31 16:41:26.98311162 2011-08-03 00:00:00.000 Presente 2011-09-21 00:00:00.000 2011-08-31 16:42:30.98311162 2011-08-04 00:00:00.000 Presente 2011-09-22 00:00:00.000 2011-08-31 16:50:39.87711162 2011-08-05 00:00:00.000 Ausente 2011-09-22 00:00:00.000 2011-08-31 16:53:04.07711162 2011-08-05 00:00:00.000 Justificada 2011-09-22 00:00:00.000 2011-09-22 09:54:39.26711162 2011-08-08 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL11162 2011-08-09 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL11162 2011-08-10 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL11162 2011-08-11 00:00:00.000 Presente 2011-09-22 00:00:00.000 2011-08-31 20:00:08.21711162 2011-08-12 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL11162 2011-08-13 00:00:00.000 Ausente 2011-09-21 00:00:00.000 NULL11162 2011-08-14 00:00:00.000 Justificada 2011-09-22 00:00:00.000 NULL11162 2011-08-15 00:00:00.000 Justificada 2011-09-22 00:00:00.000 NULLMeu retorno tem que ser exatamente esse:IdTurmaPQ DataAula Frequencia DtCadastro DtEvidencia11162 2011-08-01 00:00:00.000 Justificada 2011-09-22 00:00:00.000 2011-09-22 10:14:02.75011162 2011-08-02 00:00:00.000 Presente 2011-09-21 00:00:00.000 2011-08-31 16:41:26.98311162 2011-08-03 00:00:00.000 Presente 2011-09-21 00:00:00.000 2011-08-31 16:42:30.98311162 2011-08-04 00:00:00.000 Presente 2011-09-22 00:00:00.000 2011-08-31 16:50:39.87711162 2011-08-05 00:00:00.000 Justificada 2011-09-22 00:00:00.000 2011-09-22 09:54:39.26711162 2011-08-08 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL11162 2011-08-09 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL11162 2011-08-10 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL11162 2011-08-11 00:00:00.000 Presente 2011-09-22 00:00:00.000 2011-08-31 20:00:08.21711162 2011-08-12 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL11162 2011-08-13 00:00:00.000 Ausente 2011-09-21 00:00:00.000 NULL11162 2011-08-14 00:00:00.000 Justificada 2011-09-22 00:00:00.000 NULL11162 2011-08-15 00:00:00.000 Justificada 2011-09-22 00:00:00.000 NULLFiz um SELECT DISTINCT mas não funciona pelo fato das linhas não serem identicas...Desde já agradeço a ajuda... Edited September 23, 2011 by Fernandinha Quote Link to comment Share on other sites More sharing options...
0 fulvio Posted September 22, 2011 Report Share Posted September 22, 2011 Boa tarde Fernandinha, Poderá utilizar o "max" da DtEvidencia e o "group by" e "Order by" por DataAula. Quote Link to comment Share on other sites More sharing options...
0 Fernandinha Posted September 22, 2011 Author Report Share Posted September 22, 2011 Eu utilizei o Max e o Group By, porém só me retornou a linha que contem a data maior de evidencia e tem linhas que não tem data de evidencia que eu preciso que retorne também. Quote Link to comment Share on other sites More sharing options...
0 Fernandinha Posted September 23, 2011 Author Report Share Posted September 23, 2011 Olá, segue a resolução do meu problema, caso alguém precise...select * from Tabela as twhere (t.DtEvidencia is null) or (t.DtEvidencia in (select max(DtEvidencia) from Tabela where (IdTurmaPQ = t.IdTurmaPQ) and (DataAula = t.DataAula))) Quote Link to comment Share on other sites More sharing options...
0 fulvio Posted September 23, 2011 Report Share Posted September 23, 2011 Bom dia Fernandinha, Segue exemplo:CREATE TABLE #Teste (IdTurmaPQ INT,DataAula DATETIME,Frequencia VARCHAR(20),DtCadastro DATETIME,DtEvidencia DATETIME) INSERT INTO #teste VALUES (11162,'2011-08-01 00:00:00.000','Presente','2011-09-22 00:00:00.000','2011-09-01 11:08:53.407') INSERT INTO #teste VALUES (11162,'2011-08-01 00:00:00.000','Justificada','2011-09-22 00:00:00.000','2011-09-22 10:14:02.750') INSERT INTO #teste VALUES (11162,'2011-08-02 00:00:00.000','Presente','2011-09-21 00:00:00.000','2011-08-31 16:41:26.983') INSERT INTO #teste VALUES (11162 ,'2011-08-03 00:00:00.000','Presente','2011-09-21 00:00:00.000','2011-08-31 16:42:30.983') INSERT INTO #teste VALUES (11162 ,'2011-08-04 00:00:00.000','Presente','2011-09-22 00:00:00.000','2011-08-31 16:50:39.877') INSERT INTO #teste VALUES (11162 ,'2011-08-05 00:00:00.000','Ausente','2011-09-22 00:00:00.000','2011-08-31 16:53:04.077') INSERT INTO #teste VALUES (11162 ,'2011-08-05 00:00:00.000','Justificada','2011-09-22 00:00:00.000','2011-09-22 09:54:39.267') INSERT INTO #teste VALUES (11162 ,'2011-08-08 00:00:00.000','Ausente','2011-08-31 00:00:00.000',NULL) INSERT INTO #teste VALUES (11162 ,'2011-08-09 00:00:00.000','Ausente','2011-08-31 00:00:00.000', NULL) INSERT INTO #teste VALUES (11162 ,'2011-08-10 00:00:00.000','Ausente','2011-08-31 00:00:00.000', NULL) INSERT INTO #teste VALUES (11162 ,'2011-08-11 00:00:00.000','Presente','2011-09-22 00:00:00.000','2011-08-31 20:00:08.217') INSERT INTO #teste VALUES (11162 ,'2011-08-12 00:00:00.000','Ausente','2011-08-31 00:00:00.000',NULL) INSERT INTO #teste VALUES (11162 ,'2011-08-13 00:00:00.000','Ausente','2011-09-21 00:00:00.000',NULL) INSERT INTO #teste VALUES (11162 ,'2011-08-14 00:00:00.000','Justificada','2011-09-22 00:00:00.000', NULL) INSERT INTO #teste VALUES (11162 ,'2011-08-15 00:00:00.000','Justificada','2011-09-22 00:00:00.000', NULL) SELECT DataAula, max(DtEvidencia) FROM #Teste GROUP BY DataAula ORDER BY DataAula Quote Link to comment Share on other sites More sharing options...
Question
Fernandinha
Olá pessoal...
Estou com uma dúvida:
Tenho uma tabela e preciso retornar todos os campos porém com a maior data de evidencia.
Tenho essa tabela
IdTurmaPQ DataAula Frequencia DtCadastro DtEvidencia
11162 2011-08-01 00:00:00.000 Presente 2011-09-22 00:00:00.000 2011-09-01 11:08:53.407
11162 2011-08-01 00:00:00.000 Justificada 2011-09-22 00:00:00.000 2011-09-22 10:14:02.750
11162 2011-08-02 00:00:00.000 Presente 2011-09-21 00:00:00.000 2011-08-31 16:41:26.983
11162 2011-08-03 00:00:00.000 Presente 2011-09-21 00:00:00.000 2011-08-31 16:42:30.983
11162 2011-08-04 00:00:00.000 Presente 2011-09-22 00:00:00.000 2011-08-31 16:50:39.877
11162 2011-08-05 00:00:00.000 Ausente 2011-09-22 00:00:00.000 2011-08-31 16:53:04.077
11162 2011-08-05 00:00:00.000 Justificada 2011-09-22 00:00:00.000 2011-09-22 09:54:39.267
11162 2011-08-08 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL
11162 2011-08-09 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL
11162 2011-08-10 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL
11162 2011-08-11 00:00:00.000 Presente 2011-09-22 00:00:00.000 2011-08-31 20:00:08.217
11162 2011-08-12 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL
11162 2011-08-13 00:00:00.000 Ausente 2011-09-21 00:00:00.000 NULL
11162 2011-08-14 00:00:00.000 Justificada 2011-09-22 00:00:00.000 NULL
11162 2011-08-15 00:00:00.000 Justificada 2011-09-22 00:00:00.000 NULL
Meu retorno tem que ser exatamente esse:
IdTurmaPQ DataAula Frequencia DtCadastro DtEvidencia
11162 2011-08-01 00:00:00.000 Justificada 2011-09-22 00:00:00.000 2011-09-22 10:14:02.750
11162 2011-08-02 00:00:00.000 Presente 2011-09-21 00:00:00.000 2011-08-31 16:41:26.983
11162 2011-08-03 00:00:00.000 Presente 2011-09-21 00:00:00.000 2011-08-31 16:42:30.983
11162 2011-08-04 00:00:00.000 Presente 2011-09-22 00:00:00.000 2011-08-31 16:50:39.877
11162 2011-08-05 00:00:00.000 Justificada 2011-09-22 00:00:00.000 2011-09-22 09:54:39.267
11162 2011-08-08 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL
11162 2011-08-09 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL
11162 2011-08-10 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL
11162 2011-08-11 00:00:00.000 Presente 2011-09-22 00:00:00.000 2011-08-31 20:00:08.217
11162 2011-08-12 00:00:00.000 Ausente 2011-08-31 00:00:00.000 NULL
11162 2011-08-13 00:00:00.000 Ausente 2011-09-21 00:00:00.000 NULL
11162 2011-08-14 00:00:00.000 Justificada 2011-09-22 00:00:00.000 NULL
11162 2011-08-15 00:00:00.000 Justificada 2011-09-22 00:00:00.000 NULL
Fiz um SELECT DISTINCT mas não funciona pelo fato das linhas não serem identicas...
Desde já agradeço a ajuda...
Edited by FernandinhaLink to comment
Share on other sites
4 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.