计算按组返回的记录数

时间:2011-02-28 20:06:25

标签: sql-server tsql count group-by

如何按查询计算组返回的记录数

例如:

select count(*) 
from temptable
group by column_1, column_2, column_3, column_4

给我,

1
1
2

我需要计算上述记录才能获得1 + 1 + 1 = 3。

13 个答案:

答案 0 :(得分:145)

您可以使用另一个COUNT

上的OVER子句在一个查询中执行这两个操作
select
    count(*) RecordsPerGroup,
    COUNT(*) OVER () AS TotalRecords
from temptable
group by column_1, column_2, column_3, column_4

答案 1 :(得分:57)

最简单的解决方案是使用派生表:

Select Count(*)
From    (
        Select ...
        From TempTable
        Group By column_1, column_2, column_3, column_4
        ) As Z

另一种解决方案是使用Count Distinct:

Select ...
    , ( Select Count( Distinct column_1, column_2, column_3, column_4 )
        From TempTable ) As CountOfItems
From TempTable
Group By column_1, column_2, column_3, column_4

答案 2 :(得分:22)

我知道现在已经很晚了,但没有人建议:

select count ( distinct column_1, column_2, column_3, column_4) 
from   temptable

这至少在Oracle中有效 - 我目前没有其他数据库可以测试它,我对T-Sql和MySQL语法不太熟悉。

另外,我不完全确定在解析器中以这种方式执行它是否更有效,或者是否其他人的嵌套select语句的解决方案更好。但是从编码的角度来看,我觉得这个更优雅。

答案 3 :(得分:8)

我试图在没有子查询的情况下实现相同的目标,并且能够获得所需的结果,如下所示

SELECT DISTINCT COUNT(*) OVER () AS TotalRecords
FROM temptable
GROUP BY column_1, column_2, column_3, column_4

答案 4 :(得分:4)

CTE为我工作:

with cte as (
  select 1 col1
  from temptable
  group by column_1
)

select COUNT(col1)
from cte;

答案 5 :(得分:2)

怎么样:

SELECT count(column_1)
FROM
    (SELECT * FROM temptable
    GROUP BY column_1, column_2, column_3, column_4) AS Records

答案 6 :(得分:1)

你可以这样做:

select sum(counts) total_records from (
    select count(*) as counts
    from temptable
    group by column_1, column_2, column_3, column_4
) as tmp

答案 7 :(得分:1)

在PostgreSQL中,这对我有用:

select count(count.counts) 
from 
    (select count(*) as counts 
     from table 
     group by concept) as count;

答案 8 :(得分:1)

您可以执行以下代码吗?它在Oracle中有效。

SELECT COUNT(COUNT(*))
FROM temptable
GROUP BY column_1, column_2, column_3, column_4

答案 9 :(得分:0)

您也可以通过以下查询获取

select column_group_by,count(*) as Coulm_name_to_be_displayed from Table group by Column;

-- For example:
select city,count(*) AS Count from people group by city

答案 10 :(得分:0)

尝试此查询:

select top 1 TotalRows = count(*) over () 
from yourTable
group by column1, column2

答案 11 :(得分:0)

接着PrestoDb,其中FirstField可以具有多个值:

select *
            , concat(cast(cast((ThirdTable.Total_Records_in_Group * 100 / ThirdTable.Total_Records_in_baseTable) as DECIMAL(5,2)) as varchar), '%') PERCENTage
from 
(
    SELECT FirstTable.FirstField, FirstTable.SecondField, SecondTable.Total_Records_in_baseTable, count(*) Total_Records_in_Group
    FROM BaseTable FirstTable
    JOIN (
            SELECT FK1, count(*) AS Total_Records_in_baseTable 
            FROM BaseTable
            GROUP BY FK1
        ) SecondTable
    ON FirstTable.FirstField = SecondTable.FK1
    GROUP BY FirstTable.FirstField, FirstTable.SecondField, SecondTable.Total_Records_in_baseTable
    ORDER BY FirstTable.FirstField, FirstTable.SecondField
) ThirdTable

答案 12 :(得分:-1)

如何在SQL Server中使用COUNT OVER(PARTITION BY {column to group by})分区功能?

例如,如果您想按ItemID对产品销售进行分组,并且您想要计算每个不同的ItemID,只需使用:

SELECT
{columns you want} ,
COUNT(ItemID) OVER (PARTITION BY ItemID) as BandedItemCount ,
{more columns you want}... ,
FROM {MyTable}

如果你使用这种方法,你可以将GROUP BY从图片中删除 - 假设你想要返回整个列表(因为你可能会报告条带,你需要知道你要去的所有项目的数量)乐队无需显示整套数据,即Reporting Services)。