Найти сумму и максимум столбца из объединения двух таблиц одной и той же схемы в SQL - PullRequest
0 голосов
/ 31 августа 2018

ТАБЛИЦА 1

Name Marks
a    65
b    40

ТАБЛИЦА 2

Name Marks
c    25
b    70

СУММА будет (65 + 40 + 25 + 70) Макс будет 70

Ответы [ 2 ]

0 голосов
/ 31 августа 2018
--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
0 голосов
/ 31 августа 2018

Используйте объединение всех, чтобы объединить две таблицы, а затем применить агрегирование

   select sum(marks), max(marks)
    from
    (select * from table1
    union all
    select * from table2)a
where marks>40
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...