同时检索'count where'和总计数

时间:2013-03-11 19:43:39

标签: sql sqlite

我有一个餐厅评级和评论数据库,每个餐厅可以有1到1000个评论。

我首先尝试找到哪些餐馆评价最多4+评论中包含“taco”这个词,并且我使用了以下代码:

    select id, count(id) from test where (comment like '%taco%') AND rating >= 3 group by id order by count(id) DESC;

因此,举例来说,如果餐厅X有30个4+评价的评论包含'taco',那么我会得到'X | 30'。

我想添加两个额外的功能:

  1. 列出每家餐厅的评论总数(无条件)
  2. 给出所有餐厅评论的平均评分,包括'taco'。
  3. 如果X餐厅共有150条评论,其中30条评分为4+,并且包含'taco',这30条评论的平均评分是2.5,我会得到:

    'X | 30 | 150 | 2.5 |'

    我如何得到这个结果?

4 个答案:

答案 0 :(得分:6)

这样的事情可能有用。

select id
, count(*) totalreviews
, sum(case when rating >= 3 and comment like '%taco%' then 1 else 0 end) ratings4plus
, avg(case when rating >= 3 and comment like '%taco%' then rating else null end) avgratings4plus
from test
group by id

答案 1 :(得分:2)

这是未经测试的,但您可以尝试类似

的内容
select id,
       count(id), 
       sum(case when (comment like '%taco%' and rating >=3) then 1 
                else 0 end) taco_rating, 
       avg(case when comment like '%taco%' then rating else null end ) avg_taco
  from test
 group by id

答案 2 :(得分:1)

使用子查询:

SELECT id,
       (SELECT COUNT(*)
        FROM test
        WHERE id = t1.id
          AND comment LIKE '%taco%'
          AND rating >= 3),
       (SELECT COUNT(*)
        FROM test
        WHERE id = t1.id),
       (SELECT AVG(rating)
        FROM test
        WHERE id = t1.id
          AND comment LIKE '%taco%'
          AND rating >= 3),
FROM (SELECT DISTINCT id
      FROM test) AS t1

答案 3 :(得分:1)

对于4+,您的测试应该是rating > 3而不是rating >= 3,但这样做会:

select
    id,
    sum(case when comment like '%taco%'
            AND rating > 3 then 1 else 0 end) as rating4plus_count,
    count(*) as all_ratings_count,
    avg(case when comment like '%taco%'
            AND rating > 3 then rating else null end) as rating4plus_avg
from test
group by id
order by 1 DESC;

注意简写order by 1,这是按“列号1”排序的SQL标准方式(而不是按顺序子句重复第1列的表达式)