用NULL替换空白单元格?

时间:2016-11-17 12:23:21

标签: sql sql-server sql-server-2008 replace null

enter image description here请参阅附图。

我的查询是查看我们的“违规”百分比,但是当没有违规时,我的查询没有返回任何单元格,甚至没有返回NULLS(请参阅附图)

有什么方法可以让我只显示NULL或“100%”而不是空白单元格?

这是我的查询

select *

from 

(select '100' -  convert(decimal(5,2),(100 *         convert(float,CountBreach)/nullif(convert(float,CountNot),0)))  AS '2 week   target'
from

 (select CancerBreach, COUNT(*) [CountBreach] from
(

SELECT 

d.HospitalNumber ,


    CASE WHEN SUM(DATEDIFF(DD,ReceiptOfReferralDate, FirstAppointmentDate)-      FirstAppointmentWaitingTimeAdjusted) > 14
THEN 'Breach' ELSE 'Not Breach'
END AS CancerBreach


  FROM [WH].[SCR].[Referral] R
  LEFT OUTER JOIN WHREPORTING.LK.SpecialtyDivision S
  ON CONVERT(Varchar(10),(R.SpecialtyCode)) = S.SpecialtyCode
  LEFT JOIN WH.SCR.Demographic as d
  on d.UniqueRecordId = R.DemographicUniqueRecordId   

  WHERE 
  R.FirstAppointmentOrganisationCode IN ('RM202', 'RM201')
  AND PriorityTypeCode = '03'
  AND FirstAppointmentDate BETWEEN '01 oct 2016' AND '14 oct 2016'
  AND R.CancerTypeCode <> '16'
   AND R.FirstAppointmentDate is not NULL

  AND NewTumourSite IS NULL
  and s.SpecialtyCode = '103'
  GROUP BY


d.HospitalNumber)f

where CancerBreach = 'breach'

 group by CancerBreach) e,


 (select COUNT(*) [CountNot] from

 (

SELECT 

d.HospitalNumber ,


CASE WHEN SUM(DATEDIFF(DD,ReceiptOfReferralDate, FirstAppointmentDate)-   FirstAppointmentWaitingTimeAdjusted) > 14
THEN 'Breach' ELSE 'Not Breach'
END AS CancerBreach


  FROM [WH].[SCR].[Referral] R
  LEFT OUTER JOIN WHREPORTING.LK.SpecialtyDivision S
  ON CONVERT(Varchar(10),(R.SpecialtyCode)) = S.SpecialtyCode
   LEFT JOIN WH.SCR.Demographic as d
  on d.UniqueRecordId = R.DemographicUniqueRecordId   

   WHERE R.FirstAppointmentOrganisationCode IN ('RM202', 'RM201')
   AND PriorityTypeCode = '03'
   AND FirstAppointmentDate BETWEEN '01 oct 2016' AND '14 oct 2016'
   AND R.CancerTypeCode <> '16'
   AND R.FirstAppointmentDate is not NULL

  AND NewTumourSite IS NULL
  and s.SpecialtyCode = '103'
  GROUP BY


d.HospitalNumber)f

where CancerBreach = 'Not breach'

 group by CancerBreach) d)e

1 个答案:

答案 0 :(得分:0)

您可以使用具有默认值的联合,并返回具有Top 1记录的排序,例如:

SELECT TOP 1 *
FROM
(
    SELECT '100'-CONVERT( DECIMAL(5, 2), (100 * CONVERT(FLOAT, CountBreach) / NULLIF(CONVERT(FLOAT, CountNot), 0))) AS '2 week   target'
    FROM
    (
        SELECT CancerBreach,
               COUNT(*) [CountBreach]
        FROM
        (
            SELECT d.HospitalNumber,
                   CASE
                       WHEN SUM(DATEDIFF(DD, ReceiptOfReferralDate, FirstAppointmentDate) - FirstAppointmentWaitingTimeAdjusted) > 14
                       THEN 'Breach'
                       ELSE 'Not Breach'
                   END AS CancerBreach
            FROM [WH].[SCR].[Referral] R
                 LEFT OUTER JOIN WHREPORTING.LK.SpecialtyDivision S ON CONVERT( VARCHAR(10), (R.SpecialtyCode)) = S.SpecialtyCode
                 LEFT JOIN WH.SCR.Demographic AS d ON d.UniqueRecordId = R.DemographicUniqueRecordId
            WHERE R.FirstAppointmentOrganisationCode IN('RM202', 'RM201')
                 AND PriorityTypeCode = '03'
                 AND FirstAppointmentDate BETWEEN '01 oct 2016' AND '14 oct 2016'
                 AND R.CancerTypeCode <> '16'
                 AND R.FirstAppointmentDate IS NOT NULL
                 AND NewTumourSite IS NULL
                 AND s.SpecialtyCode = '103'
            GROUP BY d.HospitalNumber
        ) f
        WHERE CancerBreach = 'breach'
        GROUP BY CancerBreach
    ) e,
    (
        SELECT COUNT(*) [CountNot]
        FROM
        (
            SELECT d.HospitalNumber,
                   CASE
                       WHEN SUM(DATEDIFF(DD, ReceiptOfReferralDate, FirstAppointmentDate) - FirstAppointmentWaitingTimeAdjusted) > 14
                       THEN 'Breach'
                       ELSE 'Not Breach'
                   END AS CancerBreach
            FROM [WH].[SCR].[Referral] R
                 LEFT OUTER JOIN WHREPORTING.LK.SpecialtyDivision S ON CONVERT( VARCHAR(10), (R.SpecialtyCode)) = S.SpecialtyCode
                 LEFT JOIN WH.SCR.Demographic AS d ON d.UniqueRecordId = R.DemographicUniqueRecordId
            WHERE R.FirstAppointmentOrganisationCode IN('RM202', 'RM201')
                 AND PriorityTypeCode = '03'
                 AND FirstAppointmentDate BETWEEN '01 oct 2016' AND '14 oct 2016'
                 AND R.CancerTypeCode <> '16'
                 AND R.FirstAppointmentDate IS NOT NULL
                 AND NewTumourSite IS NULL
                 AND s.SpecialtyCode = '103'
            GROUP BY d.HospitalNumber
        ) f
        WHERE CancerBreach = 'Not breach'
        GROUP BY CancerBreach
    ) d
    UNION SELECT 100 AS '2 week   target'
) e
ORDER BY [2 week   target] ASC;