从多个重复记录中检索单个唯一记录

时间:2013-03-13 15:55:56

标签: sql oracle select distinct

如果我有以下两个表

HOTELTABLE

 HOTELID   HOTEL NAME
  1        PARK HOTEL
  2        PARK PLAZA
  3        HOLIDAY IN

房间表

HOTELID    ROOMINUMBER  ROOMTYPE  FLOORNUMBER  ROOMRATE
1          111          SINGLE         1          100
1          111          DOUBLE         1          200
1          113          DOUBLE         2          200
2          111          FAMILY         2          300

我想列出其名称中包含单词PARK的每家酒店的酒店名称,房间类型和房价,以及我想列出的同一酒店的同一类型和房价的房间它只有一次。以下是我的代码......我需要一个子查询,我该如何解决这个问题呢?

SELECT DISTINCT HOTELNAME, 
       DISTINCT ROOMTYPE, 
       DISTINCT ROOMRATE
  FROM ROOM, 
       HOTEL
 WHERE HOTELNAME LIKE '%PARK%'
GROUP BY HOTEL.HOTELNAME, 
         ROOMTYPE;

THE OUT PUT SHOUD

HOTEL NAME      ROOMTYPE   ROOMMATE
PARK HOTEL       SINGLE     100
PARK HOTEL       DOUBLE     200
PARK PLAZA       FAMILY     300

2 个答案:

答案 0 :(得分:2)

另一种选择:

SELECT DISTINCT 
    HOTELNAME, 
    ROOMTYPE, 
    ROOMRATE
FROM 
    ROOM 
    JOIN HOTEL
       ON ROOM.HOTEL_ID = HOTEL.HOTEL_ID
WHERE 
    HOTELNAME LIKE '%PARK%'

答案 1 :(得分:1)

您选择的所有项目都必须显示在组中。

您需要根据酒店ID实际加入两张桌子。

SELECT H.HOTELNAME, R.ROOMTYPE, R.ROOMRATE
FROM HOTEL H INNER JOIN ROOM R ON H,HOTELID = R.HOTELID
WHERE H.HOTELNAME LIKE '%PARK%'
GROUP BY H.HOTELNAME, R.ROOMTYPE, R.ROOMRATE