“Operational Error: row value misused” 错误

huangapple go评论64阅读模式
英文:

"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/>

请注意,上述查询将计算所有不同的 firstnamelastname 组合,即使其中一个或两个列为空。

英文:

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.

huangapple
  • 本文由 发表于 2023年7月6日 20:47:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定