如何将多项工作成本数据汇总到一份合同中?

How do I summarize multiple job cost data to one contract?

我从多个表中提取数据以创建一个报告,该报告提供所有合同的列表以及最近发票的日期、最近工作成本的日期、总成本金额和总计费金额。

我的表格是:

我的查询目前写成如下所示。我 运行 遇到的问题是工作成本。可以有多个工作分配给一个合同,所以我的数据集返回多个结果(见下图)。我不知道如何设置我的查询或 SSRS 报告以仅按合同项目显示详细信息(每个合同一行)。

SELECT 
c.Contract
,c.Department
,c.Contract + ' ' + c.Description as ContractDescription
,c.ContractStatus
,c.ContractAmt
,c.BilledAmt
,(Select MAX(ap.Mth) from APInvoice ap where j.JCCo = ap.JCCo AND j.Job = ap.Job) as RecentInvoice
,(Select MAX(jc.Mth) from JobCost jc where j.JCCo = jc.JCCo AND j.Job = jc.Job) as RecentJobCost
,(Select Sum(jc.ActualCost) from JobCost jc where j.JCCo = jc.JCCo AND j.Job = jc.Job) as ActualCost
,(Select Distinct jc.Job from JobCost jc where j.JCCo = jc.JCCo AND j.Job = jc.Job) as JobNumber
,c.udProjFolderRecd
,c.udBillStatus

FROM ContractDetails c
LEFT JOIN JobDetails j ON j.JCCo = c.JCCo AND j.Contract = c.Contract

WHERE c.JCCo = (@Company)
AND c.ContractStatus IN (@ContractStatus) 
AND c.Department IN (@Department)

试一试:

  SELECT
  Contract,
  Department,
  ContractDescription,
  ContractStatus,
  ContractAmt,
  Max(RecentInvoice) AS RecentInvoice,
  Max(RecentJobCost) AS RecentJobCost,
  Sum(BilledAmt)   AS TotalBilledAmt
FROM   (SELECT
          c.Contract,
          c.Department,
          c.Contract + ' ' + c.Description AS ContractDescription,
          c.ContractStatus,
          c.ContractAmt,
          c.BilledAmt,
          (SELECT             Max(ap.Mth)           FROM   APInvoice ap WHERE  j.JCCo = ap.JCCo AND j.Job = ap.Job)      AS RecentInvoice,
          (SELECT             Max(jc.Mth)           FROM   JobCost jc   WHERE  j.JCCo = jc.JCCo AND j.Job = jc.Job)      AS RecentJobCost,
          (SELECT             Sum(jc.ActualCost)    FROM   JobCost jc   WHERE  j.JCCo = jc.JCCo AND j.Job = jc.Job)      AS ActualCost,
          (SELECT DISTINCT             jc.Job       FROM   JobCost jc   WHERE  j.JCCo = jc.JCCo AND j.Job = jc.Job)      AS JobNumber,
          c.udProjFolderRecd,
          c.udBillStatus
        FROM   ContractDetails c
               LEFT JOIN JobDetails j   ON j.JCCo = c.JCCo  AND j.Contract = c.Contract
        WHERE  c.JCCo = ( @Company )
               AND c.ContractStatus IN ( @ContractStatus )
               AND c.Department IN ( @Department )) contractsummary
GROUP  BY Contract,
          Department,
          ContractDescription,
          ContractStatus,
          ContractAmt