Re: filtering groups

From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 08/11/04


Date: Wed, 11 Aug 2004 20:43:35 +0200

On Wed, 11 Aug 2004 09:19:55 -0700, novice wrote:

>I need to write a query that will return the most recent
>incident within a related group, but I want a value
>returned for each instance of that group even if there's
>only one event recorded for them.
>
>Ex: The last transaction made by each buyer regardless of
>how many transactions they have made.
>
>I know this should be simple, but I'm not getting it
>right. Thanks.

Hi Novice,

Without knowing your table, data, etc, I can't really help you much. I can
give you an example how a similar problem is solved in another database.

The following query will return details of sales; for each store, only the
last sale will be displayed (note that if there were several sales on the
same day, they are all shown).

USE pubs
go
SELECT sales.stor_id, sales.ord_num, sales.ord_date, sales.qty
FROM sales
INNER JOIN (SELECT stor_id, MAX(ord_date)
            FROM sales
            GROUP BY stor_id) AS last_sale (stor_id, ord_date)
      ON sales.stor_id = last_sale.stor_id
      AND sales.ord_date = last_sale.ord_date

Best, Hugo

-- 
(Remove _NO_ and _SPAM_ to get my e-mail address)


Relevant Pages

  • filtering groups
    ... I need to write a query that will return the most recent ... incident within a related group, ... The last transaction made by each buyer regardless of ...
    (microsoft.public.sqlserver.mseq)
  • Re: Handling notice of insufficient tax payment
    ... All of the understated income is related to securities sales during 2004. ... on the Sch D on the "correct" date and for the correct amount. ... There is one transaction they high lighted that clearly is missing from my ... put my tax liability at instead of the $153 I recieved as a refund ...
    (misc.taxes)
  • Re: Handling notice of insufficient tax payment
    ... All of the understated income is related to securities sales during 2004. ... on the Sch D on the "correct" date and for the correct amount. ... There is one transaction they high lighted that clearly is missing from ... put my tax liability at instead of the $153 I recieved as a refund ...
    (misc.taxes)
  • Re: OT: ebay rant
    ... All Sales Final is means the transaction is the end of the ... I guess lesson learned is that you should add No exchanges - No ... a bully consumer. ... I know what All Sales ...
    (alt.fashion)
  • Re: Shops accepting travellers cheques
    ... > because they are so rare many retail ... > salespeople may not know how or remember ... > how to process the transaction on their sales ...
    (rec.travel.usa-canada)