Visual Studio(2017)SQL无法显示%符号

时间:2017-12-24 18:11:12

标签: sql-server database visual-studio-2017 pie-chart

我知道Oracle SQL和SQL Server之间存在差异。查询运行正常并且显示结果很好,但是当我想在饼图上显示它时会出现问题。我在想它可能是Visual Studio的一些限制。

这是我的SQL语句:

SELECT 
    CAST(ROUND(CAST(COUNT(LoanAcNo) AS FLOAT) / 73 * 100, 1) AS VARCHAR) + '%' AS LoanPercentage,
    LoanType
FROM 
    Loan 
GROUP BY 
    LoanType;

这是我实施它的方式:

 public DataSet ReadLoanByLoanType()
 {
        SqlConnection myConn = new SqlConnection(DBConnect);

        StringBuilder sqlStr = new StringBuilder();
        sqlStr.AppendLine("SELECT cast( round( cast ( count(LoanAcNo) as float)  / 73 * 100 , 1 ) as varchar ) + '%' as LoanPercentage , LoanType");
        sqlStr.AppendLine("FROM Loan");
        sqlStr.AppendLine("GROUP BY LoanType");

        SqlDataAdapter da = new SqlDataAdapter(sqlStr.ToString(), myConn);
        DataSet ds = new DataSet();
        da.Fill(ds);

        return ds;
    }

1 个答案:

答案 0 :(得分:2)

如果要从数据库中下载值以包含在某个报表工具的饼图中,请不要通过向其添加“%”将值转换为字符串

您的报告工具将需要一些数字值到图表,而不是字符串

SQL:

    SELECT 
      round(count(LoanAcNo)/ 73.0 * 100.0, 1) as LoanPercentage,
      LoanType
    FROM Loan 
    GROUP BY LoanType;

提示:将整数除以具有小数位的常数(例如,我除以73而不是73)而不是整数应该使SQLS使用浮点数进行计算 - 节省您必须将int转换为浮点数/使SQL更短更整洁