统计问题,先谢谢了!!

koukoujiayi 2009-09-08 05:31:30
有两个表:

T1表
year1 areaID(唯一)
1999 1
1999 2
1999 3
2000 4
2000 5
2001 6

T2表
areaID
1
1
1
2
2
4
4
4
5

想得到:
year1 areaCount yearCount
1999 5 3
2000 4 2
2001 1 1

若T2中没有T1的areaID,则areaCount为1,如T1表中的2001,在T2表中没有!!

先谢谢大家了!!
...全文
221 38 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsd123 2009-09-08
  • 打赏
  • 举报
回复
.
ws_hgo 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 36 楼 js_szy 的回复:]
引用 34 楼 koukoujiayi 的回复:
8楼的代码@TT用什么代啊!!
抱歉麻烦说一下!!


你只要把8楼的代码应用到你的表上,把对应的表明改下就可以了
[/Quote]
小卒
以后注意看清题目啊
华夏小卒 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 34 楼 koukoujiayi 的回复:]
8楼的代码@TT用什么代啊!!
抱歉麻烦说一下!!
[/Quote]

你只要把8楼的代码应用到你的表上,把对应的表明改下就可以了
ws_hgo 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 34 楼 koukoujiayi 的回复:]
8楼的代码@TT用什么代啊!!
抱歉麻烦说一下!!
[/Quote]
用什么代??
你可以
create table TT
(
...
)
delare @TT table
(
..
)
都可以啊
koukoujiayi 2009-09-08
  • 打赏
  • 举报
回复
8楼的代码@TT用什么代啊!!
抱歉麻烦说一下!!
ws_hgo 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 32 楼 koukoujiayi 的回复:]
引用 26 楼 js_szy 的回复:
我搞晕了,不知道到底是0,还是1了

楼主能说下,为什么2001的areaCount 的是1吗???

如果,你把2001, 6这个算一个得话

那1999,3 为什么不算一条呢,那1999且不是3+2+1=6条了

这是自定的规则,如有则不加1,如无则加1!!

[/Quote]
isnull(areaCount,1)是这个功能的
你看下isnull函数
koukoujiayi 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 js_szy 的回复:]
我搞晕了,不知道到底是0,还是1了

楼主能说下,为什么2001的areaCount 的是1吗???

如果,你把2001, 6这个算一个得话

那1999,3 为什么不算一条呢,那1999且不是3+2+1=6条了
[/Quote]
这是自定的规则,如有则不加1,如无则加1!!
ws_hgo 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 js_szy 的回复:]
引用 28 楼 ws_hgo 的回复:
小卒

若T2中没有T1的areaID,则areaCount为1,如T1表中的2001,在T2表中没有!!

也就是没有的时候为1
已经说啦!



我倒,题目没看完整。。。。。。。。。!!!BS自己一下。撤!
[/Quote]
华夏小卒 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 ws_hgo 的回复:]
小卒

若T2中没有T1的areaID,则areaCount为1,如T1表中的2001,在T2表中没有!!

也就是没有的时候为1
已经说啦!

[/Quote]

我倒,题目没看完整。。。。。。。。。!!!BS自己一下。撤!
ws_hgo 2009-09-08
  • 打赏
  • 举报
回复
declare @TT table
(
areaID int identity(1,1) primary key,
[year] int
)
insert into @TT select 1999
insert into @TT select 1999
insert into @TT select 1999
insert into @TT select 2000
insert into @TT select 2000
insert into @TT select 2001

declare @T2 table
(
areaID int
)
insert into @T2 select 1
insert into @T2 select 1
insert into @T2 select 1
insert into @T2 select 2
insert into @T2 select 2
insert into @T2 select 4
insert into @T2 select 4
insert into @T2 select 4
insert into @T2 select 5


select T1.*,isnull(T2.areaCount,1) areaCount from
(
select [year],count([year]) yearCount from @TT group by [year]
)T1
left join
(
select [year],isnull(count(T2.areaID),1) areaCount from @T2 T2 join @TT T1 on T2.areaID=T1.areaID group by [year]
) T2
on T1.[year]=T2.[year]


