我有以下代码:
select c.category
,sum(b.is_open) as open
,count(b.name) as total
from business b inner join category c on b.id=c.business_id
group by c.category
order by sum(b.is_open) desc
limit 10
这给了我以下数据集:
+------------------+------+-------+
| category | open | total |
+------------------+------+-------+
| Restaurants | 53 | 71 |
| Shopping | 25 | 30 |
| Food | 20 | 23 |
| Health & Medical | 16 | 17 |
| Home Services | 15 | 16 |
| Beauty & Spas | 12 | 13 |
| Nightlife | 12 | 20 |
| Bars | 11 | 17 |
| Active Life | 10 | 10 |
| Local Services | 10 | 12 |
+------------------+------+-------+
但是,如果我将第2行和第3行更改为:
sum(b.is_open) / count(b.name) as '%'
我一直没有得到一个特定的值,而是一直为零。我试图将两列都转换为十进制类型(尽管看起来它们一开始就是这样),但没有用。为什么我无法获得正确的结果?我正在用SQLite编写查询。
答案 0 :(得分:0)
尝试使用浮点算术代替整数算术:
1.0 * sum(b.is_open) / count(b.name) as '%'