【问题标题】:How to write a query for select require columns and count from different tables如何编写查询以选择需要列并从不同表中计数
【发布时间】:2012-08-31 18:36:19
【问题描述】:

我在 mvc3 中工作,我需要一些帮助

这里我有一些表格,例如 (BugId)

BugID Title Description ProjectId Version BuildNumber EmployeId CategoryID CreatedDate SeverityID PriorityID ReleasePhaseID TypeID

2 银行银行应用程序 1 新版本新版本号 1 1 00:00:00.00 1 1 1 1

(项目表)

================================================ ========

 Projectid  ProjectName Description        Status

1   Finance     This is Finance Project    Active
2   Uniformatic This is Finance Project    Active
3   ProDuct     This is Finance Project    InActive
4   Cloud       This is Finance Project    INActive
5   Banking     This is Finance Project    Progress

6 电子商务这是金融项目活跃

RealesePhase(表格)

================================================ ======================

 ReleasePhaseID    ReleasePhase

1       DEV
2       QA
3       Alpha
4       Beta

5 直播

Tostatus(表格)

================================================ ====

ToStatusId        Tostatus

1       New
2       Assigned
3       Fixed
4       Re-Opened
5       Closed
6       Deffered

7 不是错误

Bughistory(表格)

BugHistoryID BugID FixedByID AssignedTo Resolution FromStatus ToStatus

5                  2         1            1     this is my banking     New           New
7                  2         1            1     this assignto res                km,l

================================================ ==================================================== =====

这里我有这些表,现在我必须编写一个查询来选择 ProjectName(dropdown) 和 (ReleasePhase) 从它 (open)(close)(fixedBy)

在 Bugs 表中,Bugs 将被记录(插入),所以现在我们必须选择 Project Name & ReleasePhase 作为下拉列表,如果我们选择项目名称,我应该得到我们拥有的 Bug 数量(计数)

从它喜欢 (view) 应该是这样的

================================================ =======================

项目名称 RealesePhase openBugs ClosedBugs fixedBy

                 1         New         EmployeName
                 2         Assigned    EmployeName
                 3         Fixed       EmployeName
                 4         Re-Opened   EmployeName

================================================ ========================

所以请帮我写一个查询计数,并显示员工插入了多少错误,释放了多少,关闭了多少

提前致谢

【问题讨论】:

    标签: sql-server-2008


    【解决方案1】:

    您最好为此使用存储过程..我认为您需要根据您的状态表和 Releasephase 和 ProjectId 计算错误数..这是我看到您的问题后得出的结论,希望对您有所帮助....

     Create procedure [dbo].[ProjectReports]
     (
     @ProjectID int,
     @ReleasePhaseID int
     )
     as
     begin
     select distinct projectName,ReleasePhase,
    (Select  COUNT(1) from Bugs where ProjectId=a.ProjectId and ReleasePhaseID=a.ReleasePhaseID and
    bugid in (select BugID from BugHistory where [status]='New')) as Newbugs,
    (Select  COUNT(1) from Bugs where ProjectId=a.ProjectId and  ReleasePhaseID=a.ReleasePhaseID and 
    bugid in (select BugID from BugHistory where [status]='Assigned')) as Assignedbugs,
    (Select  COUNT(1) from Bugs where ProjectId=a.ProjectId and ReleasePhaseID=a.ReleasePhaseID and
    bugid in (select BugID from BugHistory where [status]='Fixed')) as Fixedbugs,
    (Select  COUNT(1) from Bugs where ProjectId=a.ProjectId and ReleasePhaseID=a.ReleasePhaseID and
    bugid in (select BugID from BugHistory where [status]='Re-Opened')) as Reopenedbugs,
    (Select  COUNT(1) from Bugs where ProjectId=a.ProjectId and ReleasePhaseID=a.ReleasePhaseID and
    bugid in (select BugID from BugHistory where [status]='Closed')) as Closedbugs,
    (Select  COUNT(1) from Bugs where ProjectId=a.ProjectId and ReleasePhaseID=a.ReleasePhaseID  and
     bugid in (select BugID from BugHistory where [status]='Deffered')) as Defferedbugs,
    (Select  COUNT(1) from Bugs where ProjectId=a.ProjectId and ReleasePhaseID=a.ReleasePhaseID and
     bugid in (select BugID from BugHistory where [status]='Not a Bug')) as NotaBug
     from Bugs a
    inner join Projects p on p.ProjectId=a.ProjectId
     inner join ReleasePhase Rp on rp.ReleasePhaseID=a.ReleasePhaseID
    where a.ProjectId=@ProjectID and a.ReleasePhaseID=@ReleasePhaseID
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 2015-11-15
      相关资源
      最近更新 更多