0

我有四张桌子。

  1. 司机考勤
  2. 司机信息
  3. 车辆详细信息
  4. 预订详情

我想根据某些条件写一个查询。

每当驱动程序绑定时,它将根据分配给驱动程序的车辆进行绑定。而且只有那个司机会出现在不是每周休息的下拉列表中,他们不在我将发送的特定日期休假。

  • tblDriverInformation列是:1)id 2)每周休息

  • tblDriverAttendance列是---> 1) Driverid 2) Leave From Date 3) Leave To Date

  • Driver Id在上表中很常见。

  • tblVehicle列是---> 1)分配给>驱动程序(在上表中很常见)2)id

  • tblBooking列是---> 1)车辆 ID 2)从日期开始时间 3)结束日期和时间tblVehicletblBooking在两个表中,id 和车辆 ID 是通用键)

我已经准备了一个查询,但它没有给出正确的答案,而是因为我不了解如何输入tblDriverInformationtblDriverAttendance条件。

我的查询是:

 select 
     A.id, 
     (C.FName + '-' + C.LName) as Car_Driver 
 from 
     tblVehicle A 
 inner join  
     tblDriverInformation C on A.CarId = C.id  
 where
     A.id not in (SELECT VehicleId  
                  FROM tblBooking 
                  join tblVehicle on C.id = A.AssignedDriver 
                  WHERE A.id = @vechileid 
                   and Convert(datetime,@FromDateWithStartTime)  --start date  
                       BETWEEN FromDateWithStartTime AND ToDateWithEndTime  
                    OR Convert(datetime,@ToDateWithEndTime) --endDate  
                       BETWEEN FromDateWithStartTime AND ToDateWithEndTime  
                    or FromDateWithStartTime 
                       BETWEEN Convert(datetime,@FromDateWithStartTime) -- startdate  
                               AND Convert(datetime,@ToDateWithEndTime)  -- enddate  
                    or ToDateWithEndTime 
                       BETWEEN Convert(datetime,@FromDateWithStartTime) -- start date  
                       AND Convert(datetime,@ToDateWithEndTime)   -- enddate
4

1 回答 1

0

这可能会对您有所帮助,下面的查询找出与它们相关联的车辆和驾驶员,条件如下。

1- 司机不是每周休息。

2- 在选定日期可用的车辆(即尚未预订)

3- 司机在选定日期可用(即尚未预订)

SELECT D.ID, V.id
    FROM tblDriverInformation D
    INNER JOIN tblVehicle ON V.DRIVER_ID = D.DRIVER_ID
    INNER JOIN tblDriverAttendance tda ON  tda.DRIVER_ID = D.DRIVER_ID 
    INNER JOIN tblBooking tbl ON V.ID = tbl.VECHILCE_ID 
    WHERE D.WEEKLY_OFF= FALSE -- Driver Not Absent
    AND (@DRIVER_FROM_DATE  NOT BETWEEN (tda.FROMDATE,tda.TODATE) OR @DRIVER_TO_DATE NOT BETWEEN (tda.FROMDATE,tda.TODATE)) -- Driver not absent in these dates
    AND (@VEHILE_FROM_DATE  NOT BETWEEN (tbl.FROMDATE,tbl.TODATE) OR @VEHILE_TO_DATE NOT BETWEEN (tbl.FROMDATE,tbl.TODATE)) -- vehicle not booked in these dates
    -- @DRIVER_FROM_DATE , @DRIVER_TO_DATE, @VEHILE_FROM_DATE, @VEHILE_TO_DATE( user input selected date )
于 2013-08-29T06:20:27.580 回答