適用範圍:
There is no supertype for types A, B
問題復現:
在用union all,將兩個相同字段名稱的查詢結果相連時報錯。
SQL語句:
select count(distinct user) as "value", name from table1
union all
select sum(userTotal) as "value" , name from table2
報錯日誌:
SQL 錯誤 [386]: ClickHouse exception, code: 386, host: ...., port: ....; Code: 386, e.displayText() = DB::Exception: There is no supertype for types UInt64, Int64 because some of them are signed integers and some are unsigned integers, but there is no signed integer type, that can exactly represent all required unsigned integer values (version 19.15.2.2)
分析原因:
雖然結果字段名一樣,但是類型不同,所以無法union all到一起,一個count的結果是Int64 ,一個sum的結果是UInt64
解決辦法:
將兩個查詢結果對應的字段統一即可。
本查詢是數值類型的不一致,可用toInt64(xxx)進行類型轉換
如下,將語句修改爲:
select count(distinct user) as "value", name from table1
union all
select toUInt64(sum(userTotal)) as "value" , name from table2
再執行就沒問題了。
附 Clickhouse常見數據類型
整數:UInt8,UInt16,UInt32,UInt64,Int8,Int16,Int32,Int64
浮點數:Float32, Float64
字符串:String
日期類型:Date、DateTime
其他:枚舉、數組、元組 等
附 類型轉換函數 部分例子:
SELECT toInt8(12.3334343), toFloat32(10.001), toFloat64(1.000040);
SELECT toString(now());
SELECT now() AS now_local, toString(now(), ‘Asia/Yekaterinburg‘) AS now_yekat;
SELECT now() AS now_local, toDate(now()), toDateTime(now()), toUnixTimestamp(now());