SQL Server Performance

Problem with doing a group by

Discussion in 'SQL Server 2005 General Developer Questions' started by starwarsbigbang, Dec 7, 2008.

  1. starwarsbigbang New Member

    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.
  2. Adriaan New Member

    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.
  3. Tanveer Ahmad New Member

    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]
  4. Luis Martin Moderator

    Welcome to the forums.!
    Tanveer, always check the post date. This one is 4 years old.
    Anyway thank for your collaboration.:)
  5. Tanveer Ahmad New Member

    when i answer then i saw the reported date.
    so anyway will tell me the problem doing with group by ?:confused:

Share This Page