how can we write a column values horizontal by using sql stmt;
ex:
select name from table_name;(actual output)
a
b
c
d
require output is
a b c d

Answer Posted / pankaj goyal

SQL wm_concat function
Question: I have a table test_test and I need to count the
distinct mark columns and them display all matching values
on one line:

Name Mark
------- ------
ABC 10
DEF 10
GHI 10
JKL 20
MNO 20
PQR 30

The result should be like this, with the count and the rows
groups onto the same line;

mark count names
---- ----- -----------
10 3 ABC,DEF,GHI
20 2 JKL,MNO
30 1 PQR




Answer: By Laurent Schneider: You could write your own
aggregate function or use WM_CONCAT:

select
mark,
count(*),
wm_concat(name)
from
test_test
group by
mark;

Here is another example of using wm_contcat:

select
deptno,
wm_concat(distinct ename)
from
emp
group by
deptno;


DEPTNO WM_CONCAT(DISTINCTENAME)
---------- ----------------------------------------
10 CLARK,KING,MILLER
20 ADAMS,FORD,JONES,SCOTT,SMITH
30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD

Is This Answer Correct ?    9 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is over () in sql?

529


What is schema in sql example?

599


What is clause?

612


Can I join the same table twice?

526


What are sql functions? Describe the different types of sql functions?

563






How do I audit the sql sent to the server?

526


How can we connect an Android App to an Oracle database and use the PL/SQL procedural code?

586


how to install mysql? : Sql dba

580


what are the differences between procedure-oriented languages and object-oriented languages? : Sql dba

510


How many rows can sqlite handle?

587


What is the difference between a procedure and a function?

494


how to use myisamchk to check or repair myisam tables? : Sql dba

509


How consistent is the view of the data between and within multiple sessions, transactions or statements ?

1712


How many triggers can be applied on a table?

515


What is data control language?

532