如何计算连接表的每一行中值的出现次数?

时间:2013-11-06 11:27:08

标签: sql oracle oracle11g

我正在使用sql developer和oracle 11g为酒店系统创建数据库,我需要打印first_name,last_name,电子邮件以及每个人(电子邮件)访问酒店的次数。因此,我需要计算预订表中发送电子邮件的次数。

我有2张桌子要加入:

  • 客户:

first_name last_name phone address town email postcode

  • 售票:

Date_ room occupants arrival_time nights paid email_

我希望显示first_name, last_name, email, COUNT(email)

我该怎么做?

2 个答案:

答案 0 :(得分:0)

SELECT   first_name, last_name, customer.email, visits
FROM     customer
JOIN     (SELECT   email_, COUNT(*) AS visits
          FROM     bookings
          GROUP BY email_) v
ON       customer.email = v.email_

答案 1 :(得分:0)

这将是一对多的关系:

SELECT  c.first_name, c.last_name, c.email, COUNT(c.email)
FROM    customer  c
JOIN    booking  b
ON      c.email = b.email_
GROUP   BY
        c.first_name, c.last_name, c.email
;

与:

相同
SELECT  x.col, count(x.col)
FROM
(
        SELECT 1 col FROM DUAL UNION ALL
        SELECT 2     FROM DUAL
) x
JOIN
(
        SELECT 1 col FROM DUAL UNION ALL
        SELECT 1     FROM DUAL UNION ALL
        SELECT 2     FROM DUAL UNION ALL
        SELECT 2     FROM DUAL UNION ALL
        SELECT 2     FROM DUAL UNION ALL
        SELECT 3     FROM DUAL
)  y
ON      x.col = y.col
GROUP   BY
        x.col
;
/*
1   2
2   3
*/