473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Display all columns of a table using group by

Hi
i have the table employee with columns emp_id, emp_name, salary, dep_id and the another table department with columns dep_id, dep_name, manager.
dep_id is the foreign key for employee table.
i want the query for retrieving emp_id, salary, dep_id i.e. the employee who got maximum salary in each department using group by
Nov 16 '10 #1
4 2629
debasisdas
8,127 Expert 4TB
Kindly post the query that you are working on.
Nov 16 '10 #2
amitpatel66
2,367 Expert 2GB
How about using analytical function ROW_NUMBER()
Nov 16 '10 #3
try this..
Expand|Select|Wrap|Line Numbers
  1. create table employee_test (emp_id int, emp_name varchar(50), salary numeric(10,0), dep_id int )
  2. /
  3.  
  4. create table department_test ( dep_id int, dep_name varchar(50), manager varchar(50))
  5. /
  6.  
  7. insert into employee_test values(1,'san',100,1)
  8. insert into employee_test values(2,'san1',50,2)
  9. insert into employee_test values(3,'san2',250,1)
  10. insert into employee_test values(4,'san3',350,3)
  11. insert into employee_test values(5,'san4',900,1)
  12. insert into employee_test values(6,'san5',25,3)
  13. insert into employee_test values(7,'san6',560,1)
  14. insert into employee_test values(8,'san7',11,2)
  15. insert into employee_test values(9,'san44',900,1)
  16. insert into department_test values (1,'dep1','rr')
  17. insert into department_test values(2,'dep2','rr')
  18. insert into department_test values(3,'dep3','rr')
  19.  
  20. /
  21.  
  22. select cur.dep_ID,cur.salary,emp1.emp_name from (
  23. select dep.dep_ID,max(salary) as salary from employee_test emp inner join
  24. department_test dep on dep.dep_id=emp.dep_id
  25. group by dep.dep_ID) cur
  26. left join employee_test emp1 
  27. on emp1.salary =cur.salary
  28. /
  29.  
~~~ Enjoy Knowledge Freedom ~~~
Nov 16 '10 #4
You can use analytical function for this
Expand|Select|Wrap|Line Numbers
  1.  create table employee_test (emp_id int, emp_name varchar(50), salary numeric(10,0), dep_id int )
  2.  /
  3.  
  4.  create table department_test ( dep_id int, dep_name varchar(50), manager varchar(50))
  5.  /
  6.  
  7.  insert into employee_test values(1,'san',100,1)
  8.  insert into employee_test values(2,'san1',50,2)
  9.  insert into employee_test values(3,'san2',250,1)
  10.  insert into employee_test values(4,'san3',350,3)
  11.  insert into employee_test values(5,'san4',900,1)
  12.  insert into employee_test values(6,'san5',25,3)
  13.  insert into employee_test values(7,'san6',560,1)
  14.  insert into employee_test values(8,'san7',11,2)
  15.  insert into employee_test values(9,'san44',900,1)
  16.  insert into department_test values (1,'dep1','rr')
  17.  insert into department_test values(2,'dep2','rr')
  18.  insert into department_test values(3,'dep3','rr')
  19.  
  20.  /
  21.  
  22. select e.emp_id,e.dep_id,max(salary) over(partition by e.dep_id) from employee_test e;
  23.  
if you want to show the department name then only you need to join the two tables.
Nov 30 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Paul Littlefield | last post by:
Hi How do I select from a single table using 2 columns (surname,postcode) as the unique criteria? e.g. "Surname","Forename","Salutation","Postcode" "Bear","Daddy","Mr","CT10 7AF"...
4
by: Andre Arpin | last post by:
I am new at sql so would appreciate some help I have the name of a table in alocal variable is it possible to select this table DECLARE @name sysname SET @name = 'tblSniffedItems' PRINT...
5
by: Nirmala123 | last post by:
hi... I want to sort the table using combobox values. I give the code here. address.html: <html> <head> <title>Add a new entry</title> </head>
0
by: bimeldip | last post by:
Hi, I have managed to create codes to display data from the database in a html page.I have gone on to create a page to allow users to manipulate the table via a html page. For instance users will be...
1
by: Ankit | last post by:
Hi guys i need to make a table to store a certain data using Tkinter..I have searched on the group but i have not been able to find a solution that would work for me..The thing is that i want my...
0
by: anureddy | last post by:
help me how to add datagridview columns to another table,using windowsapplications. and set the displaymember and value member datagridviewcomboboxcolumn. i used this below code but that not...
0
by: cb123 | last post by:
Hello, Please bear with me I'm new to t-sql. I've had no luck trying to figure this out yet using sql code. I need to use an existing table (data dictionary) as input for creating a new table. ...
1
by: jelumalai | last post by:
I am using display:none with using Table. When show the onClick using javascript. Then it will show, again i will hide, content only hidden, but that table doesn't hide. <script> function...
3
by: prao2005 | last post by:
Q) How to display all the records of a table using xsl? Solution applied --> Suppose a class "test" has an attribute "test1" which itself is a table. "test1" contains some a1, a2, a3...
5
by: thatcollegeguy | last post by:
Below are my 3php and 2js files. I create a table using ajax/php and then want to change the values in the tables add(+ number for teamid) id's for each specific td in the table. I don't know...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.