如何仅在两列上使用distinct

时间:2014-11-11 22:12:00

标签: java db2

我在桌子上有这些数据:

CARID MAKECODE MODELCODE YEAR COSTCODE VIN CURRVALUE 123456 NISS MAX 2013 1 AAMBF45V5A5619007 30000.00 123457 NISS MAX 2013 1 ABMBF4DV423459007 30000.00 123458 NISS MAX 2013 1 ACMBF4D15A5319007 30000.00 123459 FORD FOC 2013 2 BAB7DF5665A1SD465 30000.00 123460 FORD FOC 2013 2 BBF7DF563453SDF65 30000.00 123461 FORD FOC 2013 2 BCF7DF5624A1SDF65 30000.00 123462 CHEV MAL 2013 3 CAMB456V223456454 30000.00 123463 CHEV MAL 2013 3 CBMB456V223456454 30000.00 123464 CHEV MAL 2013 3 CCMB456V223456454 30000.00 123465 DODG GRA 2013 4 DADF895OIU2452D25 30000.00 123466 DODG GRA 2013 4 DBDF895OIU2452D25 30000.00 123467 DODG GRA 2013 4 DCDF895OIU2452D25 30000.00 123468 TOYO COR 2013 5 EA765SADF985SAD6D 30000.00 123469 TOYO COR 2013 5 EB765SADF985SAD6D 30000.00 123470 TOYO COR 2013 5 EC765SADF985SAD6D 30000.00 123471 TOYO RAV 2013 6 FA45H45K3HJ4K5H4K 30000.00 123472 TOYO RAV 2013 6 FB45H45K3HJ4K5H4K 30000.00 123473 TOYO RAV 2013 6 FC45H45K3HJ4K5H4K 30000.00 123474 CHEV EQU 2013 7 GA8754DF876SAD876 30000.00 123475 CHEV EQU 2013 7 GB8754DF876SAD876 30000.00 123476 CHEV EQU 2013 7 GC8754DF876SAD876 30000.00

我想要的是:

CARID MAKECODE MODELCODE YEAR COSTCODE VIN CURRVALUE 123456 NISS MAX 2013 1 AAMBF45V5A5619007 30000.00 123459 FORD FOC 2013 2 BAB7DF5665A1SD465 30000.00 123462 CHEV MAL 2013 3 CAMB456V223456454 30000.00 123465 DODG GRA 2013 4 DADF895OIU2452D25 30000.00 123468 TOYO COR 2013 5 EA765SADF985SAD6D 30000.00 123471 TOYO RAV 2013 6 FA45H45K3HJ4K5H4K 30000.00 123474 CHEV EQU 2013 7 GA8754DF876SAD876 30000.00

我从java应用程序访问DB2数据库,并且排除了使用此查询输入的日期不可用的汽车:

SELECT A.CARID, B.CATEGORY, C.MAKEDESC, D.MODELDESC, A.YEAR, B.PERDAY, A.CURRVALUE
             FROM SCM.CAR_INV A
             INNER JOIN SCM.CAR_COST B ON A.COSTCODE = B.COSTCODE
             INNER JOIN SCM.CAR_MAKE C ON A.MAKECODE = C.MAKECODE
             INNER JOIN SCM.CAR_MODEL D ON A.MODELCODE = D.MODELCODE
             WHERE NOT CARID IN (SELECT CARID
             FROM SCM.CAR_RESERV
             WHERE ((PICKUP BETWEEN ? AND ?)
             OR (DROPOFF BETWEEN ? AND ?))
             OR (? BETWEEN PICKUP AND DROPOFF
             OR (? BETWEEN PICKUP AND DROPOFF)))    

所以我想要的是将DISTINCT仅应用于MAKEDESC和MODELDESC列,这些列将返回找到的第一个Car ID,并在之后使用相同的品牌和型号跳过任何ID。

1 个答案:

答案 0 :(得分:0)

如果您想在MAKEDESC和MODELDESC上应用DISTINCT 使用:
GROUP BY MAKEDESC,MODELDESC