Quantcast
Channel: IIUG Forum: IDS Forum
Viewing all articles
Browse latest Browse all 9843

Re: Retrieve the Max Date and 2nd Max Date per ID

$
0
0
John,

Try this one:

select a.stn_sld_cus_id, Max(a.stn_inv_dt) max_invoice_date
from sahstn_rec a, (

select a.stn_sld_cus_id, Max(a.stn_inv_dt) max_invoice_date

from sahstn_rec c

where stn_ord_pfx = 'SO' and c.stn_inv_dt < '04/01/2015'

group by stn_sld_cus_id;
) b
where stn_ord_pfx = 'SO' and a.stn_inv_dt < '04/01/2015'

and a.stn_sld_cus_id = b.stn_sld_cus_id

and a.stn_inv_dt < b.max_invoice_date
group by stn_sld_cus_id;

Art

Art S. Kagel, President and Principal Consultant
ASK Database Management
www.askdbmgt.com

Blog: http://informix-myview.blogspot.com/

Disclaimer: Please keep in mind that my own opinions are my own opinions
and do not reflect on the IIUG, nor any other organization with which I am
associated either explicitly, implicitly, or by inference. Neither do
those opinions reflect those of other individuals affiliated with any
entity with which I am affiliated nor those of the entities themselves.

On Thu, Apr 9, 2015 at 4:32 PM, JOHN KRUPA <jkrupa@turretsteel.com> wrote:

> I have the need to retrieve the Max(invoice date) per customer. I also
> would
> like to retrieve the second Max(invoice date) per customer. Following is
> the
> SQL for the Max(invoice date) per customer...works well.
>
> select a.stn_sld_cus_id, Max(a.stn_inv_dt) max_invoice_date
>
> from sahstn_rec a
>
> where stn_ord_pfx = 'SO' and a.stn_inv_dt < '04/01/2015'
>
> group by stn_sld_cus_id;
>
> The next SQL is what I have come up with for the second Max(invoice date)
> per
> customer. This is not working consistently.
>
> select a.stn_sld_cus_id, max(a.stn_inv_dt) max_inv_date
>
> from sahstn_rec a
>
> where stn_inv_dt not in
>
> (select max(b.stn_inv_dt)
>
> from sahstn_rec b
>
> where stn_ord_pfx = 'SO' and
>
> a.stn_sld_cus_id = b.stn_sld_cus_id and
>
> a.stn_inv_dt < '04/01/2015'
>
> group by stn_sld_cus_id)
>
> and a.stn_inv_dt < '04/01/2015'
>
> group by a.stn_sld_cus_id;
>
> Any ideas?
>
> Thank you
> John
>
>
>
>

>
>
>

--089e0111b7447c303b051350a943




*******************************************************************************

To post a response via email (IIUG members only):

1. Address it to ids@iiug.org
2. Include the bracketed message number in the subject line: [34949]

*******************************************************************************

Viewing all articles
Browse latest Browse all 9843

Trending Articles