SQL百分比格式问题

时间:2018-06-21 12:00:55

标签: sql sql-server

我编写了没有语法错误的SQL代码,但是它返回了一个空表(如下)。

在没有第13至19行的情况下工作正常,我将“保存”转换为%,并将负值变为0。

希望有人可以提供建议。

Select Distinct rptviewGovtTransparencyCode.project_title As Title,
  rptviewGovtTransparencyCode.department As Department,
  rptviewContract.AwardedDateTime As Award,
  rptviewGovtTransparencyCode.estimated_value As Budget,
  Convert(decimal,Replace(rptviewCustomFieldAnswer.Answer, ',', '')) As Value,
  Case
    When rptviewContract.estimated_value -
    Convert(decimal,Replace(rptviewCustomFieldAnswer.Answer, ',', '')) <=
    0 Then 0
    Else rptviewContract.estimated_value -
    Convert(decimal,Replace(rptviewCustomFieldAnswer.Answer, ',', ''))
  End As Saving,
  Case
    When (Format(((rptviewGovtTransparencyCode.estimated_value -
    Convert(decimal,Replace(rptviewCustomFieldAnswer.Answer, ',', ''))) /
    rptviewGovtTransparencyCode.estimated_value), 'p')) <= 0 Then 0
    Else (Format(((rptviewGovtTransparencyCode.estimated_value -
    Convert(decimal,Replace(rptviewCustomFieldAnswer.Answer, ',', ''))) /
    rptviewGovtTransparencyCode.estimated_value), 'p')) End As [%]
From rptviewGovtTransparencyCode
  Inner Join rptviewContract On rptviewContract.contract_id =
    rptviewGovtTransparencyCode.contract_id
  Inner Join rptviewCustomField On rptviewCustomField.OrgId =
    rptviewContract.OrgId
  Inner Join rptviewCustomFieldAnswer On rptviewCustomFieldAnswer.TargetAreaId =
    rptviewContract.project_id And rptviewCustomField.Id =
    rptviewCustomFieldAnswer.CustomFieldId
  Inner Join rptviewContractPrimaryContact On rptviewContract.contract_id =
    rptviewContractPrimaryContact.ContractId
Where rptviewContract.AwardedDateTime >= '2018-04-01' And
  rptviewCustomField.Title = 'awarded value'
Order By Title

以前,上述第13至19行表示为

Format(((rptviewGovtTransparencyCode.estimated_value -
  Convert(decimal,Replace(rptviewCustomFieldAnswer.Answer, ',', ''))) /
  rptviewGovtTransparencyCode.estimated_value), 'p') As [%]

但是,如何将负%值替换为0仍然不清楚。

1 个答案:

答案 0 :(得分:1)

假设这是SQL Server,问题是FORMAT包装器的位置。 FORMAT返回NVARCHAR,因此您的<= 0行为不符合预期。如下所示,将CASE放在FORMAT内(使用列别名,因为应该这样):

SELECT DISTINCT
  gtc.project_title AS Title
 ,gtc.department AS Department
 ,c.AwardedDateTime AS Award
 ,gtc.estimated_value AS Budget
 ,CONVERT(DECIMAL, REPLACE(cfa.Answer, ',', '')) AS Value
 ,CASE
    WHEN c.estimated_value - CONVERT(DECIMAL, REPLACE(cfa.Answer, ',', '')) <= 0 THEN 0
    ELSE c.estimated_value - CONVERT(DECIMAL, REPLACE(cfa.Answer, ',', ''))
  END AS Saving
 ,FORMAT(CASE WHEN ((gtc.estimated_value - CONVERT(DECIMAL, REPLACE(cfa.Answer, ',', ''))) / gtc.estimated_value) <= 0 
                THEN 0
              ELSE ((gtc.estimated_value - CONVERT(DECIMAL, REPLACE(cfa.Answer, ',', ''))) / gtc.estimated_value)
         END, 'p') 
  AS [%]
FROM
  rptviewGovtTransparencyCode AS gtc
  INNER JOIN
    rptviewContract AS c
      ON
      c.contract_id = gtc.contract_id
  INNER JOIN
    rptviewCustomField AS cf
      ON
      cf.OrgId = c.OrgId
  INNER JOIN
    rptviewCustomFieldAnswer AS cfa
      ON
      cfa.TargetAreaId = c.project_id
      AND cf.Id = cfa.CustomFieldId
  INNER JOIN
    rptviewContractPrimaryContact AS cpc
      ON
      c.contract_id = cpc.ContractId
WHERE
  c.AwardedDateTime >= '2018-04-01'
  AND cf.Title = 'awarded value'
ORDER BY
  Title;