2011-12-03 230 views
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 

使用多個計數這個查詢將返回:在SQL Server 2005

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 

現在我想在項目1至數節數模名稱和no.of.screen名。即我想要查詢返回

project-name  no.of.mod   no.of.screen 
------------------------------------------------ 
Project-1   2     4 

回答

2

這絕對有可能返回多個計數。

換句話說,如下查詢可以修改:根據您的示例數據

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屏幕的計數。