划分派生等级,从子查询中计算列以查找百分位数

时间:2014-06-10 17:48:46

标签: sql-server-2005 percentile

我无法从子查询中划分两列。唯一回答的答案是0.我已经尝试将两列相乘,看它是否有效而且确实如此。我无法弄清楚问题是什么。

SELECT cert, repdte, NAMEFULL, Rnk, Cnt, (Cnt - Rnk) / Cnt as 'Perc'
FROM
(
    SELECT STRU.cert, STRU.repdte, STRU.NAMEFULL,
    CASE
        WHEN ISNULL(BAL.DEPI5,0) = 0 THEN NULL
        ELSE (ISNULL(INC.EINTEXPA,0) / ISNULL(BAL.DEPI5,0))*100
    END AS 'CoF', RANK() OVER (Partition by STRU.repdte ORDER BY 
    CASE
        WHEN ISNULL(BAL.DEPI5,0) = 0 THEN NULL
        ELSE (ISNULL(INC.EINTEXPA,0) / ISNULL(BAL.DEPI5,0))*100
    END DESC) AS 'Rnk', COUNT(*) OVER (PARTITION BY STRU.repdte) as 'Cnt'
    FROM MODEL_RIS_RMS_FDIC.dbo.STRU as STRU
    JOIN MODEL_RIS_FDIC.dbo.CDI_RC_BAL as BAL
        ON STRU.cert = BAL.cert AND STRU.callYMD = BAL.callYMD
    JOIN MODEL_RIS_FDIC.dbo.CDI_RI_INC as INC
    ON STRU.cert = INC.cert and STRU.callYMD = INC.callYMD
    WHERE 
    CASE
        WHEN ISNULL(BAL.DEPI5,0) = 0 THEN NULL
        ELSE (ISNULL(INC.EINTEXPA,0) / ISNULL(BAL.DEPI5,0))*100
    END IS NOT NULL AND STRU.callYMD >= '2008-03-31'
) A
WHERE Perc < .11

1 个答案:

答案 0 :(得分:0)

我认为Cnt和Rnk的数据类型是INT,因此结果为您提供INT类型的最小值,即0。尝试将Cnt和Rnk转换为FLOAT

(cast(Cnt - Rnk) as FLOAT)/CAST(Cnt as FLOAT)