• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using group by clause wih multiple tables

 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am trying to make a qurey for combining two tables using group by clause

tables are-

table 1-
department

deptid deptname
1 c
2 c++
3 java



employee
empid empsal deptid
1 1000 1
2 2000 2
3 3000 3
4 1000 1
5 2000 2
6 3000 3
7 1000 1
8 2000 2
9 3000 3
10 1000 1



now the query requires to make another table having two columns

deptname totalsal
c 4000
c++ 6000
java 9000


i'v tried a lot but not getting how to make this query. the query i made is

SELECT Department.DeptId,SUM(Employee.EmpSal) as TotalSal
FROM Department, Employee
GROUP BY Department.DeptId

but it is showing wrong result.

please help


 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your GROUP BY is OK, but you haven't told your query how to link Employees to Departments - you need a join condition. If you don't include a join condition, you will get every possible combination of Employee and Department in your results.
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using JOIN i got the correct query. thanks chris.
 
reply
    Bookmark Topic Watch Topic
  • New Topic