SQL:如何选择具有最年轻日期或时间戳的行

huangapple go评论69阅读模式
英文:

SQL: How to select Row that has youngest Date OR timestamp

问题

我有一个包含三列的表格,这是一个带有内连接的选择的结果:

SELECT createTimestamp, paymentDate, buyDate
FROM ARC_PAYMENTS 
INNER JOIN PERSON_IN_PAYMENT ON ARC_PAYMENTS.PERSON_ID=PERSON_IN_PAYAMENT.ID
WHERE PERSON_IN_PAYMENT.COMPANY_ID='1234567';

现在,我需要获取所有行的三列中的最新/最年轻/最高值。因此,结果应该只是一个日期。这可能会有问题,因为createTimestamp列是一个时间戳,而另外两列是日期。

在Oracle数据库中,有没有一种方法可以做到这一点(性能非常重要)?可以在另一个选择中使用我的第一个选择的结果吗?

谢谢

英文:

I have a table with three columns, that ist result of a Select with an inner join:

SELECT createTimestamp, paymentDate, buyDate
FROM ARC_PAYMENTS 
INNER JOIN PERSON_IN_PAYMENT ON ARC_PAYMENTS.PERSON_ID=PERSON_IN_PAYAMENT.ID
WHERE PERSON_IN_PAYMENT.COMPANY_ID='1234567';

Now, I need to get the latest/youngest/highest value of the three columns of all rows. So the result should be just one Date.
That might be problematic because the createTimestamp column is a timestamp, the other two columns are date.

Is there any way to do that (performance is very important) in a oracle database? To use the result of my first SELECT in another select?

Thank you

答案1

得分: 1

查看GREATEST函数。

SELECT GREATEST(CAST(createTimestamp AS DATE), paymentDate, buyDate) 
FROM ARC_PAYMENTS 
INNER JOIN PERSON_IN_PAYMENT ON ARC_PAYMENTS.PERSON_ID=PERSON_IN_PAYMENT.ID
WHERE PERSON_IN_PAYMENT.COMPANY_ID='1234567';
英文:

Check out the GREATEST function.

SELECT GREATEST(CAST(createTimestamp AS DATE), paymentDate, buyDate) 
FROM ARC_PAYMENTS 
INNER JOIN PERSON_IN_PAYMENT ON ARC_PAYMENTS.PERSON_ID=PERSON_IN_PAYAMENT.ID
WHERE PERSON_IN_PAYMENT.COMPANY_ID='1234567';

huangapple
  • 本文由 发表于 2023年4月6日 19:28:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75948989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定