2016-02-03 126 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; 
相關問題