Firebird Query- Return first row each group

前端 未结 3 1050
不知归路
不知归路 2021-01-22 02:03

In a firebird database with a table \"Sales\", I need to select the first sale of all customers. See below a sample that show the table and desired result of query.



        
3条回答
  •  醉话见心
    2021-01-22 02:43

    Search for the sales with no earlier sales:

    SELECT S1.*
    FROM SALES S1
    LEFT JOIN SALES S2 ON S2.CUSTOMERID = S1.CUSTOMERID AND S2.DTHRSALE < S1.DTHRSALE
    WHERE S2.ID IS NULL
    

    Define an index over (customerid, dthrsale) to make it fast.

提交回复
热议问题