I have run into a very simple query. Since I rarely do SQL programming, I am asking this query. I have results returned from two queries. I need to get the aggregate of the second column. Sample query is as follows :create table emp1(name varchar(100), age int)create table emp2(name varchar(100), age int)insert into emp1values('Jacob',25)insert into emp1values('Jacob',75)I want the result to be :Name : Jacob Age : 100Please help.
Not really an aggregate query, except if there are multiple matches on the name. So if there's just one matching row between the two tables, all you need is a JOIN on the name column, and an expression that adds up the two columns. If there are multiple matches, and you need to add them all up, add SUM() around each column that you're adding up, and GROUP BY on the name column.
The process is simple. First Join these two tables and then sum Age of two employees. SELECT e1.NAME,SUM(e1.Age+ e2.Age)FROM #emp1 e1 JOIN #emp2 e2 ON e2.[NAME]=e1.[NAME] GROUPBY e1. [NAME]
Welcome to the forums.! Tanveer, always check the post date. This one is 4 years old. Anyway thank for your collaboration.