TSQL:如何显示此列的0.00总计

时间:2016-01-21 16:39:51

标签: sql tsql sql-server-2012

我有一个有15个不同部门的部门表。我希望我的查询显示某个项目的总计。在这个特定的项目中,有3个部门正在研究它,并且总计各种各样的东西。我当前的查询确实显示了这三个部门的总数。但是,我还想显示所有其他部门,但只是在这些部门的总计列中有零。这些部门与此项目没有任何关系,但仍需要显示零总计。这是我的查询,导致三行数据。 学科是我所有部门的表格。 Project_Disciplines 是那些实际在项目上工作的人

SELECT   
T.NAme
,ISNULL(CAST(t.EngHours AS DECIMAL(15,0)),0) AS EngHours
,ISNULL(SUM(t.EngBlendedRate),0) as EngRate
,ISNULL(CAST(t.EngHours * SUM(t.EngBlendedRate) AS DECIMAL(15,0)),0) AS EngDollars
,ISNULL(CAST(t.DesignHours AS DECIMAL(15,0)),0) AS DesignHours
,ISNULL(sum(t.DsgBlendedRate),0) AS DesignRate
,ISNULL(CAST(t.DesignHours * SUM(t.DsgBlendedRate) AS DECIMAL(15,0)),0) AS DesignDollars
,ISNULL(t.EngHours + t.DesignHours,0) AS TotalHours
,ISNULL(CAST(CAST((t.EngHours * SUM(t.EngBlendedRate))  + (t.DesignHours * SUM(t.DsgBlendedRate)) AS DECIMAL (18,2)) / CAST(NULLIF(t.EngHours + t.DesignHours,0) AS DECIMAL(18,2)) AS DECIMAL (18,2)),0)  AS TotalRate
,CAST(ISNULL((t.EngHours * SUM(t.EngBlendedRate)),0)  + ISNULL((t.DesignHours * SUM(t.DsgBlendedRate)),0) AS DECIMAL(15,0)) AS TotalDollars
,ISNULL(MAX(case when OA.Code = 'Travel' then OA.Cost end),0) Travel
,ISNULL(MAX(case when OA.Code = 'Equipment' then OA.Cost end),0) Equipment
,ISNULL(MAX(case when OA.Code = 'Subcontract' then OA.Cost end),0) Subcontract
,ISNULL(MAX(case when OA.Code = 'Other' then OA.Cost end),0) Other
,ISNULL(MAX(case when OA.Code = 'Markups' then OA.Cost end),0) Markups
,CAST(ISNULL((t.EngHours * SUM(t.EngBlendedRate)),0)  + ISNULL((t.DesignHours * SUM(t.DsgBlendedRate)),0) AS DECIMAL(15,0)) 
+ ISNULL(MAX(case when OA.Code = 'Travel' then OA.Cost end),0)
+ ISNULL(MAX(case when OA.Code = 'Equipment' then OA.Cost end),0)
+ ISNULL(MAX(case when OA.Code = 'Subcontract' then OA.Cost end),0)
+ ISNULL(MAX(case when OA.Code = 'Other' then OA.Cost end),0)
+ ISNULL(MAX(case when OA.Code = 'Markups' then OA.Cost end),0) AS EstimateTotal

FROM

(SELECT c.id, x.Name, 
ISNULL(CASE WHEN f.team_type_id = 1 OR f.team_type_id = 3
THEN
CAST(f.POH * (d.HourlyRate * (1-(r.Discount/100))/100) AS DECIMAL(8,2))
END,0) AS EngBlendedRate ,
ISNULL(CASE WHEN f.team_type_id = 2 OR f.team_type_id = 4
THEN
ISNULL(CAST(f.POH * (d.HourlyRate * (1-(r.Discount/100))/100) AS DECIMAL(8,2)),0) END,0) AS DsgBlendedRate
,ISNULL(CAST(SUM((b.qty * b.unit_rate)* b.Eng_RPQ /100) AS DECIMAL(8,1)),0) AS [EngHours]
,ISNULL(CAST(SUM((b.qty * b.unit_rate)* b.Design_RPQ /100) AS DECIMAL(8,1)),0) AS [DesignHours]
FROM Project p 
INNER JOIN Project_Disciplines pd on p.id = pd.project_id
INNER JOIN Discipline x on x.id = pd.Disc_id
INNER JOIN Activity c on c.discipline_id = x.id
INNER JOIN Activity_Details b ON b.activity_id = c.id
INNER JOIN Team f on f.activity_id = c.id
INNER JOIN SOF_Details d on d.id = f.sof_detail_id
INNER JOIN Rate r on r.projectid = p.id 
WHERE p.id = 19 
GROUP BY  c.id, x.Name, 
f.team_type_id
, f.POH,d.HourlyRate,r.Discount
) AS T 
LEFT OUTER JOIN (
SELECT a.activity_id, c.Code
,CAST(Sum(a.Rate * a.Qty)AS DECIMAL(18,2)) AS COST

FROM Other_Activity a
INNER JOIN Activity b on a.activity_id = b.id
INNER JOIN EXPENSE_Codes c on a.expense_code_id = c.id
WHERE b.project_id = 19

GROUP BY a.activity_id, c.Code) AS OA ON OA.activity_id = T.id

GROUP BY  T.Name, t.EngHours, t.DesignHours

结果

NAme    EngHours    EngRate EngDollars  DesignHours DesignRate  DesignDollars   TotalHours  TotalRate   TotalDollars    Travel  Equipment   Subcontract Other   Markups EstimateTotal
Architechtural  81  110.73  8947    237 78.85   18711   318.1   86.95   27658   0.00    0.00    0.00    0.00    0.00    27658.00
Concrete    3014    423.72  1277092 2136    357.96  764603  5150.0  396.45  2041695 60.00   45.00   0.00    325.00  0.00    2042125.00
Structural  903 104.98  94744   1351    0.00    0   2253.0  42.05   94744   0.00    0.00    0.00    0.00    0.00    94744.00

我想用零来显示其他部门

NAme    EngHours    EngRate EngDollars  DesignHours DesignRate  DesignDollars   TotalHours  TotalRate   TotalDollars    Travel  Equipment   Subcontract Other   Markups EstimateTotal
Architechtural  81  110.73  8947    237 78.85   18711   318.1   86.95   27658   0   0   0   0   0   27658
Concrete    3014    423.72  1277092 2136    357.96  764603  5150    396.45  2041695 60  45  0   325 0   2042125
Structural  903 104.98  94744   1351    0   0   2253    42.05   94744   0   0   0   0   0   94744
Mechanical  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
Piping  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
Process 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
Civil   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
Electrical  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
Project Admin   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
C&A 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
Estimating  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
Procurement 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

1 个答案:

答案 0 :(得分:1)

我假设Activity和Activity_Details表没有这些部门的任何数据。您应该将它们和任何其他没有所有部门数据的表连接为左连接。

您可以独立运行标记为T的子查询,并确保所有部门都在列表中。在不知道表结构的情况下,您可能需要重新排序子查询T中的表以使“Discipline”成为第一,然后将其他所有其他不保证在所有部门都有信息的内容加入