简体   繁体   中英

select n-th row of certain key SQL

How can I select specific number of date (let it be third) for all CustomerID in a row for example I have DB looks like

+---------+------------+------------+------------+-----------+
| OrderID | CustomerID | EmployeeID | OrderDate  | ShipperID |
+---------+------------+------------+------------+-----------+
|   10308 |          2 |          7 | 1996-09-18 |         3 |
|   10365 |          3 |          3 | 1996-11-27 |         2 |
|   10355 |          4 |          6 | 1996-11-15 |         1 |
|   10383 |          4 |          8 | 1996-12-16 |         3 |
|   10278 |          5 |          8 | 1996-08-12 |         2 |
|   10280 |          5 |          2 | 1996-08-14 |         1 |
|   10384 |          5 |          3 | 1996-12-16 |         3 |
|   10265 |          7 |          2 | 1996-07-25 |         1 |
|   10297 |          7 |          5 | 1996-09-04 |         2 |
|   10360 |          7 |          4 | 1996-11-22 |         3 |
|   10436 |          7 |          3 | 1997-02-05 |         2 |
+---------+------------+------------+------------+-----------+

and as the output we have to get

╔══╦════════════╦══╦════════════╦══╗
║  ║ CustomerID ║  ║ OrderDate  ║  ║
╠══╬════════════╬══╬════════════╬══╣
║  ║          5 ║  ║ 1996-12-16 ║  ║
║  ║          7 ║  ║ 1996-11-22 ║  ║
╚══╩════════════╩══╩════════════╩══╝

something like this

I use MySQL

SELECT 
 CustomerIds.CustomerID
 ,(SELECT OrderDate FROM Table1
   WHERE Table1.CustomerID = CustomerIds.CustomerID
   ORDER BY OrderDate ASC
   LIMIT 1 OFFSET 2) AS OrderDate
FROM
 (SELECT CustomerID 
  FROM Table1
  GROUP BY CustomerID
  HAVING COUNT(*) >= 3) AS CustomerIds;

SQLFiddle

尝试这个,

select customerid , orderdate from table where GROUP BY customerid

If you want all the columns, then probably the fastest way is to use variables:

select t.*
from (select t.*,
             (@rn := if(@c = CustomerId, @rn + 1,
                        if(@c := CustomerId, 1, 1)
                       )
             ) as rn
      from table1 t cross join
           (select @c := 0, @rn := 0) params
      order by customerId, OrderDate
     ) t
where rn = 3;

You can simulate row_number() in MySQL using variables. The subquery below calculates row_number() over (partition by CustomerID order by OrderDate) . Once you have the row number, it's easy to pick the 3rd:

SELECT  *
FROM    (
        SELECT  @rn := if(@prev_cust = CustomerID, @rn + 1, 1) as rn
        ,       @prev_cust := CustomerID
        ,       Table1.*
        FROM    Table1
        CROSS JOIN
                (
                SELECT  @prev_cust :=0
                ,       @rn := 0
                ) AS InitAlias
        ORDER BY
                CustomerID
        ,       OrderDate
        ) as SubQueryAlias
WHERE   rn = 3

Example at SQL Fiddle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM