【问题标题】:How can I get count of distinct rows in SQL Server?如何获取 SQL Server 中不同行的计数?
【发布时间】:2013-08-30 12:23:03
【问题描述】:

我的查询如下:

select distinct col1,col2,col3,col4 
from tab1;

当我执行上述语句时,如何获得将作为输出的行数? ...或者如何在上面的语句中添加count()

【问题讨论】:

  • 请关闭您的大写锁定!!用全部大写写作正在考虑对读者大喊大叫,这是粗鲁和冒犯的
  • 在该语句之后,@@ROWCOUNT 包含行数

标签: sql-server-2008 count distinct


【解决方案1】:
;with cte as (
    select distinct col1,col2,col3,col4 from tab1
)
select count(1) from cte

【讨论】:

    【解决方案2】:

    @@ROWCOUNT 返回您需要的内容...

    示例用法:

    declare @resultCount AS INT
    
    select distinct col1,col2,col3,col4 
    from tab1;
    
    SELECT @resultCount=@@ROWCOUNT
    
    PRINT @resultCount
    

    【讨论】:

      【解决方案3】:

      SELECT COUNT(DISTINCT col1,col2,col3,col4) FROM tabl

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-06
        • 1970-01-01
        • 2011-05-04
        • 1970-01-01
        • 1970-01-01
        • 2011-11-13
        • 2017-05-30
        • 1970-01-01
        相关资源
        最近更新 更多