SELECT COUNT(DISTINCT... ) error on multiple columns?(多列上的 SELECT COUNT(DISTINCT...) 错误?)
问题描述
我有一个表 VehicleModelYear,包含列 id、year、make 和 model.
I have a table, VehicleModelYear, containing columns id, year, make, and model.
以下两个查询按预期工作:
The following two queries work as expected:
SELECT DISTINCT make, model
FROM VehicleModelYear
SELECT COUNT(DISTINCT make)
FROM VehicleModelYear
但是,此查询不起作用
SELECT COUNT(DISTINCT make, model)
FROM VehicleModelYear
很明显答案是第一个查询返回的结果数量,但只是想知道这个语法有什么问题或者为什么它不起作用.
It's clear the answer is the number of results returned by the first query, but just wondering what is wrong with this syntax or why it doesn't work.
推荐答案
COUNT()
在 SQL Server
接受以下语法
COUNT(*)
COUNT(colName)
COUNT(DISTINCT colName)
你可以有一个子查询,它返回你可以计算的唯一的 make
和 model
集合.
You can have a subquery which returns unique set of make
and model
that you can count with.
SELECT COUNT(*)
FROM
(
SELECT DISTINCT make, model
FROM VehicleModelYear
) a
末尾的a"不是拼写错误.这是一个别名,如果没有它,SQL 将给出错误 ERROR 1248 (42000): 每个派生表必须有自己的别名
.
The "a" at the end is not a typo. It's an alias without which SQL will give an error ERROR 1248 (42000): Every derived table must have its own alias
.
这篇关于多列上的 SELECT COUNT(DISTINCT...) 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:多列上的 SELECT COUNT(DISTINCT...) 错误?


基础教程推荐
- 带更新的 sqlite CTE 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01