SELECT vhfloorno,
Count(ischeckin) AS ChecKIn,
Count(ischeckout) AS CheckOut
FROM visitorhistory t1
WHERE ( Year(vhcheckinebi) = Year(@From)
AND Month(vhcheckinebi) = Month(@From)
AND Day(vhcheckinebi) = Day(@From) )
OR ( Year(vhcheckoutebi) = Year(@To)
AND Month(vhcheckoutebi) = Month(@To)
AND Day(vhcheckoutebi) = Day(@To) )
GROUP BY vhfloorno
我想在Floor的基础上显示一个表中的Check In和Check Out Visitors的计数,但是通过此查询,check in和check Out值是相同的。 其中vhcheckinebi是访客入住时的日期时间,而vhcheckoutebi是访客结账时的日期时间。 请帮帮我。
答案 0 :(得分:2)
假设您的列ischeckin
属于bit
类型,您应该这样做
使用SUM(CAST(ischeckin as INT))
代替Count(ischeckin)
,否则每次都会计算“false”和“true”值:
SELECT vhfloorno,
SUM(CAST(ischeckin as INT)) AS ChecKIn,
SUM(CAST(ischeckout as INT)) AS CheckOut
FROM visitorhistory t1
WHERE ...
请参阅此处以获取最低示例:http://data.stackexchange.com/stackoverflow/query/477199/checkin-and-checkout-counts
答案 1 :(得分:1)
可能你想要条件聚合:
SELECT vhfloorno,
sum(case when ischeckin = 1 then 1 else 0 end) AS ChecKIn,
sum(case when ischeckout = 1 then 1 else 0 end) AS CheckOut
FROM visitorhistory t1
...