我在下面的查询中尝试将所有行放在最后一个Action =“UNLOCKED”之后,但看起来在子查询中不允许使用ORDER BY。
SELECT *
FROM TABLE
WHERE id >= (SELECT MAX(id)
FROM TABLE
WHERE ACTION='UNLOCKED' AND action_id=123
ORDER BY CREATE_DATE DESC);
示例数据
Id action_id Action ... CREATE_DATE
1 123 ADD 03/18/2018
2 123 Unlocked 03/19/2018
3 123 Updated1 03/19/2018
4 123 Updated2 03/19/2018
5 123 Unlocked 03/20/2018
6 123 Updated3 03/20/2018
7 123 Updated4 03/20/2018
输出应为id为5,6,7的行。我应该用什么来获得这个输出
答案 0 :(得分:1)
您可以在subselect上使用内部联接以获取max create_date
select * from TABLE
INNER JOIN (
select max(CREATE_DATE) max_date
from TABLE
where Action = 'Unlocked' ) T on t.max_date = TABLE.CREATE_DATE
答案 1 :(得分:1)
您无需订购内部查询,因为它只返回一个值。您可以按照以下方式执行此操作
df <- structure(list(`0-5` = 700L, `5-10` = 1000L, `10-15` = 1400L, `15-20` = 1700L,
`20-25` = 1900L, `25-30` = 1500L, `30-35` = 1000L, `35-40` = 300L,
`40-45` = 50L, `45-50` = 1L), .Names = c("0-5", "5-10",
"10-15", "15-20", "20-25", "25-30", "30-35", "35-40", "40-45",
"45-50"), class = "data.frame", row.names = 1L)
df
# 0-5 5-10 10-15 15-20 20-25 25-30 30-35 35-40 40-45 45-50
# 1 700 1000 1400 1700 1900 1500 1000 300 50 1