Отменить выбор «По первому ряду» - PullRequest
0 голосов
/ 24 мая 2018

Я выполняю приведенный ниже запрос, но в последнем подзапросе (пробеге) получаю следующую ошибку из-за ORDER BY: «ORA-00907: отсутствует правая скобка», если я удаляю ORDER BY, он работаетхорошо.

SELECT /* DATE OF THE ROUTE */
       {GPS}.[DateTime],           
       /* ROUTE DESCRIPTION */                                     
       {Route}.[Description],
       /* NAME OF THE DRIVER */  
       {Driver}.[Name],                                                 
       /* VEHICLE LICENSE PLATE */
       {Vehicle}.[Registration],
       /* QUANTITY OF STOPS */
       (SELECT COUNT({RouteStop}.[RouteId])
          FROM {RouteStop}
         WHERE {RouteStop}.[RouteId] = {GPS}.[RouteId]) AS StopCount,
       /* AMOUNT OF FUEL */
       (SELECT SUM(FUEL.[Value])
          FROM {GPS} FUEL     
         WHERE {GPS}.[RouteId] = FUEL.[RouteId]
           AND FUEL.[EventTypeId] = 23) FuelAmount, /* Event Fuel */
       /* ROUTE STARTDATETIME */
       {GPS}.[DateTime] AS ROUTESTARTDATETIME,
       /* ROUTE ENDDATETIME */
       (SELECT ROUTEENDDATETIME.[DateTime]
          FROM {GPS} ROUTEENDDATETIME
         WHERE {GPS}.[RouteId] = ROUTEENDDATETIME.[RouteId]
           AND ROUTEENDDATETIME.[EventTypeId] = 5 /* Event Route Completed */
           AND ROWNUM = 1) AS ROUTEEND, 
       /* INITIAL MILEAGE */                         
       (SELECT MILEAGEBEGIN.[Value]
          FROM {GPS} MILEAGEBEGIN 
         WHERE {GPS}.[RouteId] = MILEAGEBEGIN.[RouteId] 
           AND MILEAGEBEGIN.[EventTypeId] = 21 /* Event Mileage */
           AND ROWNUM = 1
         ORDER BY MILEAGEBEGIN.[DateTime]
        ) AS INITIALMILEAGE

FROM {GPS}

INNER JOIN {Route}
    ON {GPS}.[RouteId] = {Route}.[Id]

 INNER JOIN {Driver}
    ON {GPS}.[DriverId] = {Driver}.[Id]

 INNER JOIN {Availability}
    ON {Driver}.[Id] = {Availability}.[DriverId]

 INNER JOIN {Vehicle}
    ON {Availability}.[VehicleId] = {Vehicle}.[Id]

 WHERE {GPS}.[EventTypeId] = 3 /* Event RouteStarted */

Я пытался следующим образом, но я получаю эту ошибку: "ORA-00936: отсутствует выражение".

SELECT /* DATE OF THE ROUTE */
       {GPS}.[DateTime],           

       /* INITIAL MILEAGE */                         
       SELECT TEST,'more test' FROM (SELECT MILEAGEBEGIN.[Value] AS TEST
                                       FROM {GPS} MILEAGEBEGIN 
                                      WHERE {GPS}.[RouteId] = MILEAGEBEGIN.[RouteId] 
                                        AND MILEAGEBEGIN.[EventTypeId] = 21 /* Event Mileage */
                                      ORDER BY MILEAGEBEGIN.[DateTime] ASC
                                    ) 
                              WHERE ROWNUM = 1 AS INITIALMILEAGE

  FROM {GPS}

 WHERE {GPS}.[EventTypeId] = 3 /* Event RouteStarted */

Ответы [ 2 ]

0 голосов
/ 24 мая 2018

Правильный запрос

