2017-09-13 31 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

非常感谢你:)。 –

相关问题