转换要在Access数据库

时间:2016-12-01 07:04:59

标签: sql sql-server delphi ms-access

我发现此查询可以很好地使用SQL服务器。 所以我想把它移植到Access数据库。字段是相同的。

Rate_Start_Date = DateTime
Rate_End_Date = DateTime
Room_Type = char
Rate = Currency
Price_List_Code = Integer

然而,我不知道sql语法。 有人可以帮我重写这个,所以它适用于Access数据库吗?

代码:

    use MYDATABASE
DECLARE @StartDate  DATETIME,
        @EndDate    DATETIME,
        @RoomType   VARCHAR(6),
        @PriceListCode    INT
;

 SELECT @StartDate  = :a2,
        @EndDate    = :a3,
        @RoomType   = :a1,
        @PriceListCode  = :a4
;
WITH 
cteStayDates AS
( 
 SELECT RoomType  = Room_Type,
        StartDate = CASE WHEN Rate_Start_Date < @StartDate THEN @StartDate ELSE Rate_Start_Date END,
        EndDate   = CASE WHEN Rate_End_Date   > @EndDate   THEN @EndDate   ELSE Rate_End_Date   END,
        Rate
   FROM dbo.Room_Rates
  WHERE @RoomType  = Room_Type
    AND @StartDate < Rate_End_Date
    AND @EndDate  >= Rate_Start_Date
    AND @PriceListCode = PRICE_LIST_CODE
) 
 SELECT RoomType, StartDate, EndDate, Rate, 
        Days = DATEDIFF(dd,StartDate,EndDate)
             + CASE WHEN EndDate = @EndDate THEN 0 ELSE 0 END
   FROM cteStayDates
  ORDER BY StartDate
;

1 个答案:

答案 0 :(得分:1)

在Access SQL中应该是这样的:

PARAMETERS parmStartDate DATETIME
    ,parmEndDate DATETIME
    ,parmPriceList Short
    ,parmRoomType TEXT (6);

SELECT RoomType
    ,StartDate
    ,EndDate
    ,Price
    ,DATEDIFF("d", StartDate, EndDate) AS Days
FROM (
    SELECT Room_Type AS RoomType
        ,iif(Rate_Start_Date < parmStartDate, parmStartDate, Rate_Start_Date) AS StartDate
        ,iif(Rate_End_Date > parmEndDate, parmEndDate, Rate_End_Date) AS EndDate
        ,Price
    FROM dbo.Room_Rates
    WHERE parmRoomType = Room_Type
        AND parmPriceList = Price_List
        AND parmStartDate < Rate_End_Date
        AND parmEndDate >= Rate_Start_Date
    )
ORDER BY StartDate;