SELECT [CUSTOMER_ID], [PROPERTY_NAME], [LOCATION_CODE], [RESERVATION_DATE]
FROM a
从上表中,我必须property_Name基于上一个选择每个RESERVATION_DATE
```js
[CUSTOMER_ID] [PROPERTY_NAME] [LOCATION_CODE] [RESERVATION_DATE]
--------------------------------------------------------------------
121 b B5 2019-12-19
1256 c c5 2019-12-20
1212 b B5 2019-12-22
1236 a A5 2019-12-18
输出量
```js
[CUSTOMER_ID] [PROPERTY_NAME] [LOCATION_CODE] [RESERVATION_DATE]
-------------------------------------------------------------------
1212 b B5 2019-12-22
1256 c c5 2019-12-20
1236 a A5 2019-12-18
使用不存在:
SELECT t.[CUSTOMER_ID], t.[PROPERTY_NAME], t.[LOCATION_CODE], t.[RESERVATION_DATE]
FROM tablename AS t
WHERE NOT EXISTS (
SELECT 1 FROM tablename
WHERE [PROPERTY_NAME] = t.[PROPERTY_NAME] AND [RESERVATION_DATE] > t.[RESERVATION_DATE]
)
或在WHERE子句中使用子查询返回最新日期:
SELECT t.[CUSTOMER_ID], t.[PROPERTY_NAME], t.[LOCATION_CODE], t.[RESERVATION_DATE]
FROM tablename AS t
WHERE t.[RESERVATION_DATE] = (
SELECT MAX([RESERVATION_DATE]) FROM tablename
WHERE [PROPERTY_NAME] = t.[PROPERTY_NAME]
)
参见演示。 结果:
> CUSTOMER_ID | PROPERTY_NAME | LOCATION_CODE | RESERVATION_DATE
> ----------: | :------------ | :------------ | :------------------
> 1256 | c | c5 | 20/12/2019
> 1212 | b | B5 | 22/12/2019
> 1236 | a | A5 | 18/12/2019
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。