[英]SQL: How do I combine columns into one, and filter out NULLs?
你只想要這個嗎?
select max(newsletter) as newsletter,
max(report) as report,
max(infographics) as infographics
from t;
答案可能取決於您使用的數據庫,因此請注意。
是否像您的示例中只有一列為非空?
然后是這樣的:
SELECT id, COALESCE(newsletter, infographics, report) FROM my_table;
可能對您有用...
如果使用的是Oracle,請使用NVL
將NULL
替換為空字符串
SELECT id,
combined_column
FROM (
SELECT id,
CONCAT(NVL(newsletter,''), NVL(report,''), NVL(infographics,'')) AS combined_column
FROM table
)
WHERE combined_column is not NULL
我認為您想要coalesce
返回列表中的第一個非空值(如果您連續有多個非空值,它仍將返回第一個):
SELECT id, COALESCE(newsletter, report, infographics) AS combined_date
FROM t
WHERE COALESCE(newsletter, report, infographics) IS NOT NULL
SELECT ID,CONCAT(新聞稿,報告,信息圖表)作為組合表的組合FROM表格WHERE新聞不為空且報告不為空且信息圖表不為空
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.