SQL查询(可能很简单)

时间:2009-09-15 18:47:00

标签: sql oracle

所以我遇到了这个问题。

我有两个表(Oracle),一个叫做Destination,另一个叫Reserve。 Reserve具有Destination的id的外键(因为保留有一个目标)。保留元组意味着所有用户都已完成的所有储备。我需要一种方法来检查前3个最常访问的目的地(基于表保留中的外键)。

如何在Oracle中使用SQL执行此操作。我知道我需要在Reserve表中搜索3个最重复的Destinations ID,然后将其与Destination表连接以获取前3个目的地的详细信息。

任何帮助都有效。非常感谢你。

SCHEMA:

--------------------------------------------------------
--  File created - martes-septiembre-15-2009   
--------------------------------------------------------
--------------------------------------------------------
--  DDL for Table DESTINO
--------------------------------------------------------

  CREATE TABLE "S2501A29"."DESTINO" 
   (    "PK_ID_DESTINO" NUMBER(10,0), 
    "FK_COD_UBICACION_GEOGRAFICA" NUMBER(10,0), 
    "NOMBRE" VARCHAR2(10), 
    "FOTO" VARCHAR2(30), 
    "DESCRIPCION" VARCHAR2(50)
   ) ;
--------------------------------------------------------
--  DDL for Table LUGAR_ESTADIA
--------------------------------------------------------

  CREATE TABLE "S2501A29"."LUGAR_ESTADIA" 
   (    "PK_ID_ESTADIA" NUMBER(10,0), 
    "NOMBRE" VARCHAR2(10), 
    "TIPO" VARCHAR2(10), 
    "DESCRIPCION" VARCHAR2(50), 
    "COSTO_SERVICIOS" NUMBER, 
    "DESCRIPCION_ALOJAMIENTO" VARCHAR2(100), 
    "DESCRIPCION_ALIMENTACION" VARCHAR2(100)
   ) ;
--------------------------------------------------------
--  DDL for Table OPCION_TRANSPORTE
--------------------------------------------------------

  CREATE TABLE "S2501A29"."OPCION_TRANSPORTE" 
   (    "PK_ID_VIAJE" NUMBER(10,0), 
    "MEDIO_TRANSPORTE" VARCHAR2(10), 
    "RESPONSABLE" VARCHAR2(10), 
    "CIUDAD_ORIGEN" VARCHAR2(10), 
    "CIUDAD_DESTINO" VARCHAR2(10), 
    "COSTO" NUMBER
   ) ;
--------------------------------------------------------
--  DDL for Table RESERVA
--------------------------------------------------------

  CREATE TABLE "S2501A29"."RESERVA" 
   (    "PK_ID_RESERVA" NUMBER(10,0), 
    "FK_COD_DESTINO" NUMBER(10,0), 
    "FK_COD_ESTADIA" NUMBER(10,0), 
    "FK_COD_VIAJE" NUMBER(10,0), 
    "TARJETA_CREDITO" VARCHAR2(12), 
    "FECHA_SALIDA" DATE, 
    "FECHA_REGRESO" DATE, 
    "NOMBRE_USUARIO" VARCHAR2(50)
   ) ;
--------------------------------------------------------
--  DDL for Table UBICACION_GEOGRAFICA
--------------------------------------------------------

  CREATE TABLE "S2501A29"."UBICACION_GEOGRAFICA" 
   (    "PK_ID_UBICACION" NUMBER(10,0), 
    "CIUDAD" VARCHAR2(10), 
    "PAIS" VARCHAR2(10), 
    "CONTINENTE" VARCHAR2(10)
   ) ;

请注意:

RESERVE是Reserva DESTINATION是Destino

因为DB是西班牙语。谢谢!

4 个答案:

答案 0 :(得分:2)

也许我错过了一些东西,但是用外键分组并按结果值排序呢?

编辑:类似于:

select FK_COD_DESTINO, count(*) as qty from RESERVA group by FK_COD_DESTINO order by qty desc limit 3

答案 1 :(得分:1)

怎么样:

WITH CountsByDestination
AS
(
  SELECT FK_COD_DESTINO, 
         COUNT(*) CNT
  FROM Reservations
  GROUP BY DestinationId
),
RankingByDestination
AS
(
  SELECT FK_COD_DESTINO,
         CNT,
         RANK() OVER (ORDER BY CNT) RNK
  FROM CountsByDestination
)
SELECT *
FROM RankingByDestination R
INNER JOIN Destinations D ON D.PK_ID_DESTINO = R.FK_COD_DESTINO
WHERE R.RNK BETWEEN 1 AND 3
ORDER BY R.RNK

这保留了联系,因此如果两个或多个目的地共享相同的计数,它们应该作为结果集的一部分出现。

答案 2 :(得分:1)

Oracle不支持limit。这应该适合你。

select A.FK_COD_DESTINO
     , A.COUNT
     , A.RANK
     , B.*
  from (select FK_COD_DESTINO
             , count(*) as COUNT
             , rank() over (order by count(*) desc) as RANK
          from RESERVA
         group by FK_COD_DESTINO) A
  join DESTINO B on B.PK_ID_DESTINO = A.FK_COD_DESTINO
 where A.RANK <= 3

答案 3 :(得分:1)

你可以尝试HAVING rownum&lt; = 3