Jump to content
Fórum Script Brasil
  • 0

(Resolvido) Query - Agrupamento e ordenação


Fernandinha

Question

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 Fernandinha
Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652.1k
×
×
  • Create New...