Un grupo por reto

Digamos que tengo esta mesa MyTbl

Record    Id_try   Id        Type IsOk DateOk
1         1        MYDB00125  A     0   NULL
2         1        MYDB00125  B     1   2012-07-19 20:10:05.000
3         1        MYDB00125  A     0   2012-07-25 14:10:05.000
4         2        MYDB00125  A     0   2012-07-19 22:10:05.000
5         1        MYDB00254  B     0   2012-07-19 22:10:05.000
6         1        MYDB00254  A     0   NULL
7         3        MYDB00125  A     1   2012-07-19 22:15:05.000
8         3        MYDB00125  B     1   2012-07-19 22:42:53.000
9         1        MYDB00323  A     1   2012-07-22 00:15:05.00 0
10        1        MYDB00323  C     0   NULL

And I want a group by that brings me for each Id y Type my last "Id_Try Registro".

SELECT Id, MAX(Id_Try), MyTbl.Type, IsOK, MAX(DateOk) from MyTbl
GROUP BY Id, MyTbl.Type, IsOK

Won't do, because It'll bring me the last Id_Try Y DE the last date (Date of record 3 in the example). And I don't care if its the last date or not, I need the date of the last Id_Try.

Is this only solved by a subselect? or a having clause could do?

This is the result expected:

Record    Id_try   Id        Type IsOk DateOk
5         1        MYDB00254  B     0   2012-07-19 22:10:05.000
6         1        MYDB00254  A     0   NULL
7         3        MYDB00125  A     1   2012-07-19 22:15:05.000
8         3        MYDB00125  B     1   2012-07-19 22:42:53.000
9         1        MYDB00323  A     1   2012-07-22 00:15:05.00 0
10        1        MYDB00323  B     0   NULL

preguntado el 06 de septiembre de 12 a las 17:09

Do you really only want ID and Type or do you want all columns (as your result would suggest)? -

I want all fields, but I don't care if the date that this will show me isn't the Max date. -

3 Respuestas

I think you will need to break this into two pieces:

with maxIDTry as
(
   SELECT MAX(Id_try) as maxId, ID
   FROM MyTable
   GROUP BY ID
)
SELECT * FROM MyTable as mt
INNER JOIN maxIDTry as max
ON mt.id_try = max.maxId AND mt.id = max.id

Respondido el 06 de Septiembre de 12 a las 17:09

I'll try this and come back with the results! - apacay

I didn't know the With sentence, so this is double useful. Thanks! - apacay

Glad I could help. Look up Common table expressions (or cte if you are in the know) for an explanation of what with does - abe miessler

Creo que quieres esto:

select * FROM 
 (
   select *, row_number() over (partition by id,type order by Id_try desc) as position from mytbl
) foo 
where position = 1
order by record

http://www.sqlfiddle.com/#!3/95742/5

Your sample result set lists

9         1        MYDB00323  A     1   2012-07-22 00:15:05.00 0
10        1        MYDB00323  A     0   NULL

But that doesn't make sense since you're saying the ID y the Id_try have the same value. I assume you meant for Id_try to be 2 maybe? Otherwise I think my results match up.

Respondido el 06 de Septiembre de 12 a las 17:09

Ok, there's corrected, the expected results however, were ok. I don't seem to get your explanation, what is position? - apacay

Espero que esto ayude.

SELECT  A.Record, A.Id_try, A.Id, A.Type, A.IsOk, A.DateOk
FROM    MyTbl A INNER JOIN (
        SELECT MAX(Id_Try) Id_Try, Id, B1.Type 
        from MyTbl B1
        GROUP BY Id, B1.Type) AS B 
ON A.Id_Try = B.Id_Try AND A.Id = B.Id AND A.Type = B.Type
ORDER BY A.RECORD

Respondido el 07 de Septiembre de 12 a las 09:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.