Retrieving First element of a Group By Item [message #286713] |
Sun, 09 December 2007 23:53 |
saifurshaon
Messages: 29 Registered: December 2007 Location: Dhaka
|
Junior Member |
|
|
how to select first row from each group.
--- use hr/hr
select e.employee_id,e.first_name,d.department_id
from employees e,departments d
where e.department_id=d.department_id
order by d.department_id asc
returns
EMPLOYEE_ID FIRST_NAME DEPARTMENT_ID
----------- -------------------- -------------
200 Jennifer 10
201 Michael 20
202 Pat 20
114 Den 30
115 Alexander 30
116 Shelli 30
119 Karen 30
118 Guy 30
117 Sigal 30
203 Susan 40
120 Matthew 50
121 Adam 50
123 Shanta 50
132 TJ 50
131 James 50
130 Mozhe 50
129 Laura 50
128 Steven 50
127 James 50
. . . bla bla bla . . .
my expected result set is some thing like this one
EMPLOYEE_ID FIRST_NAME DEPARTMENT_ID
----------- -------------------- -------------
200 Jennifer 10
201 Michael 20
114 Den 30
203 Susan 40
120 Matthew 50
. . . bla bla bla . . .
how to do it ?
_________________
Thanks,
|
|
|
|
|
|
|
|
|
|
|
|
Re: Retrieving First element of a Group By Item [message #286769 is a reply to message #286745] |
Mon, 10 December 2007 01:45 |
saifurshaon
Messages: 29 Registered: December 2007 Location: Dhaka
|
Junior Member |
|
|
i am trying to solve it without using analytical function just for my curiousity. There is no other thing beyond this.
If it is solved without using analytical fn then it could be used in other rdbms. Isn't it?
Dhananjay, would u like to give the full query rather than hints?
|
|
|
|
|
|
|
|
|
Re: Retrieving First element of a Group By Item [message #286947 is a reply to message #286945] |
Mon, 10 December 2007 11:23 |
S.Rajaram
Messages: 1027 Registered: October 2006 Location: United Kingdom
|
Senior Member |
|
|
This forums has helped other fellow forums members in a lot of different ways. I am member of many other forums but i find this forum very good because they make do things by which you learn a lot and if you are stuck they will give you hints to try to solve your problem rather than spoon feeding like what I am going to do now. Because by spoon feeding it will solve your problem for now but you will still be dependent on others which I personally feel a huge bottleneck.
SQL> select deptno, empno, ename from emp;
DEPTNO EMPNO ENAME
---------- ---------- ----------
20 7369 SMITH
30 7499 ALLEN
30 7521 WARD
20 7566 JONES
30 7654 MARTIN
30 7698 BLAKE
10 7782 CLARK
20 7788 SCOTT
10 7839 KING
30 7844 TURNER
20 7876 ADAMS
DEPTNO EMPNO ENAME
---------- ---------- ----------
30 7900 JAMES
20 7902 FORD
10 7934 MILLER
SQL> select empno, ename, deptno from emp where empno in (select min(empno) from emp group by deptno
);
EMPNO ENAME DEPTNO
---------- ---------- ----------
7499 ALLEN 30
7369 SMITH 20
7782 CLARK 10
I think this is what @dhananjay meant. I think it is time for you to read the oracle reference manual. It has loads of valuable information. Read it, try it , google it and still if you are stuck come back here by all means. Believe me, if you chose to ignore only you will be the loser and not us. Because it's not everybody are ready to share the knowledge but this forum is not like that. There are loads of people here who are extremely knowledgeable and not only that they are ready to share their knowledge and expertise which you can't get it any books.
O.k enough lecturing and do some real work.
Happy learning
Regards
Raj
|
|
|
Re: Retrieving First element of a Group By Item [message #286948 is a reply to message #286713] |
Mon, 10 December 2007 11:47 |
saifurshaon
Messages: 29 Registered: December 2007 Location: Dhaka
|
Junior Member |
|
|
dear raj
thanks for ur nice reply. I am really grateful to u. Before ur reply what happended u just see
Quote: | 'Would you like to solve it for your curiosity or would like we solve it for you?'
|
Quote: | Ok. Still seems odd to me but as an academic exercise I'll tag along. What do we have so far, Saifur Rashid Shaon?
* You cannot use analytic functions because - according to your classmate dhananjay - your teacher told you so.
* You can use aggregates (MIN() + GROUP BY).
What's your next move?
|
Look the difference between ur post and these posts. R they encouraging??
Anyways thanks again for ur helpful advice.
Regards
|
|
|
|
|
|
|
|
|
Re: Retrieving First element of a Group By Item [message #287265 is a reply to message #287252] |
Tue, 11 December 2007 14:27 |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
saifurshaon wrote on Tue, 11 December 2007 18:20 | Thanks to Littlefoot and Maheer.
|
Who is Maheer? There was plenty more I said after this, but I deleted it before I posted. @saifurshaon Please have the common courtesy to spell people's 'names' correctly. Not to do so, is simply rude.
|
|
|