如何确保我的出发日期大于以下代码中的到达日期。
SELECT ArrivalDate, DATEADD(day, RAND(checksum(NEWID()))*1.5
* LengthOfStay.LengthofStay, ArrivalDate) AS DepartureDate
FROM Bookings, LengthOfStay
ORDER BY ArrivalDate
由于
韦恩
答案 0 :(得分:2)
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
答案 1 :(得分:1)
尝试此查询
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