SQL选择复杂逻辑

时间:2014-06-19 11:01:49

标签: sql sql-server sql-server-2008 group-by sum

我有一个复杂的场景,我将尝试解释。

我有一个名为" Phase"来自ProjectPhases表,其中包含有关项目阶段的描述。然后有一个名为DailyReport的表包含诸如Desription,QTY1,QTY2,PHASEID(ProjectPhases表中的外键)等字段。

现在,前端的用户将为一周内的每一天创建每日报告记录。现在我编写了一个查询来总结包含ProjectPhases数据的每日报告以及每日报告中的数据。

       select P.projectname ,PP.Description as Phase,DD.description, DD.QTYSunday,DD.QTYSMonday,DD.QTYTues,
       from 
       Document_DailyReport DD  LEFT OUTER JOIN
       ProjectPhases PP on PP.Id=DD.PhaseId LEFT OUTER JOIN
       Projects P  on P.Id=DD.ProjectId

查询的输出格式如下

  ProjectName Phase Description QTYSunday QTYMonday QtyTues  
  Project1   Phase 1 Qty-SUNDAy 10                      
  Project1   Phase 1 Qty-Monday              10          
  Project1   Phase 1 Qty-Monday                        10

现在我希望输出采用以下格式

    ProjectName Phase  QTYSunday QTYMonday QtyTues  
    Project1   Phase 1    10       10        10   

我希望特定阶段的每日报告的所有记录都在上面的一行中。

谢谢!

已编辑:

我的完整查询如下。

SELECT P.projectname ,
       PP.Description AS Phase,
       DD.Description,
       DD.DocNumber,

  (SELECT CompanyName
   FROM Companies
   WHERE Id=dbo.GetCompanyIdByUser(DD.InsertedBy)) AS CreatedBy,
       DD.ReportDate,

  (SELECT sum(quantity)+sum(cast(field9 AS INT))
   FROM Document_DailyReportOnSite
   WHERE DailyReportId=DD.Id
     AND ClassificationId=1
     AND DATEDIFF(DAY,reportdate,DATEADD(dd, -(DATEPART(dw, (@Date)-1)), @Date)) =0) AS StaffQuantitySun,

  (SELECT sum(quantity)+sum(cast(field9 AS INT))
   FROM Document_DailyReportOnSite
   WHERE DailyReportId=DD.Id
     AND ClassificationId=1
     AND DATEDIFF(DAY,reportdate,DATEADD(dd, 1-(DATEPART(dw, (@Date)-1)), @Date)) =0) AS StaffQuantityMon,

  (SELECT sum(quantity)+sum(cast(field9 AS INT))
   FROM Document_DailyReportOnSite
   WHERE DailyReportId=DD.Id
     AND ClassificationId=1
     AND DATEDIFF(DAY,reportdate,DATEADD(dd, 2-(DATEPART(dw, (@Date)-1)), @Date)) =0) AS StaffQuantityTues,

  (SELECT sum(quantity)+sum(cast(field9 AS INT))
   FROM Document_DailyReportOnSite
   WHERE DailyReportId=DD.Id
     AND ClassificationId=1
     AND DATEDIFF(DAY,reportdate,DATEADD(dd, 3-(DATEPART(dw, (@Date)-1)), @Date)) =0) AS StaffQuantityWed,

  (SELECT sum(quantity)+sum(cast(field9 AS INT))
   FROM Document_DailyReportOnSite
   WHERE DailyReportId=DD.Id
     AND ClassificationId=1
     AND DATEDIFF(DAY,reportdate,DATEADD(dd, 4-(DATEPART(dw, (@Date)-1)), @Date)) =0) AS StaffQuantityThur,

  (SELECT sum(quantity)+sum(cast(field9 AS INT))
   FROM Document_DailyReportOnSite
   WHERE DailyReportId=DD.Id
     AND ClassificationId=1
     AND DATEDIFF(DAY,reportdate,DATEADD(dd, 5-(DATEPART(dw, (@Date)-1)), @Date)) =0) AS StaffQuantityFri,

  (SELECT sum(quantity)+sum(cast(field9 AS INT))
   FROM Document_DailyReportOnSite
   WHERE DailyReportId=DD.Id
     AND ClassificationId=1
     AND DATEDIFF(DAY,reportdate,DATEADD(dd, 6-(DATEPART(dw, (@Date)-1)), @Date)) =0) AS StaffQuantitySat ,
FROM Document_DailyReport DD
LEFT OUTER JOIN ProjectPhases PP ON PP.Id=DD.PhaseId
LEFT OUTER JOIN Projects P ON P.Id=DD.ProjectId
WHERE DD.ReportDate BETWEEN DATEADD(dd, -(DATEPART(dw,@Date)-1), @Date) AND DATEADD(dd, 6-(DATEPART(dw, @Date)-1), @Date)

