在 SQL Server 2005 中使用多个计数

发布于 2024-12-19 05:43:43 字数 1125 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天气好吗我好吗 2024-12-26 05:43:43

绝对有可能返回多个计数。

换句话说,您的查询可以修改如下:

select p.vcprojectname, COUNT(DISTINCT md.intmoduleid) as no.of.mod, COUNT(md.intscreensfunc) as no.of.screen
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
GROUP BY p.vcprojectname

根据您的示例数据,我推断模块和屏幕之间存在一对多关系,因此您需要模块的 distinct 计数但屏幕不需要相同的要求(因为看起来一个屏幕不会在给定模块中多次出现)如果情况并非如此,您还可以将 distinct 添加到计数中屏幕数量。

It's definitely possible to return multiple counts.

In other words, your query could be modified as follows:

select p.vcprojectname, COUNT(DISTINCT md.intmoduleid) as no.of.mod, COUNT(md.intscreensfunc) as no.of.screen
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
GROUP BY p.vcprojectname

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 add distinct to the count of screens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文