我有三张桌子:
TimeSlot table having(TimeSlotId,TimeSlotStart,TimeSlotEnd,TimeSlotToken)
AppointmentSchedule having (ApptId,DoctorId,TimeSlot1Id,TimeSlot2Id,TimeSlot3Id)
Booking having (BookingId,DoctorId,PatientId,BookingDate,TimeSlotId)
TimeSlotToken是医生在他的TimeSlot中看到的患者人数 TimeSlot 1,2和3分别是上午,下午和晚上。
我需要找到TimeSlot未满的日子,即TimeSlot令牌少于该特定日期和该位置的总预订量。由于每位医生每天可能有多个时段,我遇到了麻烦。如果在特定日期填写了所有三个TimeSlots,请向我显示那些不可用的日期(我将在稍后使用)。并找到那些没有填补时间段的时间段和日期。
要查找不可用的日期,我正在尝试代码:
with total_token(token) as (
select timeslottoken
from timeslot
where timeslotid=(select timeslot1id,timeslot2id,timeslot3id
from appointmentbooking
where doctor id=1001),
with token_as_day(bookingdate,token) as (
select bookingdate,count(distinct bookingid)
from bookings
where doctor_id=1001
group by booking_date,timeslot_id)
select token_as_day.bookingdate
from total_token,token_as_day
where token_as_day.token'<'total_token.token;
但这不起作用。请帮忙!
P.S-列名称不区分大小写。我有更复杂的名字,我在这里简化,因此差异..
答案 0 :(得分:0)
从评论到答案供参考 - 由于OP自己也达成了相同的解决方案。
WITH
Total_Token (timeSlotStart, token) AS (
SELECT
timeSlotStart, timeSlotToken
FROM TimeSlot
WHERE
timeSlotId IN (SELECT
timeSlot1Id
FROM AppointmentSchedule
WHERE doctorId = 1
UNION ALL
SELECT
timeSlot2Id
FROM AppointmentSchedule
WHERE doctorId = 1
UNION ALL
SELECT
timeSlot3Id
FROM AppointmentSchedule
WHERE doctorId = 1
)
),
Token_As_Day (bookingDate, token) AS (
SELECT
bookingDate, COUNT(DISTINCT bookingId)
FROM Booking
WHERE doctorId = 1
GROUP BY bookingDate, timeSlotId
)
SELECT
Token_As_Day.bookingDate
FROM Total_Token
JOIN Token_As_Day
ON TRUNC(Total_Token.timeSlotStart) = TRUNC(Token_As_Day.bookingDate)
AND Token_As_Day.token < Total_Token.token
;
查看实际操作:SQL Fiddle。