我試圖創建一個視圖,并需要將 "類別 "列的所有值分配給 "指標型別",但得到一個錯誤。
我從其中獲取資料LIN_Main_Followers的表格
。metric_name metric_type metric_value
喜歡 功能 65
分享 國家 101
喜歡 SIZE_10001_OR_MORE 2
喜歡SIZE_11_TO_50 10
...and others
我想創建一個額外的列'category',并給它分配'metric_type'值,如果它 "SIZE_"一樣的值。并將'company_size'的值分配給'metric_type'列。
我的理想結果是:
metric_name metric_type metric_value category
likes company_size 2 SIZE_10001_OR_MORE
likes company_size 10 SIZE_11_TO_50
查看代碼
SELECT test.metric_name,
CASE WHEN test.metric_type LIKE 'SIZE_%' THEN test. category = test.metric_type ELSE test.category END,
CASE WHEN test.metric_type LIKE 'SIZE_%' THEN test. metric_type = 'company_size' ELSE test.metric_type END,
test.metric_type,
test.metric_value
FROM(SELECT lin.metric_name,
lin.metric_type,
lin.metric_value,
'Undefined'::text AS category
FROM "LIN_Main_Followers" lin
WHERE lin.metric_type LIKE 'SIZE_%') AS測驗
但是我得到一個錯誤,即
ERROR: ERROR: CASE-Types text and boolean不能匹配
行 2: CASE WHEN test.metric_type LIKE 'SIZE_%' THEN test.categ...
uj5u.com熱心網友回復:
我認為你的邏輯過于復雜了。 你只需要選擇你想要的列,并給它們起一個你想要的名字:
SELECT lin.metric_name, 'company_size' asmetric_type,
lin.metric_value。
作為類別。
FROM "LIN_Main_Followers" lin
WHERE lin.metric_typeLIKE 'SIZE_%'
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/322156.html
標籤:
上一篇:相同的"num"但不同的值
