SQL(Oracle)选择最常见的值(多个表)

时间:2014-02-21 20:03:15

标签: sql oracle

我想知道哪个座位是个别大厅最畅销的?

订票

IDTICKET   MOVIE_IDMOVIE     HALL_IDHALL      PRICE     SEAT    ROW
1           10               2                4         10      6
2           5                2                4         10      5
3           5                2                4         10      4
4           8                5                4         3       1
5           7                5                4         4       15
6           10               7                4         7       9
7           6                2                4         14      3

HALLS

IDHALL      PLACE_IDPLACE    NAME           NUMSEATS      EQUIPMENT   
1           5                A1             250           high  
2           5                B1             200           medium 
3           5                B2             200           medium 
4           5                C2             180           medium 
5           5                C2             180           medium 
6           9                old hall       120           low 

显示应该看起来像

  1. B1 10
  2. C2 3 ...

2 个答案:

答案 0 :(得分:0)

SELECT  b.Name, a.Seat
FROM    (SELECT     Hall, Seat, COUNT(1) AS SeatCount, RANK() OVER (PARTITION BY Hall ORDER BY COUNT(1) DESC) AS SeatRank
        FROM        SEAT
        GROUP BY    Hall, Seat ) a
            INNER JOIN
        HALL b
        ON a.HALL_IDHALL = b.IDHALL 
WHERE   a.SeatRank = 1

答案 1 :(得分:0)

从门票t,大厅h中选择h.name,t.hall_idhall,h.idhall,max(t.seat),其中t.hall_idhall = h.idhall 分组由h.name,t.hall_idhall,h.idhall

组成

尝试以上查询