2013-10-11 25 views
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 

回答

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 
+0

天才!真的很感谢这@podiluska – Mike