【发布时间】:2014-10-14 22:36:53
【问题描述】:
我正在尝试除以 2 个计数以返回一个百分比。
以下查询返回0:
select (
(select COUNT(*) from saxref..AuthCycle
where endOfUse is null and addDate >= '1/1/2014') /
(select COUNT(*) from saxref..AuthCycle
where addDate >= '1/1/2014')
) as Percentage
我应该应用演员表吗?
【问题讨论】:
-
添加
1.0 *(或100.0 *),如果它是sql-server。 -
您使用的是哪个 SQL.. SQL Server ???、mySQL 等...
-
COUNT(*) * 100(或100*(SELECT))作为第一个值。 -
语法上一切看起来都是正确的。显然不能代表数据本身。考虑切换到使用下面@PatrickHofman 的回答中的 CASE 语句。执行路径会更短,因为只需要一次读取表。
标签: sql sql-server sum