--test in your sql management
--populating test table
declare @table1 table (name varchar(30), marks int )
declare @table2 table (name varchar(30), marks int )
insert into @table1 values ('a',65)
insert into @table1 values ('b',40)
insert into @table2 values ('c',25)
insert into @table2 values ('d',70)
**--query excluding marks<=40 from sum**
select sum(marks) as 'sum' ,max(marks) as 'max' from
(
select * from @table1 where marks>40
union
select * from @table2 where marks>40
) table_temp
**--query showing sum, only if sum > 40**
select sum(marks) as 'sum' ,max(marks) as 'max' from
(
select * from @table1
union
select * from @table2
) table_temp having sum(marks) > 40
2条答案
按热度按时间hrysbysz1#
使用union all组合两个表,然后应用聚合
0g0grzrc2#