2016-02-03 96 views
-2

我有两个表。如果在表格中的百分比为null,则我想看看它在表B中SQL Server:是列值为空从另一个表中获得列

SELECT 
    t.id, 
    CASE 
     WHEN t.percentage IS NULL 
     THEN (SELECT percentage FROM test2 AS s WHERE s.id=t.id) 
     ELSE t.percentage 
    END AS percentage 
FROM 
    [project_percentage] t 
+6

[SQL的可能的复制 - 从另一个表中获取价值,如果列为空](http://stackoverflow.com/questions/31195207/sql-get-value-from-another-table-if-column-is-null) –

回答

1

这应做到:

SELECT t.id, COALESCE(t.percentage, s.percentage) AS percentage 
FROM project_percentage t 
LEFT OUTER JOIN 
test2 AS s 
ON s.id = t.id; 
相关问题