多列上的 SELECT COUNT(DISTINCT...) 错误?

SELECT COUNT(DISTINCT... ) error on multiple columns?(多列上的 SELECT COUNT(DISTINCT...) 错误?)
本文介绍了多列上的 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)

你可以有一个子查询,它返回你可以计算的唯一的 makemodel 集合.

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...) 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)