Home All Groups Group Topic Archive Search About

How to get the most new value registered...

Author
25 Nov 2005 10:56 PM
Myriam Cerda
I have this information,

header table
documento    fecha_entrada
100000        2005-11-25
100001        2005-11-25
100002        2005-11-26

detail table
documento    linea   numero_producto    fecha
100000        1        X                            2005-11-25 10:00
100000        2        Y                            2005-11-25 10:01
100001        1        M                           2005-11-25 11:02
100001        2        S                            2005-11-25 11:03
100001        3        R                            2005-11-25 11:04
100002        1        R                            2005-11-25 11:04

I want to make a SELECT query to obtein this
docuemnto    linea    numero_pro            fecha
fecha_entrada
100000        2        Y                            2005-11-25 10:01
2005-11-25
100001        3        R                            2005-11-25 11:04
2005-11-25
100002        1        R                            2005-11-25 11:04
2005-11-26


I only want the header and ONE row of the detail... the row has to be the
most recent captured..

Author
25 Nov 2005 11:13 PM
Hugo Kornelis
On Fri, 25 Nov 2005 16:56:18 -0600, Myriam Cerda wrote:

>I have this information,
(snip)
>I only want the header and ONE row of the detail... the row has to be the
>most recent captured..

Hi Myriam,

Try this:

SELECT     h.documento, d.linea, d.numero_producto,
           d.fecha, h.fecha_entrada
FROM       header AS h
INNER JOIN detail AS d
      ON   d.documento = h.documento
WHERE      d.fecha = (SELECT MAX(d2.fecha)
                      FROM   detail AS d2
                      WHERE  d2.documento = h.documento)

(untested - see www.aspfaq.com/5006 if you prefer a tested reply)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Author
26 Nov 2005 5:44 AM
Myriam Cerda
Hugo, Thanks  for your prompt response, It's works...



Show quote
"Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message
news:sf6fo1dhmjj05h6vqbrhs3htb80uffvbd3@4ax.com...
> On Fri, 25 Nov 2005 16:56:18 -0600, Myriam Cerda wrote:
>
>>I have this information,
> (snip)
>>I only want the header and ONE row of the detail... the row has to be the
>>most recent captured..
>
> Hi Myriam,
>
> Try this:
>
> SELECT     h.documento, d.linea, d.numero_producto,
>           d.fecha, h.fecha_entrada
> FROM       header AS h
> INNER JOIN detail AS d
>      ON   d.documento = h.documento
> WHERE      d.fecha = (SELECT MAX(d2.fecha)
>                      FROM   detail AS d2
>                      WHERE  d2.documento = h.documento)
>
> (untested - see www.aspfaq.com/5006 if you prefer a tested reply)
>
> Best, Hugo
> --
>
> (Remove _NO_ and _SPAM_ to get my e-mail address)

AddThis Social Bookmark Button