简体   繁体   中英

ORA-01427 single-row subquery returns more that one row

I have run into a problem with Oracle SQL the Error is: I am trying to find the locations that have at least one vehicle with manual 我正在尝试查找具有至少一部手动车辆的位置
transmission that has lower mileage than any luxury vehicle at that location.

Here is My Table

VID          MILEAGE         LOCATION        VSIZE        TRANSMISSION
----------------------------------------------------------------------
V-101       70               AHMD            COMPACT      AUTOMATIC
V-102       50               SURAT           COMPACT      AUTOMATIC
V-103       10               AHMD            MID-SIZE     MANUAL
V-104       30               AHMD            MID-SIZE     AUTOMATIC
V-105       15               VADODARA        FULL-SIZE    AUTOMATIC
V-106       20               AHMD            LUXURY       AUTOMATIC
V-107       50               AHMD            LUXURY       MANUAL

And this is my Query:

SELECT location, transmission, mileage FROM vehicles_workshop 
  WHERE transmission = 'MANUAL' AND mileage = 
      ( SELECT DISTINCT mileage FROM vehicles_workshop WHERE mileage < 
          (SELECT MAX(mileage) FROM vehicles_workshop WHERE vsize = 'LUXURY')
      );

Instead of equal to operator use IN before subquery

SELECT location, transmission, mileage FROM vehicles_workshop 
  WHERE transmission = 'MANUAL' AND mileage IN
      ( SELECT DISTINCT mileage FROM vehicles_workshop WHERE mileage < 
          (SELECT MAX(mileage) FROM vehicles_workshop WHERE vsize = 'LUXURY')
      );

Your subquery for comparing mileage is returning more than one value but equal to operator can work to compare only single values and so error is thrown.

SELECT location, transmission, mileage 
FROM vehicles_workshop t1
WHERE transmission = 'MANUAL'         --manual transmission
  AND EXISTS
      ( SELECT *
        FROM vehicles_workshop t2 
        WHERE vsize = 'LUXURY'        --luxury vehicle
        AND t2.location = t1.location --same location
        AND t2.mileage  > t1.mileage) --higher mileage
      ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM