英文:
"Operational Error: row value misused" error
问题
我从一个sqlite3查询中得到了一个错误,但找不到原因。
创建表格:
CREATE TABLE professors (
firstname text,
lastname text,
university_shortname text
);
查询:
SELECT COUNT(DISTINCT(firstname, lastname)) FROM professors;
错误信息:
OperationalError: 行值被误用
英文:
I'm getting an error from an sqlite3 query for which I can't find the reason.
CREATE TABLE professors (
firstname text,
lastname text,
university_shortname text
);
Query:
SELECT COUNT(DISTINCT(firstname, lastname)) FROM professors;
Error:
> OperationalError: row value misused
答案1
得分: 1
COUNT()
聚合函数只接受1个参数,该参数不能是像 (firstname, lastname)
这样的行值。<br/>
您的需求的一个解决方法是使用 DENSE_RANK()
窗口函数:
SELECT DENSE_RANK() OVER (ORDER BY firstname, lastname) AS distinct_count
FROM professors
ORDER BY distinct_count DESC LIMIT 1;
查看一个简化的演示。<br/>
请注意,上述查询将计算所有不同的 firstname
和 lastname
组合,即使其中一个或两个列为空。
英文:
COUNT()
aggregate function takes only 1 argument and that argument can't be a row value like (firstname, lastname)
.<br/>
A workaround for your requirement would be the use of DENSE_RANK()
window function:
SELECT DENSE_RANK() OVER (ORDER BY firstname, lastname) AS distinct_count
FROM professors
ORDER BY distinct_count DESC LIMIT 1;
See a simplified demo.<br/>
Note that the above query will count all the distinct combinations of firstname
and lastname
, even the ones where one or both columns are null
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论