简体   繁体   中英

Skipping row for each unique column value

I have a table from which I would like to extract all of the column values for all rows. However, the query needs to be able to skip the first entry for each unique value of id_customer . It can be assumed that there will always be at least two rows containing the same id_customer .

I've compiled some sample data which can be found here: http://sqlfiddle.com/#!9/c85b73/1

The results I would like to achieve are something like this:

id_customer | id_cart | date
----------- | ------- | -------------------
1           | 102     | 2017-11-12 12:41:16
2           | 104     | 2015-09-04 17:23:54
2           | 105     | 2014-06-05 02:43:42
3           | 107     | 2011-12-01 11:32:21

Please let me know if any more information/better explanation is required, I expect it's quiet a niche solution.

One method is:

select c.*
from carts c
where c.date > (select min(c2.date) from carts c2 where c2.id_customer = c.id_customer);

If your data is large, you want an index on carts(id_customer, date) .

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