我正在创建一个包含多个子查询的大型 SELECT 查询。每个子查询都可以正常工作,除了其中一个(“吨/米”)。我使用了与其他人相同的链接和值,但是它确实在不同的数据库中使用了不同的表。但是语法对我来说看起来是正确的,如果我把有问题的子选择拉出来并自己运行它,它就可以工作。
这是成功返回其中一行的 TEST 子选择(带有硬编码参数)...
SELECT SUM((OreNo*CONVERT(decimal(10,2),ISNULL(round((Code1),2),0))) + (WasteNo*CONVERT(decimal(10,2),ISNULL(round((Code2),2),0))) + (WFG*CONVERT(decimal(10,2),ISNULL(round((Code3),2),0))))
FROM BETA..Bogging, BETA..Equipment, OperationalAssetDetail
WHERE EquipmentName = 'LD038' and
OperationalAssetDetail.Name = EquipmentNameMainpac and
BETA..Bogging.EquipmentID = BETA..Equipment.EquipmentID and
'2013-01' = RTRIM(CAST(DATEPART(year, Date) AS CHAR)) + '-' + right('0' + RTRIM(CAST(DATEPART(month, Date) AS CHAR)),2)
...这是它返回的行...
(无列名)33900.00
...但后来我将它包含在主查询中,它返回 null ...
declare @myAsset varchar(50)
set @myAsset = 'LD038'
select Name, months,
ISNULL(
(
select CONVERT(decimal(10,2),ISNULL(round(ClassValue2,2),0))
from OperationalAssetDetail
where
Name = @myAsset and
SortKeyName = 1000 and
months >= RTRIM(CAST(DATEPART(year, InstallDate) AS CHAR)) + '-' + right('0' + RTRIM(CAST(DATEPART(month, InstallDate) AS CHAR)),2) and
months <= RTRIM(CAST(DATEPART(year, ExpiryDate) AS CHAR)) + '-' + right('0' + RTRIM(CAST(DATEPART(month, ExpiryDate) AS CHAR)),2)
)
,0) as 'Ownership Cost',
(
select CONVERT(decimal(10,2),ISNULL(round(sum(AdditionalCost),2),0))
from Transactiondetail
where
AccountStructureName like '%1625' and
OperationalAssetName = OperationalAssetDetail.Name and
ResourceName = 'Issue' and
months = RTRIM(CAST(DATEPART(year, createtime) AS CHAR)) + '-' + right('0' + RTRIM(CAST(DATEPART(month, createtime) AS CHAR)),2)
) as 'Operating Cost',
(
select CONVERT(decimal(10,2),ISNULL(round(sum(AdditionalCost),2),0))
from Transactiondetail
where
AccountStructureName like '%1627' and
OperationalAssetName = OperationalAssetDetail.Name and
ResourceName = 'Issue' and
months = RTRIM(CAST(DATEPART(year, createtime) AS CHAR)) + '-' + right('0' + RTRIM(CAST(DATEPART(month, createtime) AS CHAR)),2)
) as 'Component Cost',
(
select CONVERT(decimal(10,2),ISNULL(round(sum(AdditionalCost),2),0))
from Transactiondetail
where
AccountStructureName like '%1628' and
OperationalAssetName = OperationalAssetDetail.Name and
ResourceName = 'Issue' and
months = RTRIM(CAST(DATEPART(year, createtime) AS CHAR)) + '-' + right('0' + RTRIM(CAST(DATEPART(month, createtime) AS CHAR)),2)
) as 'Tyre Cost',
(
select CONVERT(decimal(10,2),ISNULL(round(sum(AdditionalCost),2),0) + ClassValue2)
from Transactiondetail
where
(AccountStructureName like '%1625' OR AccountStructureName like 'DM%1627' OR AccountStructureName like 'DM%1628') and
OperationalAssetName = OperationalAssetDetail.Name and
ResourceName = 'Issue' and
months = RTRIM(CAST(DATEPART(year, createtime) AS CHAR)) + '-' + right('0' + RTRIM(CAST(DATEPART(month, createtime) AS CHAR)),2)
) as 'Total Cost',
CASE OperationalAssetDetail.Name
WHEN 'DR%' THEN
(
select CONVERT(decimal(10,2),ISNULL(round(sum(Value),2),0))
from UsageDetail
where (OperationalAssetName LIKE OperationalAssetDetail.Name + '-07' OR OperationalAssetName LIKE OperationalAssetDetail.Name + '-08') and
months = RTRIM(CAST(DATEPART(year, createtime) AS CHAR)) + '-' + right('0' + RTRIM(CAST(DATEPART(month, createtime) AS CHAR)),2)
)
WHEN 'LH%' THEN
(
select CONVERT(decimal(10,2),ISNULL(round(sum(Value),2),0))
from UsageDetail
where OperationalAssetName = OperationalAssetDetail.Name + '-08' and
months = RTRIM(CAST(DATEPART(year, createtime) AS CHAR)) + '-' + right('0' + RTRIM(CAST(DATEPART(month, createtime) AS CHAR)),2)
)
ELSE
(
select CONVERT(decimal(10,2),ISNULL(round(sum(Value),2),0))
from UsageDetail
where OperationalAssetName = OperationalAssetDetail.Name and
months = RTRIM(CAST(DATEPART(year, createtime) AS CHAR)) + '-' + right('0' + RTRIM(CAST(DATEPART(month, createtime) AS CHAR)),2)
)
END as 'Engine Hours',
CASE OperationalAssetDetail.Name
WHEN 'LD%' THEN
(
SELECT SUM((OreNo*CONVERT(decimal(10,2),ISNULL(round((Code1),2),0))) + (WasteNo*CONVERT(decimal(10,2),ISNULL(round((Code2),2),0))) + (WFG*CONVERT(decimal(10,2),ISNULL(round((Code3),2),0))))
FROM BETA..Bogging, BETA..Equipment, OperationalAssetDetail
WHERE EquipmentName = @myAsset and
OperationalAssetDetail.Name = EquipmentNameMainpac and
BETA..Bogging.EquipmentID = BETA..Equipment.EquipmentID and
months = RTRIM(CAST(DATEPART(year, Date) AS CHAR)) + '-' + right('0' + RTRIM(CAST(DATEPART(month, Date) AS CHAR)),2)
)
END as 'Tonnes/Metres'
from months, OperationalAssetDetail
where
Name = @myAsset and
SortKeyName = 1000 and
months >= RTRIM(CAST(DATEPART(year, InstallDate) AS CHAR)) + '-' + right('0' + RTRIM(CAST(DATEPART(month, InstallDate) AS CHAR)),2) and
months <= RTRIM(CAST(DATEPART(year, CURRENT_TIMESTAMP) AS CHAR)) + '-' + right('0' + RTRIM(CAST(DATEPART(month, CURRENT_TIMESTAMP) AS CHAR)),2)
...这是结果...
Name months Ownership Cost Operating Cost Component Cost Tyre Cost Total Cost Engine Hours Tonnes/Metres
LD038 2010-08 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2010-09 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2010-10 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2010-11 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2010-12 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2011-01 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2011-02 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2011-03 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2011-04 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2011-05 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2011-06 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2011-07 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2011-08 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2011-09 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2011-10 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2011-11 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2011-12 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2012-01 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2012-02 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2012-03 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2012-04 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2012-05 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2012-06 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2012-07 45072.37 0.00 0.00 0.00 45072.37 0.00 NULL
LD038 2012-08 45072.37 0.00 0.00 0.00 45072.37 7659.00 NULL
LD038 2012-09 45072.37 839.49 0.00 0.00 45911.86 191.00 NULL
LD038 2012-10 45072.37 2817.43 0.00 20382.67 68272.47 488.00 NULL
LD038 2012-11 45072.37 3103.25 0.00 0.00 48175.62 358.00 NULL
LD038 2012-12 45072.37 1322.96 0.00 0.00 46395.33 461.00 NULL
LD038 2013-01 45072.37 23346.40 0.00 0.00 68418.77 225.00 NULL
LD038 2013-02 45072.37 507.58 0.00 0.00 45579.95 0.00 NULL