在 SQL Server 2005 中使用多个计数
select p.intprojectid, p.vcprojectname, md.intmoduleid,
md.vcmodulename, md.intscreensfunc, md.vcname
from projects as p
left join (select m.intprojectid, m.intmoduleid, m.vcmodulename,
s.intscreensfunc, s.vcname
from modules as m
left join screens_func as s on m.intmoduleid = s.intmoduleid) md
on p.intprojectid = md.intprojectid
此查询将返回:
no |project-name|mod-id|mod-name | screen-id | screen-name
----------------------------------------------------------------
2 Project-1 4 mod-1 11 scr1
2 Project-1 4 mod-1 12 scr2
2 Project-1 4 mod-1 13 scr3
2 Project-1 4 mod-1 14 scr4
2 Project-1 8 Module-2 NULL NULL
现在我想计算project-1 中的 no.of mod-name 和 no.of.screen-name 。即我希望查询返回
project-name no.of.mod no.of.screen
------------------------------------------------
Project-1 2 4
select p.intprojectid, p.vcprojectname, md.intmoduleid,
md.vcmodulename, md.intscreensfunc, md.vcname
from projects as p
left join (select m.intprojectid, m.intmoduleid, m.vcmodulename,
s.intscreensfunc, s.vcname
from modules as m
left join screens_func as s on m.intmoduleid = s.intmoduleid) md
on p.intprojectid = md.intprojectid
This query will return:
no |project-name|mod-id|mod-name | screen-id | screen-name
----------------------------------------------------------------
2 Project-1 4 mod-1 11 scr1
2 Project-1 4 mod-1 12 scr2
2 Project-1 4 mod-1 13 scr3
2 Project-1 4 mod-1 14 scr4
2 Project-1 8 Module-2 NULL NULL
Now I want to count no.of mod-name and no.of.screen-name in project-1. i.e. I want the query to return
project-name no.of.mod no.of.screen
------------------------------------------------
Project-1 2 4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绝对有可能返回多个计数。
换句话说,您的查询可以修改如下:
根据您的示例数据,我推断模块和屏幕之间存在一对多关系,因此您需要模块的
distinct
计数但屏幕不需要相同的要求(因为看起来一个屏幕不会在给定模块中多次出现)如果情况并非如此,您还可以将distinct
添加到计数中屏幕数量。It's definitely possible to return multiple counts.
In other words, your query could be modified as follows:
Based on your example data, I inferred that there would be a one-many relationship between modules and screens and thus you would want a
distinct
count for modules but that the same requirement would not be needed for screens (since it appears that one screen would not appear multiple times in a given module) If that is not the case, you can also adddistinct
to the count of screens.