填充WHERE子句的T-sql百分比计算不起作用

时间:2014-05-02 17:28:50

标签: tsql

我有t-sql如下:

SELECT (COUNT(Intakes.fk_ClientID) * 100) / (
        SELECT count(*)
        FROM INTAKES
        WHERE Intakes.AdmissionDate >= @StartDate
        )
FROM Intakes
WHERE Intakes.fk_ReleasedFromID = '1'
    AND Intakes.AdmissionDate >= @StartDate;

我试图从拥有一定范围入场日期的客户子集中获取已经从ID = 1发布的客户的百分比。但我得到的是1行和0行。现在,如果我拿出where子句,我可以得到百分比,它可以工作:

SELECT (COUNT(Intakes.fk_ClientID) * 100) / (
        SELECT count(*)
        FROM INTAKES
        )
FROM Intakes
WHERE Intakes.fk_ReleasedFromID = '1';

工作正常。它选择ReleasedFromID = 1的ClientID,将其乘以100并除以Intakes中的总行数。但是如何使用上面的WHERE子句运行百分比?

1 个答案:

答案 0 :(得分:0)

阅读@Anssssss的评论

SELECT (COUNT(Intakes.fk_ClientID) * 100.0) / (
        SELECT count(*)
        FROM INTAKES
        ) 'percentage'
FROM Intakes
WHERE Intakes.fk_ReleasedFromID = '1';