Home » SQL & PL/SQL » SQL & PL/SQL » getting total and single name in one query (Oracle 10g)
getting total and single name in one query [message #599838] Tue, 29 October 2013 06:20 Go to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Hi All,

I want to get any employee name of deptno 10 but total count of number of employees under dept 10. Please help.


DECLARE
l_deptno NUMBER:=10;
l_count NUMBER;
l_ename varchar2(20);
BEGIN
SELECT count(*) OVER(order by empno) ,ename INTO l_count,l_ename FROM emp WHERE ROWNUM=1 and deptno=l_deptno;
dbms_output.put_line(l_count||' '||l_ename);
end;
Re: getting total and single name in one query [message #599839 is a reply to message #599838] Tue, 29 October 2013 06:25 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL> select ename, deptno, count(*) over (partition by deptno) nb
  2  from emp;
ENAME          DEPTNO         NB
---------- ---------- ----------
CLARK              10          3
KING               10          3
MILLER             10          3
JONES              20          5
FORD               20          5
ADAMS              20          5
SMITH              20          5
SCOTT              20          5
WARD               30          6
TURNER             30          6
ALLEN              30          6
JAMES              30          6
BLAKE              30          6
MARTIN             30          6

Re: getting total and single name in one query [message #599853 is a reply to message #599839] Tue, 29 October 2013 09:34 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Thanks Michel,

But how your example will fit in the above procedure.
Because dept count and ename are needed at same time.
When I put rownum=1 it is giving only dept count as 1 and ename is getting.
when I remove rownum then exact return more rows error getting.
Re: getting total and single name in one query [message #599857 is a reply to message #599853] Tue, 29 October 2013 09:56 Go to previous messageGo to next message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
sss111ind wrote on Tue, 29 October 2013 15:34
Thanks Michel,

But how your example will fit in the above procedure.
Because dept count and ename are needed at same time.
When I put rownum=1 it is giving only dept count as 1 and ename is getting.
when I remove rownum then exact return more rows error getting.

What is wrong with a single query as Michel demonstrated?

If you insist on using PL/SQL, what shall be the result set? How many rows shall it contain?
If it shall contain one row, just add and extra filter on that row (which one?) to the outer query over the Michel's one.
If it shall contain all rows, use cursor loop or (maybe better) bulk collect them into the collection variable.

As DBMS_OUTPUT is not destined to "get" the result set (it just fills the internal buffer) and I have no idea what is the result set used for, it is impossible for me to propose anything.
Re: getting total and single name in one query [message #599858 is a reply to message #599853] Tue, 29 October 2013 09:56 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
So you want 1 ename and don't care which it is?
SELECT count(*), min(ename) FROM emp
Re: getting total and single name in one query [message #599861 is a reply to message #599858] Tue, 29 October 2013 10:25 Go to previous message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

That is very correct thing which CookieMonster suggested and really a good catch . And yes I tried with analytical so that was my mistake.

Nice work guys thanks.

[Updated on: Tue, 29 October 2013 10:26]

Report message to a moderator

Previous Topic: Concatenating address fields
Next Topic: Pagination Query
Goto Forum:
  


Current Time: Thu Apr 18 08:37:07 CDT 2024