Pregunta

Tengo algo parecido a la siguiente estructura de datos:

Category    StartDateTime       EndDateTime
===============================================
1           12/1/2009 12:00     12/1/2009 12:12
1           12/1/2009 04:00     12/1/2009 04:20
2           12/2/2009 10:15     12/2/2009 10:22
2           12/2/2009 11:00     12/2/2009 11:01

Quiero que el StartDateTime min y max EndDateTime para cada categoría. De esta manera:

Category    MinStartDateTime    MaxEndDateTime
===============================================
1           12/1/2009 12:00     12/1/2009 04:20
2           12/2/2009 10:15     12/2/2009 11:01

no parece funcionar Usando min y max con un grupo por categoría:

select
    Category,
    min(StartDateTime) [MinStartDateTime],
    max(EndDateTime) [MaxDateTime]
from
    MyTable
group by
    Category
order by
    Category,
    StartDateTime,
    EndDateTime

También probamos dos uniones interiores en una sub-consulta para cada declaración min y max, sin embargo, parece estar excluyendo algunos registros:

select distinct
    T1.Category,
    T1.StartDateTime [MinStartDateTime],
    T1.EndDateTime [MaxEndDateTime]

from
    MyTable T1

inner join
    (select
        Category,
        min(StartDateTime) [MinStartDateTime]
     from
        MyTable
     group by
        Category) T2
on T2.Category = T1.Category and T2.MinStartDateTime = T1.StartDateTime

inner join
    (select
        Category,
        max(EndDateTime) [MaxEndDateTime]
     from
        MyTable
     group by
        Category) T3
on T3.Category = T1.Category and T3.MaxEndDateTime = T1.EndDateTime

order by
    T1.Category,
    T1.encodeStartDateTime,
    T1.encodeEndDateTime

¿Alguna idea? La base de datos Sybase ASE es que debe ser compatible con SQL-92.

¿Fue útil?

Solución

Su primera solución parece correcto a excepción de la cláusula ORDER BY; Proveedores:

select
    Category,
    min(StartDateTime) [MinStartDateTime],
    max(EndDateTime) [MaxDateTime]
from MyTable
group by
    Category
order by
    Category,
    MinStartDateTime,
    MaxDateTime
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top