Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: SQL 插数报错 1292 - Truncated incorrect DECIMAL,查询正常。
[TiDB Usage Environment] Production Environment
[TiDB Version] TiDB v5.3.0, TiDB v6.1.2
[Reproduction Path]
- Create test table and test data
CREATE TABLE `t_tbl02` (
`loan` decimal(40,2) DEFAULT NULL COMMENT 'loan'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
CREATE TABLE `t_test` (
`company_name` varchar(255) DEFAULT NULL,
`credit_code` varchar(255) DEFAULT NULL,
`year` int(11) DEFAULT NULL,
`revenue_2_years_ago` decimal(20,2) DEFAULT NULL,
`revenue_1_year_ago` decimal(20,2) DEFAULT NULL,
`current_revenue` decimal(20,2) DEFAULT NULL,
`current_assets_1_year_ago` decimal(20,2) DEFAULT NULL,
`current_assets_2_years_ago` decimal(20,2) DEFAULT NULL,
`current_liabilities_1_year_ago` decimal(20,2) DEFAULT NULL,
`current_liabilities_2_years_ago` decimal(20,2) DEFAULT NULL,
`equity_1_year_ago` decimal(20,2) DEFAULT NULL,
`fixed_assets_1_year_ago` decimal(20,2) DEFAULT NULL,
`fixed_assets_2_years_ago` decimal(20,2) DEFAULT NULL,
`intangible_assets_1_year_ago` decimal(20,2) DEFAULT NULL,
`intangible_assets_2_years_ago` decimal(20,2) DEFAULT NULL,
`long_term_loans_1_year_ago` decimal(20,2) DEFAULT NULL,
`inventory_1_year_ago` decimal(20,2) DEFAULT NULL,
`inventory_2_years_ago` decimal(20,2) DEFAULT NULL,
`accounts_receivable_1_year_ago` decimal(20,2) DEFAULT NULL,
`accounts_receivable_2_years_ago` decimal(20,2) DEFAULT NULL,
`accounts_payable_1_year_ago` decimal(20,2) DEFAULT NULL,
`accounts_payable_2_years_ago` decimal(20,2) DEFAULT NULL,
`accumulated_depreciation_1_year_ago` decimal(20,2) DEFAULT NULL,
`short_term_loans_1_year_ago` decimal(20,2) DEFAULT NULL,
`total_profit_1_year_ago` decimal(20,2) DEFAULT NULL,
`net_profit_1_year_ago` decimal(20,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
-- 2.2 Insert test data
INSERT INTO `t_test` (`company_name`, `credit_code`, `year`, `revenue_2_years_ago`, `revenue_1_year_ago`, `current_revenue`, `current_assets_1_year_ago`, `current_assets_2_years_ago`, `current_liabilities_1_year_ago`, `current_liabilities_2_years_ago`, `equity_1_year_ago`, `fixed_assets_1_year_ago`, `fixed_assets_2_years_ago`, `intangible_assets_1_year_ago`, `intangible_assets_2_years_ago`, `long_term_loans_1_year_ago`, `inventory_1_year_ago`, `inventory_2_years_ago`, `accounts_receivable_1_year_ago`, `accounts_receivable_2_years_ago`, `accounts_payable_1_year_ago`, `accounts_payable_2_years_ago`, `accumulated_depreciation_1_year_ago`, `short_term_loans_1_year_ago`, `total_profit_1_year_ago`, `net_profit_1_year_ago`) VALUES ('PingCapTiDB', '91210102555343453T', 2017, 0.00, 0.00, 0.00, 2857907.48, 0.00, 11677684.98, 0.00, -8623888.37, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 2726700.50, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00);
INSERT INTO `t_test` (`company_name`, `credit_code`, `year`, `revenue_2_years_ago`, `revenue_1_year_ago`, `current_revenue`, `current_assets_1_year_ago`, `current_assets_2_years_ago`, `current_liabilities_1_year_ago`, `current_liabilities_2_years_ago`, `equity_1_year_ago`, `fixed_assets_1_year_ago`, `fixed_assets_2_years_ago`, `intangible_assets_1_year_ago`, `intangible_assets_2_years_ago`, `long_term_loans_1_year_ago`, `inventory_1_year_ago`, `inventory_2_years_ago`, `accounts_receivable_1_year_ago`, `accounts_receivable_2_years_ago`, `accounts_payable_1_year_ago`, `accounts_payable_2_years_ago`, `accumulated_depreciation_1_year_ago`, `short_term_loans_1_year_ago`, `total_profit_1_year_ago`, `net_profit_1_year_ago`) VALUES ('PingCapTiDB', '91210102555343453T', 2018, 0.00, -0.01, 23300.97, 2992987.08, 2857907.48, 11742867.69, 11677684.98, -8739739.48, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 2726700.50, 2726700.50, 0.00, 0.00, 0.00, 0.00, -115851.11, -115851.11);
INSERT INTO `t_test` (`company_name`, `credit_code`, `year`, `revenue_2_years_ago`, `revenue_1_year_ago`, `current_revenue`, `current_assets_1_year_ago`, `current_assets_2_years_ago`, `current_liabilities_1_year_ago`, `current_liabilities_2_years_ago`, `equity_1_year_ago`, `fixed_assets_1_year_ago`, `fixed_assets_2_years_ago`, `intangible_assets_1_year_ago`, `intangible_assets_2_years_ago`, `long_term_loans_1_year_ago`, `inventory_1_year_ago`, `inventory_2_years_ago`, `accounts_receivable_1_year_ago`, `accounts_receivable_2_years_ago`, `accounts_payable_1_year_ago`, `accounts_payable_2_years_ago`, `accumulated_depreciation_1_year_ago`, `short_term_loans_1_year_ago`, `total_profit_1_year_ago`, `net_profit_1_year_ago`) VALUES ('PingCapTiDB', '91210102555343453T', 2019, -0.01, 23300.97, 9265462.04, 2694362.03, 2992987.08, 13684986.55, 11742867.69, -10980483.39, 10141.13, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 2579575.50, 2726700.50, 0.00, 0.00, 0.00, 0.00, -2240743.91, -2240743.91);
INSERT INTO `t_test` (`company_name`, `credit_code`, `year`, `revenue_2_years_ago`, `revenue_1_year_ago`, `current_revenue`, `current_assets_1_year_ago`, `current_assets_2_years_ago`, `current_liabilities_1_year_ago`, `current_liabilities_2_years_ago`, `equity_1_year_ago`, `fixed_assets_1_year_ago`, `fixed_assets_2_years_ago`, `intangible_assets_1_year_ago`, `intangible_assets_2_years_ago`, `long_term_loans_1_year_ago`, `inventory_1_year_ago`, `inventory_2_years_ago`, `accounts_receivable_1_year_ago`, `accounts_receivable_2_years_ago`, `accounts_payable_1_year_ago`, `accounts_payable_2_years_ago`, `accumulated_depreciation_1_year_ago`, `short_term_loans_1_year_ago`, `total_profit_1_year_ago`, `net_profit_1_year_ago`) VALUES ('PingCapTiDB', '91210102555343453T', 2020, 23300.97, 9265462.04, 14023278.00, 9336012.96, 2694362.03, 18636168.47, 13684986.55, -9290014.38, 10141.13, 10141.13, 0.00, 0.00, 0.00, 0.00, 0.00, 9103051.50, 2579575.50, 0.00, 0.00, 0.00, 0.00, 1690469.01, 1690469.01);
[Encountered Problem: Problem Phenomenon and Impact]
- Problem SQL
insert into t_tbl02(
loan
)
SELECT
CAST(xq1 AS DECIMAL(40,2)) loan
FROM
(
SELECT
company_name,
credit_code,
year,
CASE WHEN revenue_1_year_ago != 0 AND current_assets_1_year_ago+current_assets_2_years_ago-current_liabilities_1_year_ago-current_liabilities_2_years_ago != 0 THEN ROUND(revenue_1_year_ago*1.000*(1-total_profit_1_year_ago*1.000/revenue_1_year_ago)*current_revenue/revenue_1_year_ago/(revenue_1_year_ago*2.000/(current_assets_1_year_ago+current_assets_2_years_ago-current_liabilities_1_year_ago-current_liabilities_2_years_ago)) - equity_1_year_ago+fixed_assets_1_year_ago+intangible_assets_1_year_ago-long_term_loans_1_year_ago - short_term_loans_1_year_ago,2) ELSE NULL END xq1
,current_revenue exponential_smoothing_forecast
,fixed_assets_1_year_ago-fixed_assets_2_years_ago+inventory_1_year_ago-inventory_2_years_ago+accounts_receivable_1_year_ago-accounts_receivable_2_years_ago-accounts_payable_1_year_ago+accounts_payable_2_years_ago-accumulated_depreciation_1_year_ago-net_profit_1_year_ago xq2
,fixed_assets_1_year_ago-fixed_assets_2_years_ago+intangible_assets_1_year_ago-intangible_assets_2_years_ago xq3
,CASE WHEN (CASE WHEN revenue_1_year_ago != 0 AND current_assets_1_year_ago+current_assets_2_years_ago-current_liabilities_1_year_ago-current_liabilities_2_years_ago != 0 THEN revenue_1_year_ago*1.000*(1-total_profit_1_year_ago*1.000/revenue_1_year_ago)*current_revenue/revenue_1_year_ago/(revenue_1_year_ago*2.000/(current_assets_1_year_ago+current_assets_2_years_ago-current_liabilities_1_year_ago-current_liabilities_2_years_ago)) - equity_1_year_ago+fixed_assets_1_year_ago+intangible_assets_1_year_ago-long_term_loans_1_year_ago - short_term_loans_1_year_ago ELSE NULL END) > 0 THEN 1 ELSE 0 END xqpg1
,CASE WHEN fixed_assets_1_year_ago-fixed_assets_2_years_ago+inventory_1_year_ago-inventory_2_years_ago+accounts_receivable_1_year_ago-accounts_receivable_2_years_ago-accounts_payable_1_year_ago+accounts_payable_2_years_ago-accumulated_depreciation_1_year_ago-net_profit_1_year_ago > 0 THEN 1 ELSE 0 END xqpg2
,CASE WHEN fixed_assets_1_year_ago-fixed_assets_2_years_ago+intangible_assets_1_year_ago-intangible_assets_2_years_ago > 0 THEN 1 ELSE 0 END xqpg3
FROM t_test F
) G WHERE company_name = 'PingCapTiDB' AND year= 2018;
- The value corresponding to the error should be the intermediate result of the following calculation
-237141453587819371373.322233045883214006350133
revenue_1_year_ago*1.000*(1-total_profit_1_year_ago*1.000/revenue_1_year_ago)*current_revenue/revenue_1_year_ago/(revenue_1_year_ago*2.000/(current_assets_1_year_ago+current_assets_2_years_ago-current_liabilities_1_year_ago-current_liabilities_2_years_ago))
- Problem Phenomenon
The SELECT query statement is fine, but adding INSERT reports an error 1292 - Truncated incorrect DECIMAL value: '-237141453587819371373.322233045883214006350133'
Testing in MySQL does not have this issue.
- SQL script to reproduce the problem.
t_test.sql (5.3 KB)