【问题标题】:count distinct records (all columns) not working [duplicate]计算不同的记录(所有列)不起作用[重复]
【发布时间】:2014-01-04 10:03:40
【问题描述】:

的正确语法是什么
count (distinct *) from t1

我得到他以下错误:

[Err] 42000 - [SQL Server]关键字“distinct”附近的语法不正确。

【问题讨论】:

    标签: sql sql-server tsql count


    【解决方案1】:
    select count(*)
    from
    (
       select distinct * from your_table
    ) x
    

    【讨论】:

    • 成功了,谢谢,十分钟后我会接受。除了语法应该如何之外,是否有一个特殊的原因为什么它有效而原始的无效?
    • @EduardoDennis: sql-syntax - select count(*) from table ... and table is select distinct * from innertable
    • @EduardoDennis:使用distinct 时需要指定一列:select count(distinct col1) ... 如果您想要所有列的不同记录,则必须像我一样使用子查询。
    • 如果要重复使用,建议列出列。
    • 它为子查询命名:(subquery) alias_for_subquery
    猜你喜欢
    • 2022-12-09
    • 2019-07-31
    • 2019-01-23
    • 1970-01-01
    • 2020-10-25
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多