+----------+-----------+----------+
| user_id | order_id | cal_date |
+----------+-----------+----------+
| 10000060 | 304452419 | 9/8/16 |
| 10000060 | 304452419 | 9/8/16 |
| 10000060 | 305742547 | 9/8/16 |
| 10000060 | 306345571 | 9/3/16 |
| 10000214 | 300179313 | 8/21/16 |
| 10000214 | 321227135 | 1/18/17 |
| 10000214 | 321227135 | 1/18/17 |
| 10000214 | 321227135 | 1/18/17 |
| 10000322 | 281254713 | 2/7/16 |
| 10000322 | 281254713 | 2/7/16 |
| 10000322 | 281254713 | 2/7/16 |
| 10000322 | 307282405 | 9/19/16 |
| 10000322 | 307282405 | 9/19/16 |
| 10000322 | 307282405 | 9/19/16 |
| 10000322 | 307282405 | 9/19/16 |
| 10001708 | 273598247 | 8/18/16 |
| 10001708 | 293004133 | 8/18/16 |
| 10001708 | 293004133 | 8/18/16 |
| 10001708 | 293004133 | 8/18/16 |
| 100018 | 304414889 | 9/4/16 |
| 1000193 | 322124963 | 1/23/17 |
| 10001964 | 292928587 | 5/18/16 |
| 10001964 | 293084327 | 5/20/16 |
+----------+-----------+----------+
我的目标是输出一个具有UNIQUE user_ids的表,该user_id的最新cal_date和最高order_id。
让我失望的部分是user_id在同一个cal_date上可以有不同的order_id(查看user_id 10000060)。
我以低效的方式完成了这项工作(我使用了几个子查询和排名函数)。似乎会有更好的方法来做到这一点?有什么想法吗?
答案 0 :(得分:0)
试试这个:
select b.user_id, max(b.orderid) maxorderid, b.cal_date from(
select user_id, max(cal_date)cal_date
from yourtable a
group by user_id)a
join yourtable b on a.userid=b.user_id and a.cal_date=b.cal_date
group by b.user_id, b.cal_date
答案 1 :(得分:0)
使用Row_number并对数据进行排序以获得您想要的行:
select user_id,
order_id,
cal_date
from
(
select user_id,
order_id,
cal_date,
row_number()
over (partition by user_id
order by cal_date desc, order_id desc) as rn
from mytable
) as dt
where rn = 1