Creating Comma Seperated Lists

From TekiWiki

Jump to: navigation, search

This whole wiki has now moved to http://www.springboardsolutions.com/wiki

Go to this page Creating Comma Seperated Lists

 
Line 1: Line 1:
== Comma Seperated Column from a List of Rows ==
== Comma Seperated Column from a List of Rows ==
-
If you have a table that contains groups of lists - such as the list of columns in tables - and you want to list the columns against their table name on one line rather than across several - such as:
+
If you have a table in an [[Oracle Database]] that contains groups of lists - such as the list of columns in tables - and you want to list the columns against their table name on one line rather than across several - such as:
{| class="wikitable"
{| class="wikitable"
Line 29: Line 29:
)
)
start with rn = 1
start with rn = 1
-
connect by prior rn = rn-1
+
connect by prior rn+1 = rn
and prior table_name=table_name
and prior table_name=table_name
and prior owner=owner
and prior owner=owner
group by owner,table_name
group by owner,table_name
order by owner,table_name
order by owner,table_name
 +
 +
OWNER TABLE_NAME                  COLUMNS
 +
----- ---------------------------- -------------------------------------------------------------------
 +
SYS  DBA_TABLESPACE_GROUPS        GROUP_NAME,TABLESPACE_NAME
 +
SYS  DBA_TABLESPACE_USAGE_METRICS TABLESPACE_NAME,USED_SPACE,TABLESPACE_SIZE,USED_PERCENT
 +
SYS  DBA_TAB_COMMENTS            OWNER,TABLE_NAME,TABLE_TYPE,COMMENTS
 +
SYS  DBA_TAB_PRIVS                GRANTEE,OWNER,TABLE_NAME,GRANTOR,PRIVILEGE,GRANTABLE,HIERARCHY
 +
SYS  DBA_TAB_STATS_HISTORY        OWNER,TABLE_NAME,PARTITION_NAME,SUBPARTITION_NAME,STATS_UPDATE_TIME
 +
SYS  DBA_TAB_STAT_PREFS          OWNER,TABLE_NAME,PREFERENCE_NAME,PREFERENCE_VALUE
 +
</pre>
</pre>
 +
 +
...and this one puts the columns in alphabetical order:
...and this one puts the columns in alphabetical order:
<pre>
<pre>
select owner,table_name,max(ltrim(sys_connect_by_path(column_name,','),',')) Columns
select owner,table_name,max(ltrim(sys_connect_by_path(column_name,','),',')) Columns
from (
from (
-
select owner,table_name,column_name,row_number() over (partition by table_name order by column_id) rn
+
select owner,table_name,column_name,row_number() over (partition by table_name order by column_name) rn
from sys.dba_tab_columns t
from sys.dba_tab_columns t
)
)
start with rn = 1
start with rn = 1
-
connect by prior rn = rn-1
+
connect by prior rn+1 = rn
and prior table_name=table_name
and prior table_name=table_name
and prior owner=owner
and prior owner=owner
group by owner,table_name
group by owner,table_name
order by owner,table_name
order by owner,table_name
 +
 +
OWNER TABLE_NAME                  COLUMNS
 +
----- ---------------------------- -------------------------------------------------------------------
 +
SYS  DBA_TABLESPACE_GROUPS        GROUP_NAME,TABLESPACE_NAME
 +
SYS  DBA_TABLESPACE_USAGE_METRICS TABLESPACE_NAME,TABLESPACE_SIZE,USED_PERCENT,USED_SPACE
 +
SYS  DBA_TAB_COMMENTS            COMMENTS,OWNER,TABLE_NAME,TABLE_TYPE
 +
SYS  DBA_TAB_PRIVS                GRANTABLE,GRANTEE,GRANTOR,HIERARCHY,OWNER,PRIVILEGE,TABLE_NAME
 +
SYS  DBA_TAB_STATS_HISTORY        OWNER,PARTITION_NAME,STATS_UPDATE_TIME,SUBPARTITION_NAME,TABLE_NAME
 +
SYS  DBA_TAB_STAT_PREFS          OWNER,PREFERENCE_NAME,PREFERENCE_VALUE,TABLE_NAME
 +
</pre>
</pre>
== Query Results into Comma Separated Values ==
== Query Results into Comma Separated Values ==
-
In SQL Plus, a CSV file can be created from the results of a query by chnaging the column separation (colsep):
+
In SQL Plus, a CSV file can be created from the results of a query by changing the column separation (colsep):
<pre>
<pre>
Line 64: Line 86:
set colsep " "
set colsep " "
 +
 +
CO,NAME
 +
--,---------------------
 +
AF,AFGHANISTAN
 +
AL,ALBANIA
 +
DZ,ALGERIA
 +
AS,AMERICAN SAMOA
 +
AD,ANDORRA
 +
AO,ANGOLA
 +
AI,ANGUILLA
 +
AQ,ANTARCTICA
 +
AG,ANTIGUA AND BARBUDA
 +
</pre>
</pre>

Latest revision as of 20:00, 18 June 2013

Personal tools