此SQLite
查询中出现此错误:
select f.rid, c.title, sum(some_expression) as ratio
from doesntmatter
where c.active = 1 or (ratio = 1.0 and c.active = 0
and c.deactivated in (1, 2, 3, 4, 5)
group by f.rid
This问题通过having
子句解决了这个问题:
select f.rid, c.title, sum(some_expression) as ratio
from doesntmatter
where c.active = 1 or (c.active = 0
and c.deactivated in (1, 2, 3, 4, 5)
group by f.rid
having ratio = 1.0
但在我的情况下,这会改变表达式的语义。你知道如何实现原始where
子句中的表达式吗?
答案 0 :(得分:1)
基本上,您不能在where子句中引用别名列。请查看此question,了解如何解决此问题。
另外,您应该使用select
语句中出现的所有非汇总列添加到组中,在本例中为c.id
和c.title
。
很难为你重写那个查询,因为逻辑有点难以理解(因为错误的分组)。
修改强>
经过一番思考,您可能只需要更正group by
。
select c.id, c.title, sum(some_expression) as ratio
from doesntmatter
where c.active = 1 or (c.active = 0 and c.deactivated in (1, 2, 3, 4, 5))
group by c.id, c.title
having ratio = 1.0
或者也许:
select f.rid, sum(some_expression) as ratio
from doesntmatter
where c.active = 1 or (c.active = 0 and c.deactivated in (1, 2, 3, 4, 5))
group by f.rid
having ratio = 1.0
我无法帮助你完成上一部分,因为我不知道你的表的字段和数据,也不知道如何解释它们。但选择不在组中的字段是一个坏主意。无论如何,请记住,您可以随时加入原始表格并获取您要查找的信息,而不仅仅是select
中显示的字段。