子查询不返回结果

时间:2013-02-07 00:20:47

标签: sql-server-2008 subquery case

我正在创建一个包含多个子查询的大型SELECT查询。每个子查询都可以正常工作,除了其中一个('Tonnes / Meters')。我使用了与其他链接和值相同的链接和值,但它确实在不同的数据库中使用不同的表。但是语法对我来说是正确的,如果我将有问题的子选择拉出并自己运行,它就可以工作。

这是成功返回其中一行的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

1 个答案:

答案 0 :(得分:1)

我没有看到关于Tones / Meters的CASE声明的ELSE声明。尝试添加 ELSE 语句,看看它是否返回了else值,然后你知道你的情况不正常。

  

(这将是OperationalAssetDetail.Name!='LD%')

尝试这种情况,看看是否返回'Wenis',如果你知道你的情况不起作用。

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) )
ELSE 0
END as 'Tonnes/Metres'

另外用于调试添加

OperationalAssetDetail.Name

作为列结果之一,可以直观地看到它是什么。

此选择需要 LOT 的工作。