0

我在转换这个十进制值时遇到了一些问题,如下所示:

21.6080402010050

谁能帮我把它转换成21.6?

我已经尝试过了,但它不起作用。

(convert(decimal(10, 2), SUM(t1.TOTAL))

这是查询

SELECT 
    t1.LINE_NO, t1.E_FIELD, t1.F_FIELD, t1.G_FIELD, t1.HSA_STATUS, 
    t1.FAMILY, t1.CACHE_FAMILY, 
    SUM(t1.TOTAL) AS CountOfLineNo, t2.TotalOfLineNo, 
    (convert(decimal(8,1), SUM(t1.TOTAL)) * 100 / t2.TotalOfLineNo ) AS Percentage
FROM (
    SELECT 
        LINE_NO, E_FIELD,F_FIELD,G_FIELD,HSA_STATUS,FAMILY,CACHE_FAMILY, 
        Count(LINE_NO) as Total
    FROM TX_HSA_SUMM
    WHERE MT_TIMESTAMP2 BETWEEN ('2013-03-07 10:10:00') AND ('2013-03-08 10:20:00')
    GROUP BY LINE_NO, E_FIELD, F_FIELD, G_FIELD, HSA_STATUS, FAMILY, CACHE_FAMILY) AS t1 
LEFT JOIN (
      SELECT LINE_NO, COUNT(LINE_NO) AS TotalOfLineNo
      FROM TX_HSA_SUMM
      WHERE MT_TIMESTAMP2 BETWEEN ('2013-03-07 10:10:00') AND ('2013-03-08 10:20:00')
      GROUP BY LINE_NO) AS t2 ON t1.LINE_NO = t2.LINE_NO
GROUP BY 
   t1.LINE_NO, t1.E_FIELD, t1.F_FIELD, t1.G_FIELD, t1.HSA_STATUS, t1.FAMILY, 
   t1.CACHE_FAMILY, t1.Total, t2.TotalOfLineNo
ORDER BY 
   t1.LINE_NO, t1.E_FIELD, t1.F_FIELD, t1.G_FIELD, t1.HSA_STATUS, t1.FAMILY, 
   t1.CACHE_FAMILY, t1.Total, t2.TotalOfLineNo

输出是

LINE_NO E_FIELD F_FIELD G_FIELD HSA_STATUS FAMILY CACHE_FAMILY CountOfLineNo    TotalOfLineNo   Percentage
23053B  00000   00000   00000   S   SUMMIT  WER 43  199 21.608040201005%
4

4 回答 4

1

请尝试以下查询

SELECT t1.LINE_NO, t1.E_FIELD, t1.F_FIELD, t1.G_FIELD, t1.HSA_STATUS, t1.FAMILY, t1.CACHE_FAMILY, 
    SUM(t1.TOTAL) AS CountOfLineNo, t2.TotalOfLineNo, convert(decimal(18,1),(convert(decimal(8,1),SUM(t1.TOTAL)) * 100 / t2.TotalOfLineNo)) AS Percentage
    FROM (
          SELECT LINE_NO, E_FIELD,F_FIELD,G_FIELD,HSA_STATUS,FAMILY,CACHE_FAMILY, Count(LINE_NO) as Total
          FROM TX_HSA_SUMM
          WHERE MT_TIMESTAMP2 BETWEEN ('2013-03-07 10:10:00') AND ('2013-03-08 10:20:00')
          GROUP BY LINE_NO,E_FIELD,F_FIELD,G_FIELD,HSA_STATUS,FAMILY,CACHE_FAMILY
        ) AS t1 
    LEFT JOIN (
          SELECT LINE_NO, COUNT(LINE_NO) AS TotalOfLineNo
          FROM TX_HSA_SUMM
          WHERE MT_TIMESTAMP2 BETWEEN ('2013-03-07 10:10:00') AND ('2013-03-08 10:20:00')
          GROUP BY LINE_NO
          ) AS t2 
      ON t1.LINE_NO = t2.LINE_NO
    GROUP BY t1.LINE_NO, t1.E_FIELD, t1.F_FIELD, t1.G_FIELD, t1.HSA_STATUS, t1.FAMILY, t1.CACHE_FAMILY, t1.Total,t2.TotalOfLineNo
    ORDER BY t1.LINE_NO, t1.E_FIELD, t1.F_FIELD, t1.G_FIELD, t1.HSA_STATUS, t1.FAMILY, t1.CACHE_FAMILY, t1.Total,t2.TotalOfLineNo
于 2013-03-22T05:16:24.170 回答
0

这应该适合你:

SELECT ROUND(21.6080402010050,1)

SQL 小提琴演示

它应该适用于大多数 RDBMS。

通过您的编辑:

ROUND((convert(decimal(8,1),SUM(t1.TOTAL)) * 100 / t2.TotalOfLineNo ),1)
于 2013-03-22T03:49:12.303 回答
0

使用函数

select round(21.6080402010050,1)
于 2013-03-22T03:49:15.610 回答
0

看看ROUND功能。

select round(21.6080402010050, 1)

适用于 MS SQL Server 或 MySQL

于 2013-03-22T03:49:19.163 回答