SQL十进制转换

时间:2013-03-22 03:42:54

标签: sql sql-server decimal

我在转换下面显示的小数值时遇到了一些问题:

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 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

这应该适合你:

SELECT ROUND(21.6080402010050,1)

SQL Fiddle Demo

它应该适用于大多数RDBMS。

进行修改:

ROUND((convert(decimal(8,1),SUM(t1.TOTAL)) * 100 / t2.TotalOfLineNo ),1)

答案 2 :(得分:0)

使用 Round 功能

select round(21.6080402010050,1)

答案 3 :(得分:0)

查看ROUND功能。

select round(21.6080402010050, 1)

可以在MS SQL Server或MySQL上运行