Home » SQL & PL/SQL » SQL & PL/SQL » need the output
need the output [message #620117] Tue, 29 July 2014 01:32 Go to next message
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 #620118 is a reply to message #620117] Tue, 29 July 2014 01:57 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Search for LISTAGG in this forum. You will get hundreds of examples.
Re: need the output [message #620119 is a reply to message #620117] Tue, 29 July 2014 02:27 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

Also always post your Oracle version, with 4 decimals.

[Updated on: Tue, 29 July 2014 02:28]

Report message to a moderator

Re: need the output [message #620120 is a reply to message #620119] Tue, 29 July 2014 02:30 Go to previous messageGo to next message
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 #620130 is a reply to message #620120] Tue, 29 July 2014 06:24 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
LISTAGG was introduced in 11g release 2. You might have to look for other options of string aggregation here http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php Try and post your query back here.

Please do not use wm_concat shown in that link as it is NOT documented. Any failure in your production system, Oracle won't provide any support.
Re: need the output [message #620136 is a reply to message #620120] Tue, 29 July 2014 06:52 Go to previous messageGo to next message
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 #620143 is a reply to message #620136] Tue, 29 July 2014 07:19 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
@OP, just thinking, why VARCHAR2 for player's score? Logically a score by a player as it seems in your table is a number.
Re: need the output [message #620193 is a reply to message #620136] Tue, 29 July 2014 23:16 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Thank you so much Michel.. Smile
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 #620194 is a reply to message #620143] Tue, 29 July 2014 23:18 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
@OP, just thinking, why VARCHAR2 for player's score? Logically a score by a player as it seems in your table is a number.

Yes you are right Lalit , wht is the next(future) issue's i will get , if i am not taking number data type for score column?
Re: need the output [message #620198 is a reply to message #620194] Wed, 30 July 2014 00:48 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
wht is the next(future) issue's i will get , if i am not taking number data type for score column?


Someone can enter 'A' or '*' or any character for score which is not a problem if you only want to get the list of scores but may if you want the average or sum of scores.

Re: need the output [message #620200 is a reply to message #620198] Wed, 30 July 2014 00:57 Go to previous messageGo to next message
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 #620202 is a reply to message #620200] Wed, 30 July 2014 01:01 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Remove leading comma with LTRIM.
Re: need the output [message #620204 is a reply to message #620202] Wed, 30 July 2014 01:05 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous message
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
Previous Topic: Convert SYSDATE to Julian Date format
Next Topic: Using REGEXP_SUBSTR
Goto Forum:
  


Current Time: Thu Apr 25 18:44:31 CDT 2024