0

我现在正在第三次尝试解决这个问题!

试图显示每个日历日期有多少假期是“活的”。

目前,我有一张带有日历日期的表格和另一张带有假期的表格。我已经使用外部加入了它们,因为没有什么可以加入这两个表。所以这是 4000 个日期 x 200,000 个假期,哎呀!

下面运行的年龄在选定的前10位时,不是前进的道路...(SQL Server)

SELECT top 10

C.CalendarDate,

Case when B.Depart <= C.CalendarDate and vwR.ReturnDate > C.CalendarDate then Count (B.ID) end as 'count'

FROM Calendar C 
CROSS JOIN Booking B -- my issue!!
LEFT join vwReturnDate vwR on vwR.ID=B.ID -- bring in the return date
AND C.CalendarDate > '2013-10-01'
AND C.CalendarDate < '2013-10-30' -- narrow to October only

Group by C.CalendarDate, B.ID, B.depart, vwR.ReturnDate

order by c.CalendarDate
4

1 回答 1

1

像这样的东西?

select 
    calendarDate, COUNT(distinct bookings.id)
from calendarDate
    left join 
        (
          select 
            booking.id, 
            booking.depart,
            vwReturnDate.returndate
          from booking
            left join vwReturnDate on booking.id = vwReturnDate.id
        ) bookings 
             on calendarDate.calendarDate between bookings.depart and bookings.returndate
 where calendarDate between '2013-10-01' and '2013-10-31'
group by calendarDate
于 2013-10-11T12:12:31.437 回答