日期:2014-05-17  浏览次数:20394 次

在线求解呀。SQL不好就我这下场。
现在有一串这样的SQL
第二行“COUNT(distinct Checkin.ID ) as playerCount”是统计的人数
现在要把这一列分为两列,平日人数和假日人数,是平日还是假日是在holiday表中保存的
也就是说如果chekcin表中的checkinTime包含在select date from Holiday查询的结果集中就是假日
不包含在其中就是平日,
这该怎么改写sql呀

select [IDENTITY].Name, 
COUNT(distinct Checkin.ID ) as playerCount,
sum(Bill.PayTotal) as sumpaytotal
from Checkin , Bill
, [IDENTITY] 
where  Checkin.ID *= Bill.CheckInId 
and Checkin.Status = 6
and Checkin.[Identity] = [Identity].Code
and Bill.Status in(3,4,5)
group by [IDENTITY].Name

------解决方案--------------------
select [IDENTITY].Name, 
COUNT(distinct Checkin.ID ) as playerCount,
sum(Bill.PayTotal) as sumpaytotal
,COUNT(distinct case when holiday.[date] is null then Checkin.ID end ) as 平日人数,
,COUNT(distinct case when holiday.[date] is not null then Checkin.ID end ) as 假日人数,
from Checkin , Bill
, [IDENTITY] ,holiday
where  Checkin.ID *= Bill.CheckInId 
and Checkin.Status = 6
and Checkin.[Identity] = [Identity].Code
and Bill.Status in(3,4,5)
and Checkin.checkinTime *= holiday.[date]
group by [IDENTITY].Name

前提是 Checkin.checkinTime必须是日期,而不能带时间,否则连接条件需要修改