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 are the benefits of triggers?

591


How do I save a stored procedure?

515


Why do we use procedures?

508


what is bcp? When does it used? : Sql dba

511


What are tables and fields in the database?

547






How to combine two stored procedures in sql?

587


Is not equal in sql?

560


Define SQL and state the differences between SQL and other conventional programming Languages?

676


Why procedure is used in sql?

526


What is lexical units in pl sql?

565


explain mysql aggregate functions. : Sql dba

541


what is an execution plan? When would you use it? How would you view the execution plan? : Sql dba

539


What is difference between sql and excel?

510


What are the key differences between SQL and PL SQL?

621


How can the performance of a trigger be improved?

588