我有以下两个select语句,我正在尝试将结果时间字段添加到一起(有效时间和时间)给我总计 - >我需要返回767.35的值,但我不能解决逻辑问题。请帮忙!
SELECT [ContractID], [CostType], SUM{[Units]}as Time,[CostClass]
FROM [TGMResource].[dbo].[tblDrillOnCost]
where ContractID = '2015/023' and dataset= 'TAGOFTP17' and costclass= 'TIME' and (CostType = 'WORKING TIME' or CostType = 'SURVEY' or CostType = 'MOVE SITE'or CostType = 'TRAVEL')
GROUP BY Costtype, CostClass, DataSet, ContractID
ORDER BY CostType
SELECT SUM([DrillTime]) as Effective_Time
FROM [TGMResource].[dbo].[tblDHDrillHistory]
where ContractID = '2015/023' and dataset= 'TAGOFTP17'
GROUP BY ContractID, DataSet
从第一个查询中我得到结果:
ContractID CostType Time CostClass
2015/023 MOVE SITE 35.5 TIME
2015/023 SURVEY 27 TIME
2015/023 WORKING TIME 44 TIME
从第二个查询我得到结果
Effective_Time
660.85
SELECT tblDrillOnCost.ContractID AS ContractID,
CostType,
SUM( Units ) + Effective_Time AS Total,
CostClass
FROM TGMResource.dbo.tblDrillOnCost AS tblDrillOnCost
JOIN ( SELECT ContractID,
DataSet,
SUM( DrillTime ) AS Effective_Time
FROM TGMResource.dbo.tblDHDrillHistory AS tblDHDrillHistory
WHERE ContractID = '2015/023'
AND dataset = 'TAGOFTP17'
GROUP BY ContractID,
DataSet
) AS EffectiveTimeFinder ON tblDrillOnCost.ContractID = EffectiveTimeFinder.ContractID
WHERE CostType = 'WORKING TIME' or CostType = 'SURVEY' or CostType = 'MOVE SITE'or CostType = 'TRAVEL'
GROUP BY tblDrillOnCost.ContractID,
Effective_Time,
CostType,
CostClass;
给我:
2015/023 MOVE SITE 696.35 TIME
2015/023 SURVEY 687.85 TIME
2015/023 WORKING TIME 711.85 TIME
这又是不正确的:(
答案 0 :(得分:2)
请尝试以下方法......
SELECT tblDrillOnCost.ContractID AS ContractID,
SUM( Units ) + Effective_Time AS Total
FROM TGMResource.dbo.tblDrillOnCost AS tblDrillOnCost
JOIN ( SELECT ContractID,
DataSet,
SUM( DrillTime ) AS Effective_Time
FROM TGMResource.dbo.tblDHDrillHistory AS tblDHDrillHistory
WHERE ContractID = '2015/023'
AND dataset = 'TAGOFTP17'
GROUP BY ContractID,
DataSet
) AS EffectiveTimeFinder ON tblDrillOnCost.ContractID = EffectiveTimeFinder.ContractID
WHERE CostType = 'WORKING TIME'
OR CostType = 'SURVEY'
OR CostType = 'MOVE SITE'
OR CostType = 'TRAVEL'
GROUP BY tblDrillOnCost.ContractID,
Effective_Time;
此语句首先计算Effective_Time
和ContractID
的{{1}}值,然后使用DataSet
和ContractID
值返回,以便结果为此子查询可以在查询的主体中连接,因为DataSet
使用的字段也必须在所选字段中。
这个GROUP BY
是在子查询的结果和INNER JOIN
之间执行的,它们共享相同的tblDrillOnCost
值,实际上附加了ContractID
的{{1}}副本}值到Contract
中的记录。
然后,所有需要做的就是选择字段。首先,Effective_Time
将tblDrillOnCost
的{{1}}字段附加到JOIN
的{{1}}字段,导致在已加入的数据集中出现两个名为EffectiveTimeFinder
的字段。这些可以通过引用它们的源表/查询名称来区分,例如, tblDrillOnCost
或ContractID
。声明的主体只需要选择一个 - 这里无关紧要。它应该在这里给出一个别名,否则结果中的字段名称将是tblDrillOnCost.ContractID
或EffectiveTimeFinder.ContractID
(无论你使用哪个),这是丑陋和笨拙的。
其次,除了计算tblDrillOnCost.ContractID
之外,您还可以在EffectiveTimeFinder.ContractID
语句的同一部分中将其添加到Time
,与Effective_Time
一样。如果您没有为此值指定别名,则该字段将被赋予(同样丑陋且难以处理)名称SELECT
。
第三,您应该从select语句中删除SUM( Units ) + Effective_Time
和SUM( Units ) + Effective_Time
以外的字段。您感兴趣的信息是ContractID
整体,其他字段适用于Total
的子部分。将它们包含在这样的结构语句中将强制对这些字段进行子分组,这将影响Contract
的计算方式。如果您想要一个包含Contract
的列表以及每个对应的Total
和ContractID
以及CostType
的整体总计,那么请修改您的问题表明这一点。
由于子查询已经应用了此过滤器,并且CostClass
正在执行,因此无需为语句的主体添加Contract
WHERE
过滤器{1}}将保留此过滤效果。
如果您有任何问题或意见,请随时发表评论。
答案 1 :(得分:0)
SELECT tblDrillOnCost.ContractID AS ContractID,
SUM( Units ) + Effective_Time AS Utilised_Time
FROM TGMResource.dbo.tblDrillOnCost AS tblDrillOnCost
JOIN ( SELECT ContractID,
DataSet,
SUM( DrillTime ) AS Effective_Time
FROM TGMResource.dbo.tblDHDrillHistory AS tblDHDrillHistory
WHERE ContractID = '2015/023'
AND dataset = 'TAGOFTP17'
GROUP BY ContractID,
DataSet
) AS EffectiveTimeFinder ON tblDrillOnCost.ContractID = EffectiveTimeFinder.ContractID
WHERE tbldrilloncost.DataSet = 'TAGOFTP17' and tbldrilloncost.CostClass = 'time'
AND (CostType = 'SURVEY'
OR CostType = 'MOVE SITE'
OR tbldrilloncost.CostType = 'TRAVEL'
OR tbldrilloncost.CostType = 'WORKING TIME')
GROUP BY tblDrillOnCost.ContractID,
Effective_Time;
工作完美 - 感谢您的帮助@toonice