SELECT /* ROUTEID */
       ROUTES.[RouteId] AS ROUTEID,       
       /* ROUTE STARTDATETIME */
       ROUTES.[DateTime] AS ROUTESTARTDATETIME,     
       /* ROUTE DESCRIPTION */                                     
       {Route}.[Description],
       /* NAME OF THE DRIVER */  
       {Driver}.[Name],         
       /* VEHICLE LICENSE PLATE */
       {Vehicle}.[Registration],
       /* QUANTITY OF STOPS */
       (SELECT COUNT({RouteStop}.[RouteId])
          FROM {RouteStop}
         WHERE {RouteStop}.[RouteId] = ROUTEID) AS STOPCOUNT,
       /* AMOUNT OF FUEL */
       (SELECT SUM(FUEL.[Value])
          FROM {GPS} FUEL     
         WHERE ROUTES.[AvailabilityId] = FUEL.[AvailabilityId]
           AND TRUNC(ROUTES.[DateTime]) = TRUNC(FUEL.[DateTime])
           AND FUEL.[EventTypeId] = @FuelEventTypeId) AS FUELAMOUNT,
       /* ROUTE ENDDATETIME */
       (SELECT ROUTEENDDATETIME.[DateTime]
          FROM {GPS} ROUTEENDDATETIME
         WHERE ROUTEID = ROUTEENDDATETIME.[RouteId]
           AND ROUTEENDDATETIME.[EventTypeId] = @RouteCompletedEventTypeId 
           AND ROWNUM = 1) AS ROUTEEND, 
       /* INITIAL MILEAGE */                         
       (SELECT INITIALMILEAGE 
          FROM (SELECT MILEAGEBEGIN.[Value] AS INITIALMILEAGE
                  FROM {GPS} MILEAGEBEGIN 
                 WHERE ROUTES.[AvailabilityId] = MILEAGEBEGIN.[AvailabilityId]
                   AND TRUNC(ROUTES.[DateTime]) = TRUNC(MILEAGEBEGIN.[DateTime])
                   AND MILEAGEBEGIN.[EventTypeId] = @MileageEventTypeId 
                 ORDER BY MILEAGEBEGIN.[DateTime] ASC
               ) 
         WHERE ROWNUM = 1),
       /* FINAL MILEAGE */                         
       (SELECT FINALMILEAGE 
          FROM (SELECT MILEAGEEND.[Value] AS FINALMILEAGE 
                  FROM {GPS} MILEAGEEND 
                 WHERE ROUTES.[AvailabilityId] = MILEAGEEND.[AvailabilityId]
                   AND TRUNC(ROUTES.[DateTime]) = TRUNC(MILEAGEEND.[DateTime])
                   AND MILEAGEEND.[EventTypeId] = @MileageEventTypeId
                 ORDER BY MILEAGEEND.[DateTime] DESC
               ) 
         WHERE ROWNUM = 1)

FROM {GPS} ROUTES

INNER JOIN {Route}
   ON ROUTES.[RouteId] = {Route}.[Id]

INNER JOIN {Availability}
   ON ROUTES.[AvailabilityID] = {Availability}.[Id]

INNER JOIN {Driver}
    ON {Availability}.[DriverId] = {Driver}.[Id]

INNER JOIN {Vehicle}
   ON {Availability}.[VehicleId] = {Vehicle}.[Id]

 WHERE ROUTES.[EventTypeId] = @RouteStartedEventTypeId 
0 голосов
/ 24 мая 2018

Снять скобки,

SELECT /* DATE OF THE ROUTE */
       GPS.DateTime,           
       /* ROUTE DESCRIPTION */                                     
       Route.Description,
       /* NAME OF THE DRIVER */  
       Driver.Name,                                                 
       /* VEHICLE LICENSE PLATE */
       Vehicle.Registration,
       /* QUANTITY OF STOPS */
       (SELECT COUNT(RouteStop.RouteId)
          FROM RouteStop
         WHERE RouteStop.RouteId = GPS.RouteId) AS StopCount,
       /* AMOUNT OF FUEL */
       (SELECT SUM(FUEL.Value)
          FROM GPS FUEL     
         WHERE GPS.RouteId = FUEL.RouteId
           AND FUEL.EventTypeId = 23) FuelAmount, /* Event Fuel */
       /* ROUTE STARTDATETIME */
       GPS.DateTime AS ROUTESTARTDATETIME,
       /* ROUTE ENDDATETIME */
       (SELECT ROUTEENDDATETIME.DateTime
          FROM GPS ROUTEENDDATETIME
         WHERE GPS.RouteId = ROUTEENDDATETIME.RouteId
           AND ROUTEENDDATETIME.EventTypeId = 5 /* Event Route Completed */
           AND ROWNUM = 1) AS ROUTEEND, 
       /* INITIAL MILEAGE */                         
       (SELECT MILEAGEBEGIN.Value
          FROM GPS MILEAGEBEGIN 
         WHERE GPS.RouteId = MILEAGEBEGIN.RouteId 
           AND MILEAGEBEGIN.EventTypeId = 21 /* Event Mileage */
           AND ROWNUM = 1
         ORDER BY MILEAGEBEGIN.DateTime
        ) AS INITIALMILEAGE

FROM GPS

INNER JOIN Route
    ON GPS.RouteId = Route.Id

 INNER JOIN Driver
    ON GPS.DriverId = Driver.Id

 INNER JOIN Availability
    ON Driver.Id = Availability.DriverId

 INNER JOIN Vehicle
    ON Availability.VehicleId = Vehicle.Id

 WHERE GPS.EventTypeId = 3 /* Event RouteStarted */

Другой запрос:

SELECT /* DATE OF THE ROUTE */
       GPS.DateTime,           

       /* INITIAL MILEAGE */                         
      ( SELECT TEST  FROM (SELECT MILEAGEBEGIN.Value AS TEST
                                       FROM GPS MILEAGEBEGIN 
                                      WHERE GPS.RouteId = MILEAGEBEGIN.RouteId 
                                        AND MILEAGEBEGIN.EventTypeId = 21 /* Event Mileage */
                                      ORDER BY MILEAGEBEGIN.DateTime ASC
                                    ) 
                              WHERE ROWNUM = 1) AS INITIALMILEAGE

  FROM GPS

 WHERE GPS.EventTypeId = 3 /* Event RouteStarted */
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...