标题:求助~~如何实现分组统计的时候,没有记录的组可以显示为0,而不是不显示
只看楼主
leicumt
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-8-23
结帖率:100%
已结贴  问题点数:20 回复次数:3 
求助~~如何实现分组统计的时候,没有记录的组可以显示为0,而不是不显示
按照TheYear分组,统计每年Numbers<60的条数;
select TheYear,Count(*) from Test Where Numbers<60 Group BY TheYear
得到的结果是:
TheYear Numbers
1999 1


如何才能得到
TheYear Numbers
1999 1
2000 0



PS:我用ISNULL(count(*),0)也没实现想要的效果~~~请各位大侠出手相助~~

表结构如下:
CREATE TABLE [dbo].[Test] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[TheYear] [int] NULL ,
[Numbers] [int] NULL  
) ON [PRIMARY]
GO
insert into Test(TheYear,TotalScore) values (1999,87)
insert into Test(TheYear,TotalScore) values (1999,33)
insert into Test(TheYear,TotalScore) values (2000,85)
insert into Test(TheYear,TotalScore) values (2000,89)
搜索更多相关主题的帖子: 统计 分组 记录 
2010-08-23 22:24
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
得分:10 
select TheYear,次数=count(case when Numbers<60 then Numbers end) from Test group by TheYear

C#超级群 74862681,欢迎大家的到来!
2010-08-23 22:41
qingshuiliu
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:17
帖 子:323
专家分:1538
注 册:2009-12-28
得分:10 
select t.TheYear,sum(t.Number) as Number from
(select TheYear,Count(*) as Number from Test Where Numbers<60 Group BY TheYear
union all
select TheYear,0 from Test) as t
group by t.TheYear

2010-08-23 22:57
aei135
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:12
帖 子:232
专家分:1176
注 册:2009-4-6
得分:0 
select theyear,count(case when numbers<60 then numbers else null end) from test group by theyear
2010-08-24 09:31



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-316617-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.143152 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved