Convert Multiple Rows in a single Comma Seprated value
Is is possible to convert miltiple rows in a single value by concatenating each row value in OBIEE. I think OI had seem a blog post relted to this by I'm not able to to find it again. Basically Here is what I would like to do:
Customer number Location
101 IL
101 NY
101 PA
102 TX
102 CA
Convert this to
101 IL, NY, PA
102 TX,CA
Can someone please let me know if this is possible and how to make it work.
Thanks you for the help in advance.
You can do this by creating a DB function,
Check this link
Re: Horizontal value display
Thanks,
Vino
Similar Messages
-
SQL Query to convert multiple rows to a single row output
I have the following sql query;
SELECT B.ClientID,
B.BillNo, B.[BillDate],
B.CurrencyCode,
B.BAmount
FROM tblCompanyDetails CD
CROSS
APPLY (
SELECT TOP 5 B1.ClientID,
B1.[BillNumber],
B1.[BillDate], B1.[CurrencyCode] AS CCY,
B1.[BillAmount]
FROM tblBills B1
WHERE ROUND(B1.[BillAmount],2)<>0
AND B1.ClientID=CD.CompanyID
AS B
WHERE CD.ContactId=235405
ORDER
BY B.ClientID,B.BillNo
DESC
This gives me the following row output;
ClientID
BillNo
BillDate
CurrencyCode
BAmount
94466
425616
2015-04-07
GBP
5000
94466
424517
2015-01-15
GBP
6000
94466
424455
2014-10-15
GBP
4000
However, I would like the ouput to appear like this;
ClientID
BillNo1
BillDate
CCY
BAmount
BillNo2
BillDate
CCY
BAmount
BillNo3
BillDate
CCY
BAmount
94466
425616
2015-04-07
GBP
5000
424517
2015-01-15
GBP
6000
424455
2014-10-15
GBP
4000I have tried to create a sample based on your question
CREATE TABLE MyTable
ClientID INT ,
BillNo INT,
BillDate DATE,
CurrencyCode varchar(5),
BAmount INT
INSERT INTO MyTable
VALUES (94466,425616,'2015-04-07','GBP',5000),
(94466,424517,'2015-01-15','GBP',6000),
(94466,424455,'2014-10-15','GBP',4000)
Then I have used another table to put ranking in that, you can actually tweak your statement to include rank also
CREATE TABLE RowTable
ClientID INT ,
BillNo INT,
BillDate DATE,
CurrencyCode varchar(5),
BAmount INT,
Ranking INT
INSERT INTO RowTable
SELECT * , ROW_NUMBER() OVER (ORDER BY ClientID) as row
FROM MyTable
Once your data is ready then execute below dynamic query
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(col+cast(Ranking as varchar(10)))
from RowTable
cross apply
select 'BillNo', 1 union all
select 'BillDate', 2 union all
select 'CurrencyCode', 3 union all
select 'BAmount' , 4
) c (col, so)
group by col, so, Ranking
order by Ranking, so
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT ClientId,' + @cols + '
from
select clientid, col+cast(Ranking as varchar(10)) col, value
from RowTable
cross apply
SELECT ''BillNo'', cast(BillNo as varchar(20)) union all
SELECT ''BillDate'', cast(BillDate as varchar(20)) union all
SELECT ''CurrencyCode'', CurrencyCode union all
SELECT ''BAmount'', CAST(BAmount as varchar(10))
) c (col, value)
) x
pivot
max(value)
for col in (' + @cols + ')
) p '
execute sp_executesql @query;
Output:
Hope this will help. -
Convert multiple rows into a single row
Hi friends..
I have a table with the following information
SQL> select * from tsting;
A B C D E
10
10
10
29-MAY-09
TESTBut I need the following result..
A B C D E
10 10 10 29-MAY-09 TESTHello,
Try:
WITH test_data AS (
SELECT 10 A, NULL B, NULL C, NULL D, NULL E FROM DUAL UNION ALL
SELECT NULL A, 10 B, NULL C, NULL D, NULL E FROM DUAL UNION ALL
SELECT NULL A, NULL B, 10 C, NULL D, NULL E FROM DUAL UNION ALL
SELECT NULL A, NULL B, NULL C, TO_DATE('29-MAY-09','DD-MON-YY') D, NULL E FROM DUAL UNION ALL
SELECT NULL A, NULL B, NULL C, NULL D, 'TEST' E FROM DUAL)
-- end test data
SELECT MAX(A) A, MAX(B) B, MAX(C) C, MAX(D) D, MAX(E) E
FROM test_data;
A B C D E
10 10 10 29-MAY-09 TEST -
Convert multiple row field to single line
Firstly: Apologies I am not a developer and am learning how to use crystal by error and hardwork.. so I'm a little slow on the lingo and the uptake.
I am creating a report that has under "details" a field {jobTests.testTypeName} this currently finds all tests associated with the job and lists them one under another. i.e
(Job blah blah) Test1
Test3
Test7
Test9
Job info (blah blah) is displayed by a different field under the details tab, it is not associated.
Then it repeats with the next job until all jobs under a PO are completed, then it moves to the next PO and continues
To save space I need to list the Tests beside one another i.e Test1, Test 3, Test7, Test9.
I haven't been able to work out how to do this at all.
OR
If it is possible can anyone tell me how to take the list of Tests that are output and enter them under an IF THEN statement...
I can choose one TEST and rename (as shown below) but I need to be able to choose a group of specific tests i.e If Test1 + Test3 + Test7 + Test9 Then display "Group 1"
If {jobTests.testTypeName} = "TEST1" Then
"T1"
Else
{jobTests.testTypeName}
Thanks to anyone who can help. I hope my question makes sense.
Edited by: Kathryn Todd on Nov 11, 2008 4:01 PM fixed typoHi Kathryn,
If you haven't done so already, create a group on the Job.
In the Group Header for Job, create a formula like:
WhilePrintingRecords;
StringVar Tests := "";
In the Detail section, create a formula like:
WhilePrintingRecords;
StringVar Tests;
Tests := Tests & {jobTests.testTypeName} & " ";
Create a third formula in the Group Footer for the Job like:
WhilePrintingRecords;
StringVar Tests;
Here's what's happening. The first formula is in the Group Header to define and initialize the variable called Tests. The variable will be reset for each new Job otherwise you'd eventually get all the tests for all the jobs.
The second formula will create your string.
The last formula will display the string for each test.
Good luck,
Brian -
Need help converting multipe rows in a single row
I want to convert multiple rows into a single row. Oracle version is 10.2
SQL> with abc
2 as
3 (select 1 a, 2 b, 3 c from dual union
4 select 4, 5, 6 from dual)
5 select * from abc;
A B C
1 2 3
4 5 6I want the output as
A B C
1,4 2,5 3,6Thanks,
Gangadhar
Edited by: GR on Feb 6, 2012 9:35 PMGR wrote:
I want to convert multiple rows into a single row. Oracle version is 10.2
SQL> with abc
2 as
3 (select 1 a, 2 b, 3 c from dual union
4 select 4, 5, 6 from dual)
5 select * from abc;
A B C
1 2 3
4 5 6I want the output as
A B C
1,4 2,5 3,6
You haven't specified if there's a particular order to the data, so I've assumed one...
SQL> ed
Wrote file afiedt.buf
1 with t as (select 1 a, 2 b, 3 c from dual union
2 select 4, 5, 6 from dual)
3 --
4 -- end of test data
5 --
6 select trim(',' from sys_connect_by_path(a,',')) as a
7 ,trim(',' from sys_connect_by_path(b,',')) as b
8 ,trim(',' from sys_connect_by_path(c,',')) as c
9 from (select a,b,c,row_number() over (order by a) as rn from t)
10 where connect_by_isleaf = 1
11 connect by rn = prior rn+1
12* start with rn = 1
SQL> /
A B C
1,4 2,5 3,6
SQL> -
Convert multiple rows into single rows for the respective index name
Dear Experts,
I want to convert multiple rows into single rows for the respective index name,
Here is my query.
SELECT user_tables.table_name, user_indexes.index_name, user_ind_columns.column_name
FROM user_tables
JOIN user_indexes on user_indexes.table_name = user_tables.table_name
join USER_IND_COLUMNS on USER_INDEXES.INDEX_NAME = USER_IND_COLUMNS.INDEX_NAME
where user_indexes.index_name not like '%PK%' AND user_ind_columns.column_name NOT LIKE '%SYS%'
ORDER BY user_tables.table_name,user_indexes.index_name;
Result of previous query
TABLE_NAME
INDEX_NAME
COLUMN_NAME
T1
IDX_ACCNTYPCFG1
ENABLE_SERVICE
T1
IDX_ACCTTYPCFG1
ACC_CODE
T1
IDX_ACCTTYPCFG1
ACCTYPE
T2
IDX_ACCTTYPCFGAPP1
ACCTYPE
T3
IDX_ACTLG1
MOBILE_NO
T3
IDX_ACTLG1
ID
Desired output required is
TABLE_NAME
INDEX_NAME
COLUMN_NAME
T1
IDX_ACCNTYPCFG1
ENABLE_SERVICE,ACC_CODE,ACCTYPE
T2
IDX_ACCTTYPCFGAPP1
ACCTYPE
T3
IDX_ACTLG1
ACCTYPE,MOBILE_NO
please help.Maybe
with
user_tables as
(select 'T1' table_name,'IDX_ACCNTYPCFG1' index_name,'ENABLE_SERVICE' column_name from dual union all
select 'T1','IDX_ACCTTYPCFG1','ACC_CODE' from dual union all
select 'T1','IDX_ACCTTYPCFG1','ACCTYPE' from dual union all
select 'T2','IDX_ACCTTYPCFGAPP1','ACCTYPE' from dual union all
select 'T3','IDX_ACTLG1','MOBILE_NO' from dual union all
select 'T3','IDX_ACTLG1','ID' from dual
select table_name,
case index_name when 'IDX_ACCNTYPCFG1' then 'IDX_ACCTTYPCFG1' else index_name end index_name,
listagg(case column_name when 'ID' then 'ACCTYPE' else column_name end,',') within group (order by null) column_name
from user_tables
group by table_name,case index_name when 'IDX_ACCNTYPCFG1' then 'IDX_ACCTTYPCFG1' else index_name end
TABLE_NAME
INDEX_NAME
COLUMN_NAME
T1
IDX_ACCTTYPCFG1
ACCTYPE,ACC_CODE,ENABLE_SERVICE
T2
IDX_ACCTTYPCFGAPP1
ACCTYPE
T3
IDX_ACTLG1
ACCTYPE,MOBILE_NO
Regards
Etbin -
Multiple rows in a single row.
I want to know how can I store the values of multiple rowe in a single row.for an example..
machine name manchine no frequency month
Lathe MOO1 yearly jan
Lathe MOO1 halfyearly feb
Lathe MOO1 weekly march and so on
I want this in a single row..
like jan feb march
Lathe M001 yearly halfyearly weekly.
and in a single row I want this as per the month specified .
In my report I have 12 months distribution for each machine.Taruna,
Just make sure understand the scenario correctly. Would you like to store the row in DB like this:
Lathe MOO1 yearly jan,feb,mar
then you expect to show in the report like this:
Lathe MOO1 yearly jan
Lathe MOO1 yearly feb
Lathe MOO1 yearly mar
Yes, you can do it this way, but somehow break the 1st principle of DB schema design - Atomic
Of course, you need to convert jan,feb,mar into list and display for each row.
string monthStr = "jan,feb,mar";
string [] months = monthStr.Split(",");
Kind Regards
-Yatsea -
How To Concatenate Column Values from Multiple Rows into a Single Column?
How do I create a SQL query that will concatenate column values from multiple rows into a single column?
Last First Code
Lesand Danny 1
Lesand Danny 2
Lesand Danny 3
Benedi Eric 7
Benedi Eric 14
Result should look like:
Last First Codes
Lesand Danny 1,2,3
Benedi Eric 7,14
Thanks,
David JohnsonStarting with Oracle 9i
select last, first, substr(max(sys_connect_by_path(code,',')),2) codes
from
(select last, first, code, row_number() over(partition by last, first order by code) rn
from a)
connect by last = prior last and first = prior first and prior rn = rn -1
start with rn = 1
group by last, first
LAST FIRST CODES
Lesand Danny 1,2,3
Benedi Eric 7,14Regards
Dmytro -
How do I insert multiple rows from a single form ...
How do I insert multiple rows from a single form?
This form is organised by a table. (just as in an excel format)
I have 20 items on a form each row item has five field
+++++++++++ FORM AREA+++++++++++++++++++++++++++++++++++++++++++++++++++++
+Product| qty In | Qty Out | Balance | Date +
+------------------------------------------------------------------------+
+Item1 | textbox1 | textbox2 | textbox3 | date +
+ |value = $qty_in1|value= &qty_out1|value=$balance1|value=$date1 +
+------------------------------------------------------------------------+
+Item 2 | textbox1 | textbox2 | textbox4 | date +
+ |value = $qty_in2|value= $qty_out1|value=$balance2|value=$date2 +
+------------------------------------------------------------------------+
+ Item3 | textbox1 | textbox2 | textbox3 | date +
+------------------------------------------------------------------------+
+ contd | | | +
+------------------------------------------------------------------------+
+ item20| | | | +
+------------------------------------------------------------------------+
+ + + SUBMIT + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Database Structure
+++++++++++++++++
+ Stock_tabe +
+---------------+
+ refid +
+---------------+
+ item +
+---------------+
+ Qty In +
+---------------+
+ Qty Out +
+---------------+
+ Balance +
+---------------+
+ Date +
+++++++++++++++++
Let's say for example user have to the use the form to enter all 10 items or few like 5 on their stock form into 4 different textbox field each lines of your form, however these items go into a "Stock_table" under Single insert transaction query when submit button is pressed.
Please anyone help me out, on how to get this concept started.Hello,
I have a way to do this, but it would take some hand coding on your part. If you feel comfortable hand writing php code and doing manual database calls, specificaly database INSERT calls you should be fine.
Create a custom form using the ADDT Custom Form Wizard that has all the rows and fields you need. This may take a bit if you are adding the ability for up to 20 rows, as per your diagram of the form area. The nice thing about using ADDT to create the form is that you can setup the form validation at the same time. Leave the last step in the Custom Form Wizard blank. You can add a custom database call here, but I would leave it blank.
Next, under ADDT's Forms Server Behaviors, select Custom Trigger. At the Basic tab, you enter your custom php code that will be executed. Here you are going to want to put your code that will check if a value has been entered in the form and then do a database INSERT operation on the Stock_table with that row. The advanced tab lets you set the order of operations and the name of the Custom Trigger. By default, it is set to AFTER. This means that the Custom Trigger will get executed AFTER the form data is processed by the Custom Form Transaction.
I usually just enter TEST into the "Basic" tab of the Custom Trigger. Then set my order of operations in the "Advanced" tab and close the Custom Trigger. Then I go to the code view for that page in Dreamweaver and find the Custom Trigger function and edit the code manually. It's much easier this way because the Custom Trigger wizard does not show you formatting on the code, and you don't have to keep opening the Wizard to edit and test your code.
Your going to have to have the Custom Trigger fuction do a test on the submitted form data. If data is present, then INSERT into database. Here's a basic example of what you need to do:
In your code view, the Custom Trigger will look something like this:
function Trigger_Custom(&$tNG) {
if($tNG->getColumnValue("Item_1")) {
$item1 = $tNG->getColumnValue("Item_1");
$textbox1_1 = $tNG->getColumnValue("Textbox_1");
$textbox1_2 = $tNG->getColumnValue("Textbox_2");
$textbox1_3 = $tNG->getColumnValue("Textbox_3");
$date1 = $tNG->getColumnValue("Textbox_3");
$queryAdd = "INSERT INTO Stock_table
(item, Qty_In, Qty_Out, Balance, Date) VALUES($item1, $textbox1_1, $textbox1_2, $textbox1_3, $date1)"
$result = mysql_query($queryAdd) or die(mysql_error());
This code checks to see if the form input field named Item_1 is set. If so, then get the rest of the values for the first item and insert them into the database. You would need to do this for each row in your form. So the if you let the customer add 20 rows, you would need to check 20 times to see if the data is there or write the code so that it stops once it encounters an empty Item field. To exit a Custom Trigger, you can return NULL; and it will jump out of the function. You can also throw custom error message out of triggers, but this post is already way to long to get into that.
$tNG->getColumnValue("Item_1") is used to retrieve the value that was set by the form input field named Item_1. This field is named by the Custom Form Wizard when you create your form. You can see what all the input filed names are by looking in the code view for something like:
// Add columns
$customTransaction->addColumn("Item_1", "STRING_TYPE", "POST", "Item_1");
There will be one for each field you created with the Custom Form Wizard.
Unfortunately, I don't have an easy way to do what you need. Maybe there is a way, but since none of the experts have responded, I thought I would point you in a direction. You should read all you can about Custom Triggers in the ADDT documentation/help pdf to give you more detailed information about how Custom Triggers work.
Hope this helps.
Shane -
Inserting multiple rows using a single Insert statement without using dual
Hi all,
i am trying to insert multiple rows using a single insert statement like the below one.
The below one works fine..
But is there any other change that can be done in the below one without using dual...
insert all
into ps_hd_samp (num1,num2) values (1,1)
into ps_hd_samp (num1,num2) values (2,2)
into ps_hd_samp (num1,num2) values (3,3)
select 1 from dual;NiranjanSe wrote:
Hi all,
i am trying to insert multiple rows using a single insert statement like the below one.
The below one works fine..
But is there any other change that can be done in the below one without using dual...
insert all
into ps_hd_samp (num1,num2) values (1,1)
into ps_hd_samp (num1,num2) values (2,2)
into ps_hd_samp (num1,num2) values (3,3)
select 1 from dual;
SQL> create table ps_hd_samp (num1 number,num2 number);
Table created.
SQL> insert all
2 into ps_hd_samp (num1,num2) values (1,1)
3 into ps_hd_samp (num1,num2) values (2,2)
4 into ps_hd_samp (num1,num2) values (3,3)
5 select count(*)
6 from ps_hd_samp;
3 rows created.
SQL> select * from ps_hd_samp;
NUM1 NUM2
1 1
2 2
3 3 -
Multiple row as a single row in a column
Hi,
I want to select Multiple rows into a single line in 'Single Column Table' .
For ex:
Employee table has only one column , named as empname . it has three rows
Select empname from emp;
empname
thambi
peter
antony
My expected result: thambi,peter,antonyHow do I ask a question on the forums?
https://forums.oracle.com/forums/thread.jspa?threadID=2174552#9360002 -
Select or deselect multiple rows with one single selection event
Does anyone know how to create a JTable which can select or deselect multiple rows with one single selection event in JTable. Fore example, if the table has
row1
row2
row3
row4
row5
row6
What I need is when user select row1 or row2, both row1 and row2 should be set to be selected. Then if user press CTRL and click one of row3 or row4, both of them should be selected including the previouse selected row1 and row2.
For deselection, if row1 and row2 are selected, when users deselect one of row1 or row2, both of them should be deselected.
Hopefully someone can give me a hint.Here is a partial solution using a JList. Only one line gets highlighted when the user makes a selection in the list. But, two lines are returned. There is a blank line between every two lines.
private void addLineToList() {
String a = f_one.getText();
String b = f_two.getText();
if (a.length() == 0) {
Utils.showInformationMessage("Item field is empty.");
f_one.requestFocusInWindow();
return;
if (b.length() == 0) {
Utils.showInformationMessage("Match field is empty.");
f_two.requestFocusInWindow();
return;
model.addElement("item: " + a);
model.addElement("match: " + b);
model.addElement(" ");
int size = model.getSize();
pairList.setSelectedIndex(size - 3);
f_one.setText("");
f_two.setText("");
f_one.requestFocusInWindow();
private void editList() {
if (pairList.getSelectedValue().toString().equalsIgnoreCase(" ")) {
Toolkit.getDefaultToolkit().beep();
f_one.requestFocusInWindow();
return;
if (!f_one.getText().equals("")) {
int result = JOptionPane.showConfirmDialog(this,
"The Item field contains text. Replace the text?",
"Flash Card Activity", JOptionPane.YES_NO_OPTION,
OptionPane.INFORMATION_MESSAGE);
if (result == JOptionPane.NO_OPTION) return;
if (!f_two.getText().equals("")) {
int result = JOptionPane.showConfirmDialog(this,
"The Match field contains text. Replace the text?",
"Flash Card Activity", JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if (result == JOptionPane.NO_OPTION) return;
String item = "";
String match = "";
int index = pairList.getSelectedIndex();
String choice = model.getElementAt(index).toString();
if (choice.startsWith("item")) {
item = choice;
match = model.getElementAt(index + 1).toString();
model.remove(index);
model.remove(index);
model.remove(index);
else {
item = model.getElementAt(index - 1).toString();
match = choice;
model.remove(index + 1);
model.remove(index);
model.remove(index - 1);
int size = model.getSize();
if (size > 2) {
pairList.setSelectedIndex(size - 2);
f_one.setText(item.substring(6));
f_two.setText(match.substring(7));
f_one.requestFocusInWindow();
} -
Trying to convert multiple rows into multipe columns within a single row
I am trying to convert data from multiple rows into multiple columns. Let me see if I can paint the picture for you.
Here is a sample of the table i am trying to read from:
Company Name Account
1 Sam 123
1 Sam 234
1 Joe 345
1 Sue 789
1 Sue 987
1 Sue 573
I am trying to put this into a View that would have the data represented as such:
Company Name Acct1 Acct2 Acct3 Acct4
1 Sam 123 234 <null> <null>
1 Joe 345 <null> <null> <null>
1 Sue 789 987 573 <null>
Many thanks in advance for your help!test@XE> --
test@XE> with t as (
2 select 1 as company, 'Sam' as name, 123 as account from dual union all
3 select 1, 'Sam', 234 from dual union all
4 select 1, 'Joe', 345 from dual union all
5 select 1, 'Sue', 789 from dual union all
6 select 1, 'Sue', 987 from dual union all
7 select 1, 'Sue', 573 from dual)
8 --
9 select company,
10 name,
11 max(case when rn = 1 then account else null end) as acct1,
12 max(case when rn = 2 then account else null end) as acct2,
13 max(case when rn = 3 then account else null end) as acct3,
14 max(case when rn = 4 then account else null end) as acct4
15 from (select company,
16 name,
17 account,
18 row_number() over (partition by company, name order by 1) as rn
19 from t)
20 group by company, name;
COMPANY NAM ACCT1 ACCT2 ACCT3 ACCT4
1 Joe 345
1 Sam 234 123
1 Sue 573 789 987
3 rows selected.
test@XE>
test@XE>isotope -
Multiple rows Converge to Single row and join
Hi Folks,
I am facing a tricky challenge to join a table with multiple rows and converge into a single row (based on ID, period) and join with another table to get a single row. Let me explain.
Table 1: DTL_TABLE (id, period, course, names, title, type)
1 2010 mat john null null
1 2010 mat jim null null
1 2010 cam null officer null
1 2010 cam null Prof null
1 2010 phy null null Inclass
1 2010 phy null null Online
Join with
Table 2: ID_TABLE(id, period, Loc, Dept, Code)
1 2010 nj 101 CC.
Output format (id, period, course, names, title, type, Loc, Dept, Code)
result : 1 2010 mat,cam,phy john,jim officer,prof inclass,online nj 101 CC
I have created all the DDLs and DMLs if that help. Kindly let me know if a Join SQL query is possible.
Thank you,
Aj
CREATE TABLE DTL_TABLE
ids VARCHAR2 (10),
period VARCHAR2 (10),
course VARCHAR2 (10),
names VARCHAR2 (10),
title VARCHAR2 (10),
TYPE VARCHAR2 (10)
INSERT INTO DTL_TABLE
VALUES ('1',
'2010',
'mat',
'jim',
NULL,
NULL);
INSERT INTO DTL_TABLE
VALUES ('1',
'2010',
'mat',
'john',
NULL,
NULL);
INSERT INTO DTL_TABLE
VALUES ('1',
'2010',
'mat',
'kale',
NULL,
NULL);
INSERT INTO DTL_TABLE
VALUES ('1',
'2010',
'cam',
NULL,
'officer',
NULL);
INSERT INTO DTL_TABLE
VALUES ('1',
'2010',
'cam',
NULL,
'prof',
NULL);
INSERT INTO DTL_TABLE
VALUES ('1',
'2010',
'phy',
NULL,
NULL,
'inclass');
INSERT INTO DTL_TABLE
VALUES ('1',
'2010',
'phy',
NULL,
NULL,
'online');
COMMIT;
CREATE TABLE id_table
ids VARCHAR2 (10),
period VARCHAR2 (10),
loc VARCHAR2 (10),
dept VARCHAR2 (10),
code VARCHAR2 (10)
INSERT INTO id_table
VALUES ('1',
'2010',
'nj',
'101',
'cc');
COMMIT;Aj09 wrote:
Hi Folks,
I am facing a tricky challenge to join a table with multiple rows and converge into a single row (based on ID, period) and join with another table to get a single row. Let me explain.
Table 1: DTL_TABLE (id, period, course, names, title, type)
1 2010 mat john null null
1 2010 mat jim null null
1 2010 cam null officer null
1 2010 cam null Prof null
1 2010 phy null null Inclass
1 2010 phy null null Online
Join with
Table 2: ID_TABLE(id, period, Loc, Dept, Code)
1 2010 nj 101 CC.
Output format (id, period, course, names, title, type, Loc, Dept, Code)
result : 1 2010 mat,cam,phy john,jim officer,prof inclass,online nj 101 CC
I have created all the DDLs and DMLs if that help. Kindly let me know if a Join SQL query is possible.
SELECT TBL.id,
DTLperiod,
course,
names,
title,
TYPE,
Loc,
Dept,
Code
FROM ID_TABLE TBL,
DTL_TABLE DTL
WHERE TBL.ID = DTL.ID
AND TBL.PERIOD = DTL.PERIOD; it not good to use RESERVED WORDS like "ID" or "TYPE" as column names -
Is there a way in Oracle to return multiple rows as a single string?
Hi gurus,
I just got help from your guys fixing my dynamic sql problem. What I am doing in that function is to return a single string from multiple rows and I use it in the select statement. It works fine once the problem was solved. But is there any way in Oracle to do this in the select statement only? I have a table that stores incidents (incident_id is the PK) and another table that stores the people that are involved in an incident.
Incident_table
(incident_id number PK);
Incident_people_table
(incident_id number PK/FK,
person_id number PK);
Now in a report, I need to return the multiple rows of the Incident_People_table as a single string separated by a comma, for example, 'Ben, John, Mark'. I asked the SQL Server DBA about this and he told me he can do that in SQL Server by using a variable in the sql statement and SQL Server will auomatically iterate the rows and concatenate the result (I have not seen his actual work). Is there a similar way in Oracle? I have seen some examples here for some similar requests using the sys_connect_by_path, but I wonder if it is feasible in a report sql that is already rather complex. Or should I just stick to my simpler funcion?
Thanks.
BenHi,
May be, this example will help you.
SQL> CREATE TABLE Incident_Table(
2 incident_id number
3 );
Table created.
SQL> CREATE TABLE Person_Table(
2 person_id number,
3 person_name VARCHAR2(200)
4 );
Table created.
SQL> CREATE TABLE Incident_People_Table(
2 incident_id number,
3 person_id number
4 );
Table created.
SQL> SELECT * FROM Incident_Table;
INCIDENT_ID
1
2
SQL> SELECT * FROM Person_Table;
PERSON_ID PERSON_NAME
1 John
2 Mark
3 Ben
4 Sam
SQL> SELECT * FROM Incident_People_Table;
INCIDENT_ID PERSON_ID
1 1
1 2
1 3
2 1
2 2
2 4
6 rows selected.
SQL> SELECT IT.*,
2 (
3 WITH People_Order AS (
4 SELECT IPT.incident_id, person_id, PT.person_name,
5 ROW_NUMBER() OVER (PARTITION BY IPT.incident_id ORDER BY PT.person_name) AS Order_Num,
6 COUNT(*) OVER (PARTITION BY IPT.incident_id) AS incident_people_cnt
7 FROM Incident_People_Table IPT
8 JOIN Person_Table PT USING(person_id)
9 )
10 SELECT SUBSTR(SYS_CONNECT_BY_PATH(PO.person_name, ', '), 3) AS incident_people_list
11 FROM (SELECT * FROM People_Order PO WHERE PO.incident_id = IT.incident_id) PO
12 WHERE PO.incident_people_cnt = LEVEL
13 START WITH PO.Order_Num = 1
14 CONNECT BY PRIOR PO.Order_Num = PO.Order_Num - 1
15 ) AS incident_people_list
16 FROM Incident_Table IT
17 ;
INCIDENT_ID INCIDENT_PEOPLE_LIST
1 Ben, John, Mark
2 John, Mark, SamRegards,
Dima
Maybe you are looking for
-
Synching, ringtones and wiping out my contacts. What is wrong?
I just got a 3GS and imported my contacts from my old phone. Then I made four ringtones and installed them into the Ringtones folder in ITunes. I added them into my phone. Now I made 2 more ringtones but when I tried to install them by dragging from
-
Guest network...can't get it to stick
I'm trying to add a Guest Network to my existing airport network (using the current model Airport Extreme base station) and can't get it to work. Scoured the entire Airport Utility settings looking for it, nowhere to be found. Finally decided to rese
-
Mail Unresponsive on System Resume from Sleep, Have to Force Quit
When I resume my MacBook Air from sleep everything except mail resumes nicely. Mail seems to freeze up (unresponsive) and I end up having to force quit and re-launch it to get it working again. I've rebuilt all the mailboxes but no joy. I am usin
-
KokChoy: Control Panel crash with newest PCI drivers
As another user posted, when using the newest PCI drivers posted, in my case, the Xtreme Music X-Fi drivers, marked as 2.8.500, the control panel, crashes or hangs when loading under Windows 7 or windows vista 64 bit. Can you please confirm or relay
-
ANN: LR/Mogrify plugin released. Alamy users rejoice!
Hi, Frustrated by Alamy's 48Mb image size requirement, and the fact that Lightroom's export feature can't automatically resize images by the minimum amount necessary to guaranty that the requirement is met, I've written a plugin to solve the problem