根据预订数量的客户详细信息

时间:2019-04-19 15:07:40

标签: oracle

写一个查询以显示用户ID和用户名,其中用户在单个预订中预订的座位数大于1。按用户名升序显示记录。

我尝试了此查询,但出现错误。请帮帮我!

select u.user_id,u.name 
     from users u join bookingdetails bd 
     on u.name=bd.name
     join tickets t on u.user_id=t.user_id
     group by u.name
     having count(t.no_seats) > 1
     order by u.name;

4 个答案:

答案 0 :(得分:1)

select distinct u.user_id,u.name from users u 
join tickets t on u.user_id = t.user_id
where
u.user_id in( select user_id from tickets where no_seats>1)
order by u.name;

答案 1 :(得分:0)

如果我了解您的作业,看起来您应该这样做

WITH cteBookings AS (SELECT bd.USER_ID, SUM(t.NO_SEATS) AS TOTAL_SEATS_BOOKED
                       FROM BOOKINGDETAILS bd 
                       INNER JOIN PAYMENTS p
                         ON p.BD_ID = bd.BD_ID
                       INNER JOIN TICKETS t
                         ON t.TICKET_ID = p.TICKET_ID
                       GROUP BY bd.USER_ID)
SELECT DISTINCT b.USER_ID, u.USER_NAME
  FROM USERS u
  INNER JOIN cteBookings b
    ON b.USER_ID = u.USER_ID
  WHERE b.TOTAL_SEATS_BOOKED > 1
  ORDER BY u.USER_NAME ASC

好运。

答案 2 :(得分:0)

根据问题,它要求个人预订> 1 因此,解决方案非常简单,例如,对于 每个ticket_id 座位数不能大于1

SELECT DISTINCT(user_id),名称

FROM票证内部加入用户 使用(user_id)

no_seats> 1

按名称订购;

答案 3 :(得分:0)

如果不使用联接就尝试将更简单。

使用子查询的简单示例:

select  user_id, name from users 
where user_id = any(select distinct user_id from tickets where no_seats > 1) 
order by name asc;