分组依据在SQL中没有给出适当的计数

时间:2019-02-12 11:52:22

标签: sql sql-server tsql

在上一篇文章中,我像这样在temp表中获取了所有数据

select * from #MyTempEmployeeTable

数据:

version     count1  productname
------------------------------------------
10.0        2       Windows client Update
10.0        10      Windows client Update
10.0        486     Windows client Update
10.0.1      1       Windows client Update
10.0.1      2       Windows client Update
10.0.1      7       Windows client Update
10.0.1      9       Windows client Update
10.0.1      10      Windows client Update
10.0.1      56      Windows client Update
10.0.1      159     Windows client Update
10.0.1      233     Windows client Update
10.0.14     2       Windows client Update
10.0.14     3       Windows client Update
10.0.14     84      Windows client Update
10.0.14     265     Windows client Update
10.0.15     11      Windows client Update
10.0.15     29      Windows client Update
10.0.15     277     Windows client Update
10.0.15     8456    Windows client Update
10.0.18     1       Windows client Update
7.4.76      571     Windows client Update
7.5.7601    27      Windows client Update
7.6.76      1       Windows client Update
7.6.76      606     Windows client Update
7.6.7601    10      Windows client Update
7.6.7601    80      Windows client Update
7.6.7601    128     Windows client Update
7.6.7601    6491    Windows client Update
7.6.7601    51079   Windows client Update
7.8.9200    2       Windows client Update
7.8.9200    171     Windows client Update
7.9.9600    1       Windows client Update
7.9.9600    8       Windows client Update
7.9.9600    11      Windows client Update
7.9.9600    15      Windows client Update
7.9.9600    18      Windows client Update
7.9.9600    41      Windows client Update
7.9.9600    200     Windows client Update
7.9.9600    2026    Windows client Update

我正在编写以下查询,以按版本对分组进行计数,但它没有返回预期的结果。

select count(count1), version   
from #MyTempEmployeeTable 
group by version

运行此查询后,我没有得到适当的数据

(No column name)    version
------------------------------
3   10.0
8   10.0.1
4   10.0.14
4   10.0.15
1   10.0.18
1   7.4.76
1   7.5.7601
2   7.6.76
5   7.6.7601
2   7.8.9200
8   7.9.9600

能帮我一下吗?对于10.0版,理想情况下,我应该将数据作为496

enter image description here

3 个答案:

答案 0 :(得分:2)

您要sum(),而不是count()

select sum(count1), version
from #MyTempEmployeeTable
group by version

答案 1 :(得分:2)

我认为您不希望sum()不计算

select sum (count1),version   
from #MyTempEmployeeTable group by version

答案 2 :(得分:1)

我认为您需要使用SUM

select sum(count1),
  version 
from #MyTempEmployeeTable 
group by version

count是行数,sum是列中值的总和。

,对于10.0版,您将获得498(486 + 10 + 2),而不是问题中的496。