Combining multiple rows to singe row thru SQL Stmt
Hello,
I am trying to combine values returned from multiple row into one row,
thru inner/outer sql or any optimal way.
In the example i would like to have First name, Last name, email and phone to be
returned as a single row.
create table TEMP_AAAAA
FIRST_NAME VARCHAR2(25),
LAST_NAME VARCHAR2(25),
CON_METHOD VARCHAR2(25),
CON_VALUE VARCHAR2(25)
INSERT INTO TEMP_AAAAA VALUES('TOM','MAC','EMAIL','[email protected]');
INSERT INTO TEMP_AAAAA VALUES('TOM','MAC','PHONE','12345');Any suggestion in doing it thru sql stmt.
I have done this thru pl/sql, wondering if this could be achieve thru single SQL Stmt
DECLARE
v_FIRST_NAME VARCHAR2(25);
v_SECOND_NAME VARCHAR2(25);
v_EMAIL VARCHAR2(25);
v_PHONE VARCHAR2(25);
BEGIN
v_FIRST_NAME := NULL;
v_SECOND_NAME := NULL;
v_EMAIL := NULL;
v_PHONE := NULL;
FOR IMPL_CUR IN(SELECT * FROM TEMP_AAAAA ORDER BY CON_METHOD DESC)
LOOP
IF v_FIRST_NAME IS NULL
THEN
v_FIRST_NAME := IMPL_CUR.FIRST_NAME;
END IF;
IF v_SECOND_NAME IS NULL
THEN
v_SECOND_NAME := IMPL_CUR.LAST_NAME;
END IF;
IF v_PHONE IS NULL AND IMPL_CUR.CON_METHOD = 'PHONE'
THEN
v_PHONE := IMPL_CUR.CON_VALUE;
END IF;
IF v_FIRST_NAME = IMPL_CUR.FIRST_NAME AND
v_SECOND_NAME = IMPL_CUR.LAST_NAME AND
length(v_PHONE) > 0
THEN
IF v_EMAIL IS NULL AND IMPL_CUR.CON_METHOD = 'EMAIL'
THEN
v_EMAIL := IMPL_CUR.CON_VALUE;
EXIT;
END IF;
END IF;
END LOOP;
DBMS_OUTPUT.put_line('firstName...:' || v_FIRST_NAME);
DBMS_OUTPUT.put_line('lastName....:' || v_SECOND_NAME);
DBMS_OUTPUT.put_line('PHONE.......:' || v_PHONE);
DBMS_OUTPUT.put_line('EMAIL.......:' || v_EMAIL);
END;
Hi Ludy,
Following query should work -
P.S. - I have added records for one more person with first name as 'TOM1' and last name as 'MAC1' for testing purpose. Given inserts for these 2 records as well.
Connected to Oracle Database 11g Release 11.2.0.1.0
SQL>
SQL> INSERT INTO TEMP_AAAAA VALUES('TOM1','MAC1','EMAIL','[email protected]');
1 row inserted
SQL> INSERT INTO TEMP_AAAAA VALUES('TOM1','MAC1','PHONE','12345');
1 row inserted
SQL>
SQL>
SQL> SELECT t.first_name
2 ,t.last_name
3 ,MAX(decode(t.con_method, 'PHONE', t.con_value, NULL)) phone
4 ,MAX(decode(t.con_method, 'EMAIL', t.con_value, NULL)) email
5 FROM temp_aaaaa t
6 GROUP BY t.first_name
7 ,t.last_name
8 /
FIRST_NAME LAST_NAME PHONE EMAIL
TOM MAC 12345 [email protected]
TOM1 MAC1 12345 [email protected]
SQL> Hope this helps.
Cheers,
- Anirudha
Edited by: Anirudha Dhopate on Nov 10, 2011 9:12 PM
Similar Messages
-
Combine multiple web services with the same SQL query into one
Hello,
I would like to ask a question regarding combine multiple similar web services into one. Can you please tell me if it is possible to combine 4-5 web services into one since they built on the same SQL query with 5 different criterias or condition so that the user can enter any of the 5 criterias to populate the data on the form instead of having 5 different web services?
e.g Query: Select appName, permit#, address, phone, description, type, section, from table where appName = can be 'appName, permit#, address, phone, or description' to populate the rest of the data to the form.
Does any one have ever done some thing like this in Workbench ES? If so please assist. I know it can be easier to build it in Visual Basic, C#, or dot.net but the requirement is to build it in workbench ES.
Thanks in advance,
Han DaoIf you are querying for Name, PhoneNumber, and SSN, and you queried for all people with a phone number that started with 867, you would have a potentially long list of people. So to keep track of all of the people, we store each record in XML complex elements. The root node is just any name you want, and the repeating element is the complex element name.
So using the example from above, I'm going to specify the following:
Root Node: Result
Repeating Element: Person
So now when I do a query, my resultXML will look like:
<Result>
<Person>
<Name>Alex</Name>
<PhoneNumber>867-5309</PhoneNumber>
<SSN>111-11-1111</SSN>
</Person>
</Result>
If your query returned multiple results (like ours would probably), it would look like:
<Result>
<Person>
<Name>Alex</Name>
<PhoneNumber>867-5309</PhoneNumber>
<SSN>111-11-1111</SSN>
</Person>
<Person>
<Name>Han</Name>
<PhoneNumber>867-2169</PhoneNumber>
<SSN>222-22-2222</SSN>
</Person>
</Result>
So Result and Person is just to give a little bit of structure to the xml result (containers really). So you can name them whatever is helpful for you.
The column name mappings map the query columns (Name, PhoneNumber, SSN) to some node in the XML (Name, PhoneNumber, SSN). So you don't need to specify which field maps to what in the form. Just copy the column names to the element name so you have a 1-to-1 naming. If you want to manipulate the XML a bit though, you could do:
Column Name Element
Name YourName
PhoneNumber Phone
SSN Secret
which would then make your xml look like:
<Result>
<Person>
<YourName>Alex</YourName>
<Phone>867-5309</Phone>
<Secret>111-11-1111</Secret>
</Person>
</Result>
It lets you change the XML element names to whatever you want. Otherwise by default they take on their column names.
In your form, you could bind to the WSDL through the Data Connections pane and point it to your web service. This will then create form elements that you can just drag and drop allowing you to have the information available when the service gets ran. Once the service is called, you can modify the field's data to get whatever information you need in order to populate other form fields.
If that is too confusing, feel free to send me your form (e-mail is on profile page) and I'll add comments to it to show you how to set up the form for the web service call (and also give me the link to your webservice) -
XML-SQL multiple SQL_QUERY to sing row instances..
Hello,
Is it possible to run multiple querys and return them all to a structure like this..
<HEADER_SELECTS>
<row>
<select results1></select results1>
</row>
<row>
<select results2></select results2>
</row>
</HEADER_SELECTS>
I have tried several data type structures, but all of them execute only the first "access" SQL_QUERY option.Hi Tom,
A suggestion : you could go for SQL joins.
Concerbed weblog is:
https://www.sdn.sap.com/sdn/weblogs.sdn?blog=/pub/wlg/1725 [original link is broken] [original link is broken] [original link is broken] [original link is broken] [original link is broken] [original link is broken] [original link is broken] [original link is broken] [original link is broken]
hope this helps.
Regards,
Priyanka -
Combine multiple rows in single row
I am new to SQL server and i am trying to combine multiple row in single row but i am not able to do it.Can anyone help me out?
Input :
Id |RED |BUY |BSW
1328 NULL NULL 0.05
1328 NULL 0.06 NULL
1328 0.01 NULL NULL
1328 0.05 NULL NULL
1329 NULL NULL 0.05
1329 NULL 0.05 NULL
1329 0.05 NULL NULL
Output
Id |RED |BUY |BSW
1328 0.01 0.06 0.05
1328 0.05 NULL NULL
1329 0.05 0.05 0.05Actually I am consolidating above result into text file and sending it to external system.Main aim is to remove NULL values and arrange the data as expected output.
Also expected output can be
Id |RED
|BUY |BSW
1328 0.05
0.06 0.05
1328 0.01
NULL NULL
Or
Id |RED
|BUY |BSW
1328 0.01
0.06 0.05
1328 0.05
NULL NULL
for Id= 1328. -
Concatenation Multiple Rows into Single Row
My select query is like wise
ID Name
1 Arthi
2 Preethi
3 Madhu
4 Saranya
Above i listed all the names using the select query. Now i have to combine this 4 rows in single row like
Arthi,Preethi,Madhu,Saranya.
Also this rows may be 5 or 50 too. So what are Names listed using select statement those should combine into one Single String.
Help me plPlease post this in an appropriate forum;
SQL and PL/SQL
PL/SQL
Community Feedback and Suggestions (Do Not Post Product-Related Questions Here)
Adith -
Hi friends,
I have one table like:
10 SAM
10 CAN
10 MAN
10 DEV
20 MAL
And want output like:
10 SAM,CAN,MAN,DEV
20 MAL
i want to combine multiple rows into one within an existing view.
What query will suit this to get this type of output.Please refer to this thread
column values separated by , -
Hi
I have SPGridView control (fetching value from DB) with a column which is having multiple values
in single row seperated by ";", whenever filter applied it show these values in single row but i want
to dispaly these values separately in diff. rows when column filter is applied.
I have exactly same req. as mentioned in http://sadomovalex.blogspot.in/2011/03/spgridview-with-filtering-by-multiple.html
but this solution didn't work for me.
Please help/suggest.Hi Erland,
Thank you very much for the answer. Sorry I had to be more accurate on my XML : This XML is in a SQL Server Table column. This table also has an id column. It will be great if you can answer specifically to this.
<RootNode>
<Node1>
<Line_id_Node>1 </Line_id_Node>
<A>
<B>
<D>Val1 </D>
</B>
<B>
<D>Val2 </D>
</B>
<B>
<D>Val3 </D>
</B>
</A>
</Node1>
<Node1>
<Line_id_Node> 2 </Line_id_Node>
<A>
<B>
<D>Val4 </D>
</B>
<B>
<D>Val2 </D>
</B>
</A>
</Node1>
<Node1>
<Line_id_Node> 3 </Line_id_Node>
<A>
<B>
<D>Val5 </D>
</B>
</A>
</Node1>
</RootNode>
Expected O/P:
Line_id_Node Column_D
1 1
val1, val2, val3
2 2
val4, val2
3 3
val5
Nichesl -
I wanted to display the multiple rows in one row but column should be diff
Hi
Could any body help me regarding this query how to write to get the multiple rows in one row.
eg.
i have one table tab1(eno number,ename varchar2,uid1 varchar2,uid2 varchar2,uid3 varchar4)
but when i am runing the query I am getting multiple record against one eno number because of uid1,uid2,uid3
suppose value of table is
eno ename uid1 uid2 uid3
1 a u1
1 a u2
1 a u3
when i am quering it is coming same as above but I want in one row
eno ename uid1 uid2 uid3
1 a u1 u2 u3
can any onle help me how to write the query for this requirement.
thanks
saifwhich is hard coded in my code? Here another approach, but fail for c as there is no information for the value of a column: does 1 in u1 means col 1, etc.
/* Formatted on 2012/05/29 16:29 (Formatter Plus v4.8.8) */
WITH t AS
(SELECT 1 col1, 'a' col2, 'u1' col3
FROM DUAL
UNION ALL
SELECT 1 col1, 'a' col2, 'u2' col3
FROM DUAL
UNION ALL
SELECT 1 col1, 'a' col2, 'u3' col3
FROM DUAL
UNION ALL
SELECT 1 col1, 'b' col2, 'u1' col3
FROM DUAL
UNION ALL
SELECT 1 col1, 'b' col2, 'u3' col3
FROM DUAL
UNION ALL
SELECT 1 col1, 'b' col2, 'u2' col3
FROM DUAL
UNION ALL
SELECT 1 col1, 'c' col2, 'u1' col3
FROM DUAL
UNION ALL
SELECT 1 col1, 'c' col2, 'u3' col3
FROM DUAL)
SELECT xx.col1, xx.col2, MAX (DECODE (xx.rn, 1, col3)) AS uid1, MAX (DECODE (xx.rn, 2, col3)) AS uid2,
MAX (DECODE (xx.rn, 3, col3)) AS uid3
FROM (SELECT t.col1, t.col2, t.col3, ROW_NUMBER () OVER (PARTITION BY col1, col2 ORDER BY col3) rn
FROM t) xx
GROUP BY col1, col2;output:
COL1 COL2 UID1 UID2 UID3
1 a u1 u2 u3
1 b u1 u2 u3
1 c u1 u3
Edited by: ʃʃp on May 29, 2012 2:30 AM -
Crystal reports 2008 will not allow multiple exports in a row
I recently had service pack 3 and fix pack 3.3 updated to my system ( crystal 2008) to help correct a issue with mapi not populating addresses from outlook when trying to export a report. I have a new issue. - sort of???
Now when I try to export multiple reports in a row I still have the same issue. I can send 1 report out. But if i try to send another report out i have the same issue that I had with mapi not populating the addresses field from outlook.
To get past this I shut down crystal and then bring it back up and I can send (ONE) report again before the same thing happens. Does anyone know if there is fix for this issue?Im not sure I agree with you designation that this report should be in the report DESIGN forum. I
My problem has nothing to do with report design in my OP. The MAPI function (in crystal reports itself) will not populate addresses once I export 1 report. When I try to export a second report that is when it quits works. This is basically the same problem that a hot fix or fix pack corrected but now instead of now working at all. I can only export 1 report before the same mapi issue happens???? -
Tabular form last row not accessible through PL/SQL
i am using apex 3.2
I have created one tabular form for daily receipt of material.
data get stared in table storereceipt. Now I have created one PL/SQL , it reads each row of tabular form one by one and update quantity of item in master table.
begin
FOR i IN 1 .. apex_application.g_f02.COUNT
LOOP
if apex_application.g_f12(i) != 'LOCK' then
update inv_ms_item set qty = qty apex_application.g_f11(i)+ this row update quantity in another table
where id = apex_application.g_f10(i);
end if;
END LOOP;
end;
problem arrives in last row, which was created blank with add row then data was entered and submit button was clicked.
my pl/sql is called on click of submit button . it updates all items except on last row.
remaining things works fine.
if i create 5 rows with add row and click one more time. last row will be blank. now if i click submit then all rows get updated and as last row is blank i dont have to care about it.
Edited by: TEJU on Oct 21, 2010 4:40 PM
Edited by: TEJU on Oct 21, 2010 4:40 PMHi ,
Teju this is the major drawback in the apex 3.2 because if u see the length when there is only one record is present in the tabular form
then the outpuyt u will get is undefined , but though the colum exists it wont .
i have faced this problem , one thing u can do is use document.getElementByID method of java script
first find out the length , that specifies how many rows are present then write a loop
for 1=0 till i<=-tahta length
document.getElemetByid('f01_00'+i)
/* this will give u f01_001 ...this can be ur element 1 , f01_002 this will ur element two .
I had already used it and its working fine...
Regards,
Nandini Thakur. -
Power Query: Expand multiple lists within a row at once
I have a json file that I've been fairly successful importing into excel using power query. My issue now is dealing with lists in multiple columns. I need to figure out how to expand the values of the lists at once so I only add rows once, versus multiple
times.
Example:
Row 1 - Text | List1 {1,1,2,4} | List2 {A,B,B,D} | List3 {W,X,Z,Z}|
Row 2 - Text | List1 {1,1,2,4} | List2 {A,B,B,D} | List3 {W,X,Z,Z}|
Row 3 - Text | List1 {1,1,2,4} | List2 {A,B,B,D} | List3 {W,X,Z,Z}|
When I use Table.ExpandListColumn on the first column (List1), it adds rows to the table. Row1 is expanded into four rows, which is fine but I can only do this one at a time resulting in too many rows being added. Row 1 is expanded as:
Text | 1 | List2 {A,B,B,D} | List3 {W,X,Z,Z}
Text | 1 | List2 {A,B,B,D} | List3 {W,X,Z,Z}
Text | 2 | List2 {A,B,B,D} | List3 {W,X,Z,Z}
Text | 4 | List2 {A,B,B,D} | List3 {W,X,Z,Z}
If you do this again for the next column (list2), it keeps adding rows. The above example should only have 12 rows. A single row needs to be expanded to look like:
Text | 1 | A | W
Text | 1 | B | X
Text | 2 | B | Z
Text | 4 | D | Z
Note, the lists will always be equal in size across a starting row but may be of larger or smaller sizes and can be null.You can account for nulls by testing to see if there is a list in the columns, but it would be cleaner to do this using a custom function e.g.
Create a custom function (query) called TableFromLists, The code would be:
(value1, value2, value3)=>
let
List1 = if Value.Is(value1, type {list}) then value1
else {value1},
List2 = if Value.Is(value2, type {list}) then value2
else {value2},
List3 = if Value.Is(value3, type {list}) then value3
else {value3},
Table = Table.FromColumns({List1,List2,List3})
in
Table
In the code, you test to see if each input value is a list. If so, then each identifier (List1...List3) is assigned the corresponding input value (value1...value3). If not, each identifier is assigned the input value converted to a list (by
surrounding the input value in braces).
Then, in the previous step 2, the formula for the custom column would be:
TableFromLists([List1],[List2],[List3]) -
I have 3 drop down boxes in my report done SQL SSRS .. I want t to put them in the same row (just one row) ..how can I do it.?
Hi Sashi123,
Just as Ivan said, we do not have the ability to change the Report Manager in this way. Because we can’t change the parameters locations as this is not supported to write expressions for the location of parameters.
While we can build a custom application in Visual Studio that we can get it through codes after deploying the report, then through URL we can get it by customization.
Hope this helps.
Thanks,
Katherine Xiong
Katherine Xiong
TechNet Community Support -
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 -
SSRS - multiple toggle in single row item to drill down sub report
I need to open a sub report using drill down concept. Is there any possible to give toggle for multiple columns in single row.
http://social.msdn.microsoft.com/Forums/getfile/528971
The sub report have two parameters to show data. If I click first link the subreport to be drill down with parameters 1 and 5. If I click second link the
same sub report to be drill through with different parametrs as 2 and 6.
Any idea to do this?Thanks for your reply.
I do it this in different way.
See the below sample image url. I created the main report having four details with toggle to drill down the sub report. If I click the Detail1 the
subreport1 will expand drill down then I click Detail2 the subreport2 to be expand.
But still the subreport1 is visible I want to collapse Subreort1 once click toggle second and vice versa.
Please give your suggetions.
http://social.msdn.microsoft.com/Forums/getfile/529184 -
How to combine many rows into one row
Hi all,
I have a question regarding to how to combine many rows into one row?
My result set is like that:
ITEM_NO NAME1
11 abc
11 cde
11 fg
Want to combine them into
ITEM_NO NAME1
11 abc;cde;fg
would anybody can tell me how to do that? Thanks
RayYou can check this --
satyaki>
satyaki>
satyaki>create table t
2 as
3 select 11 ITEM_NO, 'abc' NAME1 from dual
4 union all
5 select 11 ITEM_NO, 'cde' NAME1 from dual
6 union all
7 select 11 ITEM_NO, 'fg' NAME1 from dual;
Table created.
satyaki>
satyaki>
satyaki>
satyaki>set lin 10
satyaki>
satyaki>desc t;
Name Null? Type
ITEM_NO NUMBER
NAME1 VARCHAR2(3)
satyaki>
satyaki>
satyaki>set lin 1000
satyaki>
satyaki>
satyaki>
satyaki>SELECT ITEM_NO,
2 LTRIM(MAX(SYS_CONNECT_BY_PATH(NAME1,';'))
3 KEEP (DENSE_RANK LAST ORDER BY curr),';') AS NAME1_DET
4 FROM (SELECT ITEM_NO,
5 NAME1,
6 ROW_NUMBER() OVER (PARTITION BY ITEM_NO ORDER BY NAME1) AS curr,
7 ROW_NUMBER() OVER (PARTITION BY ITEM_NO ORDER BY NAME1) -1 AS prev
8 FROM t)
9 GROUP BY ITEM_NO
10 CONNECT BY prev = PRIOR curr AND ITEM_NO = PRIOR ITEM_NO
11 START WITH curr = 1;
ITEM_NO NAME1_DET
11 abc;cde;fgRegards.
Satyaki De.
Maybe you are looking for
-
As I'm unable to add music to my iPod any longer, I tried updating. I'm unable to achieve the updating cause as soon as I launch the update process, a meesage with "Internal error of iPod manager" appears. Help plaese
-
Popup issue - Please help!
Hello All, I have a child window, where I have a tabular form , where user can select multiple values and it returns as one string for ex: 1,2,3,4,5 . I am returning the string value in a item. I have also a parent window , where I have another tabul
-
Permanently disable 'Extras'...???
Have this problem in both CS4 and CS5: Whenever I open images or create new ones or simply pasting a new layer ontop of an already existing image this "resize-outline" extra keeps showing up. Hotting command+H makes it go away, until I paste another
-
How to provide Convolution in Frequency domain?
Hi everybody, I have some problem with applying a convolution of the image and Smoothing Kernel in LabView. The idea behind this is, that convolution is a point-wise multiplication in the fourier domain. So firstly I want to apply convolution by stan
-
Setting JRE Runtime parameters at login
I manage a domain with 140 users and they need to have java runtime parameters set to use their web applications. Is there a way, via a script, to set the runtime parameters for users? Currently using JRE 6u5 i586 -Necco