Home » SQL & PL/SQL » SQL & PL/SQL » need the output
need the output [message #620117] |
Tue, 29 July 2014 01:32 |
|
mist598
Messages: 1195 Registered: February 2013 Location: Hyderabad
|
Senior Member |
|
|
Hi all,
I have one table
Player Score
001 10
001 20
002 20
002 20
001 10
002 10
003 20
002 20
001 10
i want o/p in select statment
Player Score
001 10,20,10,10
002 20,20,10,20
003 20
Can you please help me
Thanks
|
|
|
|
|
Re: need the output [message #620120 is a reply to message #620119] |
Tue, 29 July 2014 02:30 |
|
mist598
Messages: 1195 Registered: February 2013 Location: Hyderabad
|
Senior Member |
|
|
Hi ,
Oracle version 11.1.0.7.0
create table t1 (Player varchar2(10),score varchar2(10))
insert into t1 values('001','10')
insert into t1 values('001','20')
insert into t1 values('002','20')
insert into t1 values('002','20')
insert into t1 values('001','10')
insert into t1 values('002','10')
insert into t1 values('003','20')
insert into t1 values('002','20')
insert into t1 values('001','10')
select * from t1
|
|
|
|
Re: need the output [message #620136 is a reply to message #620120] |
Tue, 29 July 2014 06:52 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Why your statements does not end by a ";" or "/"?
Why there are so many empty lines?
Why not post a working test we just have to copy and paste in your SQL*Plus session, like:
drop table t1;
create table t1 (Player varchar2(10),score varchar2(10));
insert into t1 values('001','10');
insert into t1 values('001','20');
insert into t1 values('002','20');
insert into t1 values('002','20');
insert into t1 values('001','10');
insert into t1 values('002','10');
insert into t1 values('003','20');
insert into t1 values('002','20');
insert into t1 values('001','10');
commit;
SQL> with
2 data as (
3 select player, score, row_number() over (partition by player order by null) rn
4 from t1
5 )
6 select player,
7 substr(sys_connect_by_path(score,','),2) scores
8 from data
9 where connect_by_isleaf = 1
10 connect by prior player = player and prior rn = rn - 1
11 start with rn = 1
12 /
PLAYER SCORES
---------- --------------------------------------------------
001 10,20,10,10
002 20,10,20,20
003 20
And why not choose a meaningful title other than "need the output "?
[Updated on: Tue, 29 July 2014 06:53] Report message to a moderator
|
|
|
|
Re: need the output [message #620193 is a reply to message #620136] |
Tue, 29 July 2014 23:16 |
|
mist598
Messages: 1195 Registered: February 2013 Location: Hyderabad
|
Senior Member |
|
|
Thank you so much Michel..
Quote:
Why your statements does not end by a ";" or "/"?
Why there are so many empty lines?
Why not post a working test we just have to copy and paste in your SQL*Plus session, like:
Sorry for that is my mistake.
Quote:
And why not choose a meaningful title other than "need the output "?
Yes, but i didn't get any meaningful title, can you please what is the Tittle for this please don't mind.
[Updated on: Tue, 29 July 2014 23:17] Report message to a moderator
|
|
|
|
|
Re: need the output [message #620200 is a reply to message #620198] |
Wed, 30 July 2014 00:57 |
|
mist598
Messages: 1195 Registered: February 2013 Location: Hyderabad
|
Senior Member |
|
|
Hi Michel,
I tried in the Function,
SQL> CREATE OR REPLACE FUNCTION f2 (p_deptno NUMBER)
2 RETURN VARCHAR2
3 IS
4 a VARCHAR2 (100);
5 BEGIN
6 FOR i IN (SELECT ename
7 FROM emp
8 WHERE deptno = p_deptno)
9 LOOP
10 a := a || ',' || i.ename;
11 END LOOP;
12 RETURN a;
13 END;
14 /
Function created.
SQL> select deptno,f2(deptno) from emp
2 group by deptno
3 /
DEPTNO F2(DEPTNO)
------- ----------------------------------
30 ,BLAKE,ALLEN,WARD,MARTIN,TURNER,JAMES
20 ,JONES,SCOTT,FORD,SMITH,ADAMS
10 ,KING,CLARK,MILLER
Used the same functionality in my Palyer and score..
SQL> CREATE OR REPLACE FUNCTION f4 (p_PLAYER VARCHAR2)
2 RETURN VARCHAR2
3 IS
4 a VARCHAR2 (100);
5 BEGIN
6 FOR i IN (SELECT SCORE
7 FROM t1
8 WHERE PLAYER = p_PLAYER)
9 LOOP
10 a := a || ',' || i.SCORE;
11 END LOOP;
12 RETURN a;
13 END;
14 /
Function created.
SQL>
SQL> SELECT player, f4 (player) score
2 FROM t1
3 GROUP BY player
4 ORDER BY player
5 /
PLAYER SCORE
------- ---------------
001 [b],20,10,10[/b]
002 ,20,20,10,20
003 ,20
I need
Player Score
------ ---------
001 [b] 10,20,10,10[/b]
002 20,20,10,20
003 20
Please suggest if i did any wrong.
Thanks
|
|
|
|
Re: need the output [message #620204 is a reply to message #620202] |
Wed, 30 July 2014 01:05 |
|
mist598
Messages: 1195 Registered: February 2013 Location: Hyderabad
|
Senior Member |
|
|
Quote:Remove leading comma with LTRIM.
Yes Thanks Littlefoot, And please check the below output's I am not getting proper output
Function f4 output
PLAYER SCORE
------- ----------
001 ,20,10,10 --->This!!
002 ,20,20,10,20
003 ,20
Needed out put
Player Score
------ ---------
001 10,20,10,10 --->This!!
002 20,20,10,20
003 20
|
|
|
Re: need the output [message #620205 is a reply to message #620200] |
Wed, 30 July 2014 01:10 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
I do not retrieve the same thing than you:
SQL> SELECT player, f4 (player) score FROM t1
2 GROUP BY player
3 ORDER BY player
4 /
PLAYER SCORE
---------- ------------------------------
001 ,10,20,10,10
002 ,20,20,10,20
003 ,20
Check the content of your table.
|
|
|
Re: need the output [message #620207 is a reply to message #620202] |
Wed, 30 July 2014 01:12 |
|
mist598
Messages: 1195 Registered: February 2013 Location: Hyderabad
|
Senior Member |
|
|
I have used LTRIM removing leading comma,but at the last row getting with comma.
SQL> CREATE OR REPLACE FUNCTION f4 (p_PLAYER VARCHAR2)
2 RETURN VARCHAR2
3 IS
4 a VARCHAR2 (100);
5 BEGIN
6 FOR i IN (SELECT SCORE
7 FROM t1
8 WHERE PLAYER = p_PLAYER)
9 LOOP
10 a := LTRIM(a,',')|| ',' || i.SCORE;
11 END LOOP;
12 RETURN a;
13 END;
14 /
Function created.
SQL> SELECT player, f4 (player) score
2 FROM t1
3 GROUP BY player
4 ORDER BY player
5 /
PLAYER SCORE
------ --------------
001 20,10,10
002 20,20,10,20
003 ,20 ---->This One!!!!!
|
|
|
Re: need the output [message #620208 is a reply to message #620200] |
Wed, 30 July 2014 01:17 |
martijn
Messages: 286 Registered: December 2006 Location: Netherlands
|
Senior Member |
|
|
(2014-07-30 08:11:54) bla@bodb >select * from t1;
PLAYER SCORE
---------- ----------------------------------------------------------------------------------------------------
001 10
001 20
002 20
002 20
001 10
002 10
003 20
002 20
001 10
9 rijen zijn geselecteerd.
Verstreken: 00:00:00.06
(2014-07-30 08:11:58) bla@bodb >select player, f4(player) score from t1 group by player order by player;
PLAYER SCORE
---------- ----------------------------------------------------------------------------------------------------
001 ,10,20,10,10
002 ,20,20,10,20
003 ,20
Verstreken: 00:00:00.03
(2014-07-30 08:12:04) bla@bodb >
Based on the input (inserted rows) you supplied earlier I would say the result is correct.
If the "," at the start of the output concerns you, maybe you can 'program' around it.
pseudo-code:
if a is empty
then a = 'new value'
else a= a||','||'new value'
end if;
|
|
|
Re: need the output [message #620210 is a reply to message #620208] |
Wed, 30 July 2014 01:41 |
|
mist598
Messages: 1195 Registered: February 2013 Location: Hyderabad
|
Senior Member |
|
|
Thanks Martijin..now perfect.
CREATE OR REPLACE FUNCTION f4 (p_player VARCHAR2)
RETURN VARCHAR2
IS
a VARCHAR2 (100);
BEGIN
FOR i IN (SELECT score
FROM t1
WHERE player = p_player)
LOOP
--a := LTRIM(a,',')|| ',' || i.SCORE;
IF a IS NULL
THEN
a := i.score;
ELSE
a := LTRIM (a, ',') || ',' || i.score;
END IF;
END LOOP;
RETURN a;
END;
SELECT player, f4 (player) score
FROM t1
GROUP BY player
ORDER BY player
|
|
|
Goto Forum:
Current Time: Thu Apr 25 18:44:31 CDT 2024
|