year yearCount areaCount
----------- ----------- -----------
1999 3 5
2000 2 4
2001 1 1
ws_hgo 2009-09-08
  • 打赏
  • 举报
回复
小卒

若T2中没有T1的areaID,则areaCount为1,如T1表中的2001,在T2表中没有!!

也就是没有的时候为1
已经说啦!
华夏小卒 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 ws_hgo 的回复:]
引用 21 楼 koukoujiayi 的回复:
10楼,18楼2001是0了!!
我希望为1!!
可能比较困难,

2001确实为0
用isnull(null,1)
看我8楼写的
[/Quote]

如果这样的话,倒是能接受。。。。
华夏小卒 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 koukoujiayi 的回复:]
10楼,18楼2001是0了!!
我希望为1!!
可能比较困难,
[/Quote]


[Quote=引用 23 楼 ws_hgo 的回复:]
小卒
是为0

[/Quote]


我搞晕了,不知道到底是0,还是1了

楼主能说下,为什么2001的areaCount 的是1吗???

如果,你把2001, 6这个算一个得话


那1999,3 为什么不算一条呢,那1999且不是3+2+1=6条了
soft_wsx 2009-09-08
  • 打赏
  • 举报
回复
/*
Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) Jul 9 2008 14:43:34 Copyright (c)
1988-2008 Microsoft Corporation Enterprise Evaluation Edition on Windows NT 5.1 <X86>
(Build 2600: Service Pack 3)
愿和大家共同进步
如有雷同、实属巧合
●●●●●2009-09-08 18:06:23.640●●●●●
 ★★★★★soft_wsx★★★★★
*/
if OBJECT_ID('t1') is not null drop table t1
create table t1(year1 nvarchar(10),areaid int identity(1,1) primary key)
insert t1(year1)
select'1999'
union all select'1999'
union all select'1999'
union all select'2000'
union all select'2000'
union all select'2001'

if OBJECT_ID('t2') is not null drop table t2
create table t2(areaid int)
insert t2(areaid)
select 1
union all select 1
union all select 1
union all select 2
union all select 2
union all select 4
union all select 4
union all select 4
union all select 5
select year1,SUM(areaCount) as areaCount,MAX(yearcount) as yearcount
from(
select x.year1,areaCount=COUNT(b.areaid),yearcount=(select count(1) from t1 where year1=x.year1 and areaid>=a.areaid)
from (select distinct year1 from t1) x left join t1 a
on x.year1=a.year1
left join t2 b
on a.areaid=b.areaid
group by x.year1,a.year1,a.areaid
) a group by year1
/*
year1 areaCount yearcount
1999 5 3
2000 4 2
2001 0 1
*/
2000方法
ws_hgo 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 koukoujiayi 的回复:]
10楼,18楼2001是0了!!
我希望为1!!
可能比较困难,
[/Quote]
2001确实为0
用isnull(null,1)
看我8楼写的
ws_hgo 2009-09-08
  • 打赏
  • 举报
回复

小卒
是为0
华夏小卒 2009-09-08
  • 打赏
  • 举报
回复
果果,我是说那个areaCount 的2001的为什么会是1
koukoujiayi 2009-09-08
  • 打赏
  • 举报
回复
10楼,18楼2001是0了!!
我希望为1!!
可能比较困难,
ws_hgo 2009-09-08
  • 打赏
  • 举报
回复
看错啦
ws_hgo 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 js_szy 的回复:]
引用 12 楼 ws_hgo 的回复:
引用 11 楼 js_szy 的回复:
奇怪 为什么2001的areaCount
会是1呢

是为1啊



t2 里不是没有2001对应的areaid=6的记录吗
怎么是1?

难道这也要左连接??? 我晕
[/Quote]

declare @TT table
(
areaID int identity(1,1) primary key,
[year] int
)
insert into @TT select 1999
insert into @TT select 1999
insert into @TT select 1999
insert into @TT select 2000
insert into @TT select 2000
insert into @TT select 2001
select [year],count([year]) yearCount from @TT group by [year]
year yearCount
----------- -----------
1999 3
2000 2
2001 1
加载更多回复(18)

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