Sql 我想显示一个表中的记录
我想在楼层的基础上显示一个表中的入住和退房访客计数,但通过此查询,入住和退房值是相同的。 其中,vhcheckinebi是访客入住的日期时间,vhcheckoutebi是访客退房的日期时间。Sql 我想显示一个表中的记录,sql,sql-server-2008,Sql,Sql Server 2008,我想在楼层的基础上显示一个表中的入住和退房访客计数,但通过此查询,入住和退房值是相同的。 其中,vhcheckinebi是访客入住的日期时间,vhcheckoutebi是访客退房的日期时间。 请帮助我。假设您的列ischeckin是bit类型,您应该 使用SUM(CAST(ischeckin as INT))而不是Count(ischeckin),因为否则每次都会计算“false”和“true”值: SELECT vhfloorno, Count(ischeckin) AS C
请帮助我。假设您的列
ischeckin
是bit
类型,您应该
使用SUM(CAST(ischeckin as INT))
而不是Count(ischeckin)
,因为否则每次都会计算“false”和“true”值:
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
请参见此处,了解最基本的示例:您可能需要条件聚合:
SELECT vhfloorno,
SUM(CAST(ischeckin as INT)) AS ChecKIn,
SUM(CAST(ischeckout as INT)) AS CheckOut
FROM visitorhistory t1
WHERE ...
移除整个WAR子句时,计数是否返回不同的值?签入和签出返回相同的值,即使它们彼此不同
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
...