2017-09-13 30 views
0

enter image description here包含單個和多個結果的公式

我的報告使用兩個表。第一個表格有一個值,第二個表格有多個。我怎樣才能做出一個公式,顯示第一個表中的單個值,以及第二個表中的多個值的總和?

來自這兩個表格的數據來自Excel上傳。所以,這兩個表中的一些數據沒有匹配的值。一些數據具有匹配的值。我想在報告中提供這兩種數據。

回答

0

我認爲你可以計算出它在你的SQL查詢,像這樣:

select 
    [Code], [Name], 
    sum(case when ord = 1 then amount else 0 end) as Amount1, 
    sum(case when ord = 2 then amount else 0 end) as Amount2 
from (
    select [Code], [Name], [Amount1] amount, 1 ord 
    from table1 
    union all 
    select [Code], [Name], [Amount2], 2 
    from table2 
) t 
group by 
    [Code], [Name]; 

SQL Fiddle Demo

+0

非常感謝你:)。 –

相關問題