Sql server 交叉连接到日期的计数值

Sql server 交叉连接到日期的计数值,sql-server,join,count,Sql Server,Join,Count,我已经是第三次尝试解决这个问题了 试图显示每个日历日期有多少个假期是“活的” 现在我有一张有日历日期的桌子,还有一张有假期的桌子。我使用了一个外部连接,因为没有任何东西可以连接两个表。那就是4000次约会,200000次假期,哎哟 以下是选择前10名的时间段,因此不是前进的方向。。。SQL Server SELECT top 10 C.CalendarDate, Case when B.Depart <= C.CalendarDate and vwR.ReturnDate > C

我已经是第三次尝试解决这个问题了

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

现在我有一张有日历日期的桌子,还有一张有假期的桌子。我使用了一个外部连接,因为没有任何东西可以连接两个表。那就是4000次约会,200000次假期,哎哟

以下是选择前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
像这样的

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

天才真的很感谢这个@podiluska