如何确保我的出发日期大于以下代码中的到达日期。
SELECT ArrivalDate, DATEADD(day, RAND(checksum(NEWID()))*1.5
* LengthOfStay.LengthofStay, ArrivalDate) AS DepartureDate
FROM Bookings, LengthOfStay
ORDER BY ArrivalDate
谢谢
韦恩
如何确保我的出发日期大于以下代码中的到达日期。
SELECT ArrivalDate, DATEADD(day, RAND(checksum(NEWID()))*1.5
* LengthOfStay.LengthofStay, ArrivalDate) AS DepartureDate
FROM Bookings, LengthOfStay
ORDER BY ArrivalDate
谢谢
韦恩
DATEADD
取一个整数......并且从随机化返回的任何十进制值都将被截断。因此,您可能只是添加0
到ArrivalDate
,导致两个日期相等。
您可以通过1
在随机化中添加最少的来解决此问题:
SELECT ArrivalDate, DATEADD(day, 1 + RAND(checksum(NEWID()))*1.5
* LengthOfStay.LengthofStay, ArrivalDate) AS DepartureDate
FROM Bookings, LengthOfStay
ORDER BY ArrivalDate
试试这个查询
SELECT * FROM
(
SELECT
ArrivalDate,
DATEADD(day, RAND(checksum(NEWID()))*1.5 * LengthOfStay.LengthofStay, ArrivalDate) AS DepartureDate
FROM
Bookings, LengthOfStay
) a
WHERE a.DepartureDate > a.ArrivalDate
ORDER BY a.ArrivalDate