输出

 projectname                Phase                                             description                     CreatedBy  0StaffQuantityMon StaffQuantityTues  StaffQuantityWed  StaffQuantityThur
   Bollywood Park   (MCC-ARCO) Bollywood Theatre Main Contractor Package    Daily Report as on 16-Jun-2014  ARCO Contracting    22     NULL     NULL    NULL
   Bollywood Park   (MCC-ARCO) Bollywood Theatre Main Contractor Package    Daily Report as om 17-Jun-2014  ARCO Contracting    NULL        23       NULl        NULL
  Bollywood Park    (MCC-ARCO) Bollywood Theatre Main Contractor Package    Daily Report as on 18.06.2014   ARCO Contracting    NULL       NULL      NULL    23

3 个答案:

答案 0 :(得分:1)

使用GROUP BY功能

试试这个:

SELECT P.projectname, PP.Description AS Phase, 
       SUM(DD.QTYSunday) AS QTYSunday, 
       SUM(DD.QTYSMonday) AS QTYSMonday, 
       SUM(DD.QTYTues) AS QTYTues
FROM Projects P 
INNER JOIN Document_DailyReport DD ON P.Id = DD.ProjectId
LEFT OUTER JOIN ProjectPhases PP ON PP.Id = DD.PhaseId 
GROUP BY P.projectname, PP.Description;

答案 1 :(得分:0)

我可以看到你的描述包括列名,例如。 “数量-周日”。

您可以在说明字段中使用PIVOT返回所需的结果集。有大量文档可以向您展示如何执行此操作。

答案 2 :(得分:0)

您可以尝试以下操作:

SELECT ProjectName, Phase, SUM(StaffQuantitySun) AS StaffQuantitySun, SUM(StaffQuantityMon) AS StaffQuantityMon, SUM(StaffQuantityTues) AS StaffQuantityTues, SUM(StaffQuantityWed) AS StaffQuantityWed,
SUM(StaffQuantityThur) AS StaffQuantityThur, SUM(StaffQuantityFri) AS StaffQuantityFri, SUM(StaffQuantitySat) AS StaffQuantitySat
(
select P.projectname ,PP.Description as Phase,DD.Description,DD.DocNumber,
(Select CompanyName from Companies where Id=dbo.GetCompanyIdByUser(DD.InsertedBy)) As CreatedBy,DD.ReportDate,

(select sum(quantity)+sum(cast(field9 as INT)) from Document_DailyReportOnSite  where DailyReportId=DD.Id  and ClassificationId=1 and DATEDIFF(day,reportdate,DATEADD(dd, -(DATEPART(dw, (@Date)-1)), @Date)) =0) as StaffQuantitySun,

(select sum(quantity)+sum(cast(field9 as INT)) from Document_DailyReportOnSite  where DailyReportId=DD.Id  and ClassificationId=1 and DATEDIFF(day,reportdate,DATEADD(dd, 1-(DATEPART(dw, (@Date)-1)), @Date)) =0) as StaffQuantityMon,

(select sum(quantity)+sum(cast(field9 as INT)) from Document_DailyReportOnSite  where DailyReportId=DD.Id  and ClassificationId=1 and DATEDIFF(day,reportdate,DATEADD(dd, 2-(DATEPART(dw, (@Date)-1)), @Date)) =0) as StaffQuantityTues,

(select sum(quantity)+sum(cast(field9 as INT)) from Document_DailyReportOnSite  where DailyReportId=DD.Id  and ClassificationId=1 and DATEDIFF(day,reportdate,DATEADD(dd, 3-(DATEPART(dw, (@Date)-1)), @Date)) =0) as StaffQuantityWed,

(select sum(quantity)+sum(cast(field9 as INT)) from Document_DailyReportOnSite  where DailyReportId=DD.Id  and ClassificationId=1 and DATEDIFF(day,reportdate,DATEADD(dd, 4-(DATEPART(dw, (@Date)-1)), @Date)) =0) as StaffQuantityThur,

(select sum(quantity)+sum(cast(field9 as INT)) from Document_DailyReportOnSite  where DailyReportId=DD.Id  and ClassificationId=1 and DATEDIFF(day,reportdate,DATEADD(dd, 5-(DATEPART(dw, (@Date)-1)), @Date)) =0) as StaffQuantityFri,

(select sum(quantity)+sum(cast(field9 as INT)) from Document_DailyReportOnSite  where DailyReportId=DD.Id  and ClassificationId=1 and DATEDIFF(day,reportdate,DATEADD(dd, 6-(DATEPART(dw, (@Date)-1)), @Date)) =0) as StaffQuantitySat ,

from 
    Document_DailyReport DD  LEFT OUTER JOIN
    ProjectPhases PP on PP.Id=DD.PhaseId LEFT OUTER JOIN
    Projects P  on P.Id=DD.ProjectId
Where DD.ReportDate between DATEADD(dd, -(DATEPART(dw,@Date)-1), @Date) and DATEADD(dd, 6-(DATEPART(dw, @Date)-1), @Date)
) DerivedTable
GROUP BY ProjectName, Phase

基本上,我已将您的查询嵌入为派生表,然后将GROUP BY项目名称和阶段嵌入,然后将您想要的所有列的SUM整合到一行上。