Sql 在Postgres中获得4位小数
我正在尝试运行以下查询:Sql 在Postgres中获得4位小数,sql,postgresql,Sql,Postgresql,我正在尝试运行以下查询: SELECT questionid, (SUM(CASE WHEN correct = FALSE THEN 1 ELSE 0 END)::decimal(10,4) / COUNT(*)::decimal(10,4))::decimal(10,4) AS PercentWrong, COUNT(questionid) AS Questions FROM asmt.testscores WHERE testscoreid IN (SELECT
SELECT questionid,
(SUM(CASE WHEN correct = FALSE THEN 1 ELSE 0 END)::decimal(10,4) / COUNT(*)::decimal(10,4))::decimal(10,4) AS PercentWrong,
COUNT(questionid) AS Questions
FROM asmt.testscores
WHERE testscoreid IN (SELECT DISTINCT test_score_id
FROM ads.fbs_assessment_adaptive_pause_staging)
AND answered = TRUE
AND memberid IS NOT NULL
GROUP BY questionid
ORDER BY PercentWrong
我希望列percenterror
有4位小数,但是上面的查询只给了我2位小数,尽管有decimal(10,4)
。我也试着使用real
,但它根本没有给我小数点
如何获取小数点后4位?尝试使用
试用
SELECT questionid,
to_char(SUM(CASE WHEN correct = FALSE THEN 1 ELSE 0 END)::float / COUNT(*)::float,'99D9999') AS PercentWrong,
COUNT(questionid) AS Questions
FROM asmt.testscores
WHERE testscoreid IN (SELECT DISTINCT test_score_id
FROM ads.fbs_assessment_adaptive_pause_staging)
AND answered = TRUE
AND memberid IS NOT NULL
GROUP BY questionid
ORDER BY PercentWrong