1

如何确保我的出发日期大于以下代码中的到达日期。

SELECT ArrivalDate, DATEADD(day, RAND(checksum(NEWID()))*1.5
* LengthOfStay.LengthofStay, ArrivalDate) AS DepartureDate    
FROM Bookings, LengthOfStay
ORDER BY ArrivalDate

谢谢

韦恩

4

2 回答 2

2

DATEADD取一个整数......并且从随机化返回的任何十进制值都将被截断。因此,您可能只是添加0ArrivalDate,导致两个日期相等。

您可以通过1在随机化中添加最少的来解决此问题:

SELECT ArrivalDate, DATEADD(day, 1 + RAND(checksum(NEWID()))*1.5
* LengthOfStay.LengthofStay, ArrivalDate) AS DepartureDate    
FROM Bookings, LengthOfStay
ORDER BY ArrivalDate
于 2013-02-19T16:39:28.450 回答
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
于 2013-02-19T16:39:11.317 回答