SQL - 在BigDecimal中

时间:2016-03-21 15:09:23

标签: mysql sql

SELECT SUM(IF(create_month = MONTH(NOW() - INTERVAL 1 MONTH)
          AND create_year = YEAR(NOW() - INTERVAL 1 MONTH), 1, 0)) AS lastmonth,
   SUM(IF(create_month = MONTH(NOW() - INTERVAL 2 MONTH)
          AND create_year = YEAR(NOW() - INTERVAL 2 MONTH), 1, 0)) AS lastmonth2
FROM incident_view
WHERE customer_company_name = "Company"

大家好,

如何将两个结果(lastmonth和lastmonth2,它们都是Int)转换为Bigdecimal类型/ double或任何其他十进制类型?

会感激一些帮助。

干杯

2 个答案:

答案 0 :(得分:0)

您提到BigDecimal,这是一种Java类型,而不是MySQL类型。在任何情况下,documentation建议使用多种类型(包括NUMERICDECIMAL MySQL类型)与Java BigDecimal进行转换,因此,演员可能看起来像这样:

SELECT CAST(SUM(IF(create_month = MONTH(NOW() - INTERVAL 1 MONTH)
         AND create_year = YEAR(NOW() - INTERVAL 1 MONTH), 1, 0)) AS NUMERIC) AS lastmonth,
       CAST(SUM(IF(create_month = MONTH(NOW() - INTERVAL 2 MONTH)
         AND create_year = YEAR(NOW() - INTERVAL 2 MONTH), 1, 0)) AS NUMERIC) AS lastmonth2
FROM incident_view
WHERE customer_company_name = "Company"

答案 1 :(得分:0)

使用CAST( .. AS DECIMAL)

SELECT 
    CAST(SUM(IF(create_month = MONTH(NOW() - INTERVAL 1 MONTH)
          AND create_year = YEAR(NOW() - INTERVAL 1 MONTH), 1, 0)) AS DECIMAL) AS lastmonth,
    CAST(SUM(IF(create_month = MONTH(NOW() - INTERVAL 2 MONTH)
          AND create_year = YEAR(NOW() - INTERVAL 2 MONTH), 1, 0)) AS DECIMAL) AS lastmonth2
FROM incident_view
WHERE customer_company_name = "Company"