The expression after ELSE should have the same type as those after THEN: "bigint" is expected but "i

本文详细介绍了在使用Hive执行SQL语句时遇到的类型不匹配错误,通过实例展示了如何将else后的常数值从int类型更改为bigint类型,从而成功解决问题。文章还提供了完整的SQL语句示例,帮助读者理解并解决类似问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

hive中执行sql语句:
select pc.category_id, 
sum(case when t.so_month between 3 and 5 then t.order_item_num else 0 end) as spring, 
sum(case when t.so_month between 6 and 8 then t.order_item_num else 0 end) as summer, 
sum(case when t.so_month between 9 and 11 then t.order_item_num else 0 end) as autumn, 
sum(case when t.so_month=12 or t.so_month<=2 then t.order_item_num else 0 end) as winnter 
from product_category pc join (select si.product_id, si.order_item_num, month(si.order_create_time) as so_month from so_item si where si.ds between '2013-05-01' and '2014-04-30' and si.is_gift=0) t on pc.product_id=t.product_id 
group by pc.category_id;
其中t.order_item_num是bigint类型

执行sql,提示错误
“The expression after ELSE should have the same type as those after THEN: "bigint" is expected but "int" is found”

解决办法:
把else后面的0改为0L即可!
select pc.category_id, 
sum(case when t.so_month between 3 and 5 then t.order_item_num else 0L end) as spring, 
sum(case when t.so_month between 6 and 8 then t.order_item_num else 0L end) as summer, 
sum(case when t.so_month between 9 and 11 then t.order_item_num else 0L end) as autumn, 
sum(case when t.so_month=12 or t.so_month<=2 then t.order_item_num else 0L end) as winnter 
from product_category pc join (select si.product_id, si.order_item_num, month(si.order_create_time) as so_month from so_item si where si.ds between '2013-05-01' and '2014-04-30' and si.is_gift=0) t on pc.product_id=t.product_id 
group by pc.category_id;
<think>好的,我现在需要帮用户解决数据库错误:“column "domain_count" is of type bigint but expression is of type text”。首先,这个错误通常出现在PostgreSQL中,当插入或更新数据时,目标列的类型和提供的值类型不匹配。 用户可能是在执行一个INSERT或者UPDATE语句时,给domain_count列赋了一个字符串类型的值,而该列是bigint类型。这时候需要检查SQL语句中的值是否正确,是否不小心用引号包裹了数字,或者从其他表中选择时,源列是文本类型。 接下来要考虑用户可能的场景。比如,他们可能在写一个应用程序,使用参数化查询,但传递的参数类型不对。或者,在从另一个表中选择数据时,源表的对应列是text类型,而目标列是bigint,导致隐式转换失败。 还需要考虑用户是否在拼接SQL字符串时,没有正确转换数据类型,比如在Python中使用字符串格式化,导致数字被当作字符串传递。或者,在数据库迁移或数据导入时,CSV文件中的数字被错误地读成字符串。 然后,我应该提供具体的解决步骤。首先检查SQL语句中的domain_count对应的值是否正确,是否有引号。然后检查源列的数据类型是否匹配。如果使用参数化查询,确认传递的是整数而不是字符串。可能还需要显式转换类型,比如使用CAST或者::bigint。 同时,用户可能需要示例代码来理解如何修正,比如在SQL中使用显式转换,或者在Python中确保传递整数类型。此外,提醒用户注意字段映射和类型一致性,特别是在使用INSERT INTO SELECT时,确保对应列的类型兼容。 还要考虑用户可能不太熟悉PostgreSQL的类型转换规则,特别是隐式转换的限制。例如,PostgreSQL不会自动将text转换为bigint,必须显式转换。因此,用户需要明确处理这类情况。 最后,总结常见原因和解决方案,帮助用户系统地排查问题,并提供预防措施,比如代码审查、测试和数据库设计时的一致性检查。</think>--- ### **错误解析** 当 PostgreSQL 报错 `column "domain_count" is of type bigint but expression is of type text` 时,表示你**尝试将文本类型(字符串)写入一个定义为整型(bigint)的列**,导致类型不匹配。以下是详细分析和解决方法: --- ### **原因分析** | 错误点 | 示例场景 | 错误SQL示例 | |------------------------|------------------------------------------------------------------------|---------------------------------------| | 直接赋值字符串 | 插入数据时用单引号包裹数字 | `INSERT INTO table (domain_count) VALUES ('100')` | | 隐式类型转换失败 | 从其他表中选择数据时,源字段是`text`类型,目标字段是`bigint` | `INSERT INTO table SELECT text_col FROM source` | | 参数化查询类型错误 | 在Python/Java中传递字符串类型参数给`bigint`列 | `cursor.execute("...%s", ("123",))` (传递字符串) | --- ### **解决方案** #### **1. 检查直接赋值语句** ```sql -- ❌ 错误:用单引号包裹数字(隐式转为text) INSERT INTO my_table (domain_count) VALUES ('100'); -- ✅ 修正:移除单引号(显式使用整型) INSERT INTO my_table (domain_count) VALUES (100); ``` #### **2. 处理字段间类型转换** 若从其他表中选择数据,需**显式转换类型**: ```sql -- ❌ 错误:直接选择text类型字段插入bigint列 INSERT INTO target_table (domain_count) SELECT text_col FROM source_table; -- ✅ 修正:使用CAST或::运算符 INSERT INTO target_table (domain_count) SELECT CAST(text_col AS bigint) FROM source_table; -- 方法1 INSERT INTO target_table (domain_count) SELECT text_col::bigint FROM source_table; -- 方法2 ``` #### **3. 参数化查询类型修正(以Python为例)** 确保传入参数为**整型**而非字符串: ```python # ❌ 错误:传递字符串参数 cursor.execute("INSERT INTO table (domain_count) VALUES (%s)", ("100",)) # ✅ 修正:转换为整数 cursor.execute("INSERT INTO table (domain_count) VALUES (%s)", (100,)) # 直接传整型 # 或从字符串转换 cursor.execute("INSERT INTO table (domain_count) VALUES (%s)", (int("100"),)) ``` --- ### **深度验算示例** 假设有一个数据文件 `data.csv`: ```csv id,domain_count 1,"100" -- 注意此处数字被引号包裹成字符串 2,200 ``` **错误导入方式**: ```sql COPY my_table FROM 'data.csv' CSV HEADER; -- 报错:类型不匹配 ``` **正确解决方法**: ```sql -- 创建临时表中转 CREATE TEMP TABLE temp_table (id int, domain_count text); COPY temp_table FROM 'data.csv' CSV HEADER; -- 显式转换后插入目标表 INSERT INTO my_table (id, domain_count) SELECT id, domain_count::bigint FROM temp_table; ``` --- ### **类型映射对照表** | 数据库类型 | 合法值示例 | 非法值示例 | 转换方法 | |-------------|-------------------|----------------|----------------------| | `bigint` | `100`, `-5` | `'100'`, `abc` | `CAST(value AS bigint)` | | `text` | `'hello'`, `'123'` | 无严格限制 | 自动兼容 | --- ### **预防措施** 1. **代码审查**:检查所有涉及`domain_count`的SQL操作 2. **数据库约束**: ```sql -- 添加CHECK约束防止非法值 ALTER TABLE my_table ADD CONSTRAINT domain_count_is_numeric CHECK (domain_count ~ '^[0-9]+$'); -- 仅允许纯数字 ``` 3. **自动化测试**:构建包含字符串和数字的测试用例验证写入逻辑 --- 通过类型严格匹配和显式转换,可彻底解决此类类型冲突问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值