关于COUNT的简单问题(是Pradox的)

zoujinhe 2003-12-27 12:58:49
三个表 a,b,c
a是主表,b和c都是子表
a b c (表名)
---------------------------------
SN SN Detail SN Detail (字段名)
------------------------------------------
1 1 fdf 2 gfgdg
2 1 342 3 dfsdfs
3 2 ewrew 3 fdsfsdf

怎么得出
SN count(b) count(c)
--------------------
1 2 0
2 0 1
3 0 2
我写的是
Select a.sn,count(b.sn), count(c.sn)
from a left join b on a.sn=b.sn
left join c on a.sn=c.sn
group by a.sn
结果是错误的.请大家指点.....
...全文
51 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
zoujinhe 2003-12-30
  • 打赏
  • 举报
回复
up
netcoder 2003-12-29
  • 打赏
  • 举报
回复
白忙活了,呵呵
zoujinhe 2003-12-29
  • 打赏
  • 举报
回复
对不起,错了,应该是:
怎么得出
SN count(b) count(c)
--------------------
1 2 0
2 1 1
3 0 2
Jianli2004 2003-12-27
  • 打赏
  • 举报
回复
Select a.sn,count(b.sn), count(c.sn)
from a left join b on a.sn=b.sn
left join c on a.sn=c.sn
group by a.sn 没有问题

根据你的数据结果应该为

sn
----------- ----------- -----------
1 2 0
2 1 1
3 0 2
zoujinhe 2003-12-27
  • 打赏
  • 举报
回复
select SN,
(select count(*) from b where SN = a.SN),
(select count(*) from c where SN = a.SN)
from a

是可以通过,但统计出来全是0。
Jianli2004 2003-12-27
  • 打赏
  • 举报
回复
select SN,
(select count(*) from b where SN = a.SN),
(select count(*) from c where SN = a.SN)
from a

paradox不支持count(1)
xiaocuo_zrf 2003-12-27
  • 打赏
  • 举报
回复
好像不行,只能用Union
cgsun 2003-12-27
  • 打赏
  • 举报
回复
Pardox,是啥
zoujinhe 2003-12-27
  • 打赏
  • 举报
回复
不是吧,都有人说:

{zjcxc(邹建) 哥哥好厉害
=====================
学习~~}

呵呵,我也姓邹。

zjcxc 元老 2003-12-27
  • 打赏
  • 举报
回复
老实一点

我没用过pradox

zoujinhe 2003-12-27
  • 打赏
  • 举报
回复
谢谢各位大佬。

select SN,
(select count(1) from b where SN = a.SN),
(select count(1) from c where SN = a.SN)
from a

到是可以通过,但统计出来全是0。

另外: pradox 是不支持isnull的.
realgz 2003-12-27
  • 打赏
  • 举报
回复
原来如此 paradox 支不支持 isnull?
Select a.sn,(count(*) -sum(isnull(b.sn,1))) sn_b,(count(*) -sum(isnull(c.sn,1))) sn_c
from a left join b on a.sn=b.sn
left join c on a.sn=c.sn
group by a.sn
erigido 2003-12-27
  • 打赏
  • 举报
回复
呵呵~~~,还没有吃饭
xiaocuo_zrf 2003-12-27
  • 打赏
  • 举报
回复
吃乐饭在看
gmlxf 2003-12-27
  • 打赏
  • 举报
回复
select SN,
[count(b)] = (select count(1) from b where SN = a.SN),
[count(c)] = (select count(1) from c where SN = a.SN)
from a

-- 卡看可以用吗?我也不知道。sql是可以。

dlpseeyou 2003-12-27
  • 打赏
  • 举报
回复
select SN,[count(b)] = (select count(1) from b where SN = a.SN), [count(c)] = (select count(1) from c where SN = a.SN)
from a
zoujinhe 2003-12-27
  • 打赏
  • 举报
回复
各位大佬都比较忙,看看标题头好吗?

是Pradox,不支持CASE的。
shuiniu 2003-12-27
  • 打赏
  • 举报
回复

declare @tablea table(sn int)
insert @tablea values(1)
insert @tablea values(2)
insert @tablea values(3)

declare @tableb table(sn int ,detail char(5))
insert @tableb values(1,'fdf')
insert @tableb values(1,'342')
insert @tableb values(2,'ewrew')

declare @tablec table(sn int ,detail char(5))
insert @tablec values(2,'dfdf')
insert @tablec values(3,'dfd')
insert @tablec values(3,'fdds')


select a.sn,
(select count(*) from @tableb where a.sn = sn) [count(b)],
(select count(*) from @tablec where a.sn = sn) [count(c)]
from @tablea a

/*
sn count(b) count(c)
----------- ----------- -----------
1 2 0
2 1 1
3 0 2

(所影响的行数为 3 行)
*/
realgz 2003-12-27
  • 打赏
  • 举报
回复
对 或者用 sum(case when b.sn is null then 0 else 1 end) as [count(b)]
gmlxf 2003-12-27
  • 打赏
  • 举报
回复
select SN,
[count(b)] = (select count(1) from b where SN = a.SN),
[count(c)] = (select count(1) from c where SN = a.SN)
from a
加载更多回复(1)

34,593

社区成员

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

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