Single Column Multiple Rows
Dear All,
I want a solution for the following,
If there is a column Ename with n number rows, i want the rows to be reterived like the following ename1,ename2.....
I don't know how many rows will be there and the sql query should be of dynamic..
please throw some light....
You can also use the aggregate package for varchar2 for exemple :
create or replace type string_agg_type as object
total varchar2(4000),
static function
ODCIAggregateInitialize(sctx IN OUT string_agg_type )
return number,
member function
ODCIAggregateIterate(self IN OUT string_agg_type ,
value IN varchar2 )
return number,
member function
ODCIAggregateTerminate(self IN string_agg_type,
returnValue OUT varchar2,
flags IN number)
return number,
member function
ODCIAggregateMerge(self IN OUT string_agg_type,
ctx2 IN string_agg_type)
return number
create or replace type body string_agg_type
is
static function ODCIAggregateInitialize(sctx IN OUT string_agg_type)
return number
is
begin
sctx := string_agg_type( null );
return ODCIConst.Success;
end;
member function ODCIAggregateIterate(self IN OUT string_agg_type,
value IN varchar2 )
return number
is
begin
self.total := self.total || ',' || value;
return ODCIConst.Success;
end;
member function ODCIAggregateTerminate(self IN string_agg_type,
returnValue OUT varchar2,
flags IN number)
return number
is
begin
returnValue := ltrim(self.total,',');
return ODCIConst.Success;
end;
member function ODCIAggregateMerge(self IN OUT string_agg_type,
ctx2 IN string_agg_type)
return number
is
begin
self.total := self.total || ctx2.total;
return ODCIConst.Success;
end;
end;
CREATE or replace
FUNCTION stragg(input varchar2 )
RETURN varchar2
PARALLEL_ENABLE AGGREGATE USING string_agg_type;
select deptno, stragg(ename)
from emp
group by deptno
DEPTNO
STRAGG(ENAME)
10
CLARK,KING,MILLER
20
SMITH,FORD,ADAMS,SCOTT,JONES
30
ALLEN,BLAKE,MARTIN,TURNER,JAMES,WARD
would be the approach.
Similar Messages
-
INSERTED Table - When it gets populated with single or multiple rows?
Hi,
I'm trying to create a trigger which then insert to a table. i'm wondering when does the INSERTED table gets populated with single or multiple rows?
Should I always assume that the INSERTED Table will contains several rows? What does the scope of the INSERTED table in the trigger, isn't based on the user session?
The reason why i asked this is because as far as i know inserted table may contain several table when the trigger fires which is why I use cursor to insert records in the table ( there's a behind why i use cursor).
But if the inserted table will only contain a single record during the session of the trigger then i can avoid the cursor.
Thanks.But since we control the transaction process and we know for a fact that user will only be able to save a record one at a time, do we still expect multiple rows? I just want to have a clear concept on the INSERTED table.
...and then the DBA or someone else sees fit to enter a number of rows directly from a query window. And don't laugh. That is bound to happen sooner or later.
However, just because this can (and will) happen does not mean that you need to handle it on equal footing with the normal case user entering data through the application. What you cannot permit yourself to is to drop the DBA case on the floor, that is write
the trigger as if there would either be single-row inserts and produce incorrect results for multi-row inserts.
But, yes, allowing yourself to use a cursor, if you want to reuse the existing stored procedure is feasible. That is also the more drastic solution suggested by Tom to add an explicit check that disallows multi-row inserts.
Finally, permit me to comment on this:
Additionally, it's difficult to use the code below as i need to pass the identity id of tbl_A to tbl_B
You can use the OUTPUT clause to capture the values, but that requires that you have something you can map the identity values to in the columns you insert, and this is not always the case. However, there is a lot simpler solution to the problem: don't
use IDENTITY. IDENTITY is one of these over-used and over-abused features in SQL Server. You need it when you want to support high-concurrency inserts, because rolling your own requires a serialisation point. But with a moderate insertion frequency, IDENTITY
only gives you headache.
Erland Sommarskog, SQL Server MVP, [email protected] -
Convert single column into rows
hi Gurus,
I have one table test colums are id and name.
id number
name varchar2
data is like
id name
1 xy
2 xyy
3 mm
4 pp
Now my requirement is to convert single column id into rows
i,e my output should be of singel rows like :- 1,2,3,4
How to achive this result .
I dont have any idea to do this query.
Please help guys.
Thanks in advance.
VijayWell,
As long as your code doesn't have to run in production, simplest way is:
WM_CONCAT (but it's not documented)
or use XMLAGG, it's simpler than a connect by:
MHO%xe> with t as (
2 select 1 col, 'xy' str from dual union all
3 select 2, 'xyy' from dual union all
4 select 3, 'mm'from dual union all
5 select 4, 'pp' from dual union all
6 select 8, 'pp' from dual union all
7 select 12, 'pp' from dual union all
8 select 40, 'pp' from dual
9 )-- actual query, based on id's generated above:
10 select rtrim(xmlagg(xmlelement(e,col||',')).extract('//text()'),',') col
11 from t;
COL
1,2,3,4,8,12,40
1 rij is geselecteerd. -
Convert 1 row, multiple (identical) columns into 1 column, multiple rows
I have 10g, so unpivot is unavailable to me. I have a query that looks like:
with data as (
select 1,2,3 from dual
) select * from data
which returns 1,2,3. I need it to return:
1
2
3 and be able to name the output column. The results of this will be used in a sub-select in an IN clause. This is a trivial example. In reality the list of numbers will be varied and large - too many to either use bind variables or to put in the IN clause as a list. Is there a simple way I'm overlooking?Hi,
One way to unpivot is to join to a table that has as many rows as you need. Instead of an actual table, you can always generate a Counter Table (actually a result set), with exactly the right number of rows. Use CASE (or something similar) go get a different column for each row of the counter table.
For example, to get the 3 columns of the scott.dept table onto 3 separate rows:
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 3
SELECT CASE c.n
WHEN 1 THEN TO_CHAR (d.deptno)
WHEN 2 THEN d.dname
WHEN 3 THEN d.loc
END AS data
FROM scott.dept d
CROSS JOIN cntr c
;Output:
DATA
10
20
30
40
ACCOUNTING
RESEARCH
SALES
OPERATIONS
NEW YORK
DALLAS
CHICAGO
BOSTON -
Deletion of single or multiple rows by selecting check boxes?
Hi ,
I want to delete a row or no of rows in a table by selecting the check boxes.COuld any one suggest me how to do this?
Regards,
RamFollowing is the JavaScript sample you may have to tweak the code to fit your situation....
// Invoke the Instance Manager to remove the current instance of the detail subform.
_<SubformName/RowSOMName>.removeInstance(index of the object to remove)
//_Section2Detail.removeInstance(this.parent.index); //in my case I have a delete button next to each row when clicked that will pass the index of the parent(row index as it is part of that row).
// Invoke the recalculate method to update the form calculations.
xfa.form.recalculate(1); -
Multiple row resizing or automatic height fit
Hi,
I have just opened a very simple excel file in Numbers. The workbook consists of only 1 sheet and one table with 3 columns and around 30 rows; is just a list. The problem is that, when I Opened, the text didn't fit the height of the rows, so I had to manually change the row heigh.
Is there any way to change the heigh of multiple rows or just something like the "autoFit" feature in excel?I am not sure if this is what your asking or not, but if you select a single column or row, then use the little blue dot to extend the selection. now you can grab the right/bottom edge where you see the gripper markings and you can change the width/height of all the selected columns/rows.
Autofit for columns is not there (That I can find it). but rows are automatically autofit in height. If you have some text that wraps in the cell, if you make the column wide enough, you will notice that all the rows get smaller in height to fir the unwrapped text.
Hope this is even close to what your asking
Jason -
Hello everyone,
I am developing an application (ALV OO) to select more than 1 column by row ( one ). This is an a holiday application so the idea is:
-One column will be the day, and the row will be the user.
So I am trying to select more than one day by user (that would be the row).
I am using the method set_table_for_first_display but when it shows the alv, doesn't let me to select more than one column with a click of the mouse.
Does anybody know if I can do this (select more than one column, by row) in somehow?
Please let me know if you need more clarification about this.
Thanks in advance
DiegoHi Diego,
it's all in the documentation.
set different selection modes through the value of the field u201CSEL_MODEu201D in the layout structure.
SPACE
same as 'B'
see 'B'
Default setting
'A'
Column and row selection
Multiple columns
Multiple rows
The user selects the rows through pushbuttons at the left border of the grid control.
'B'
Simple selection, list box
Multiple columns
Multiple rows
'C'
Multiple selection, list box
Multiple columns
Multiple rows
'D'
Cell selection
Multiple columns
Multiple rows
Any cells
The user selects the rows through pushbuttons at the left border of the grid control
Beyond setting this option, you can set u201CNO_ROWMARKu201D option to hide the mark column which is normally visible when the selection mode allows multiple row selection.
One point to notice here is that if you set your ALV Grid as to be editable, it may override your selection mode regardless of your layout settings.
This is from SDN Community Contribution "An Easy Reference for ALV Grid Control" By: Serdar ŞİMŞEKLER
Sorry, no link,. it's on my disk.
Regards,
Clemens -
Need help with query for converting columns to rows
Hello,
I know this is a very common question asked in the forum. I have searched regading this, i did find some threads, but i was not able to achieve what i require from the answers posted. So anybody please help me.
I have a table which is having multiple columns as follows:
Insert into table_1 (X,Y,Z,A,B,C,D,E,F,G,H,I) values (0,0,2,0,0,1,3,0,0,0,0,0);I want to convert the result into a two column, multiple rows i.e., I want the result as follows:
Col1 Col2
X 0
Y 0
Z 2
A 0
B 0
C 1
D 3
E 0
F 0
G 0
H 0
I 0Please anybody help me in writing the query for this..Is this what you are expecting:
SQL> WITH T AS
2 (
3 SELECT 0 X, 0 Y, 2 Z, 0 A, 0 B, 1 C, 3 D, 0 E, 0 F, 0 G, 0 H, 0 I FROM DUAL
4 )
5 SELECT 'X' col1, X col2 FROM T
6 UNION ALL
7 SELECT 'Y' col1, Y col2 FROM T
8 UNION ALL
9 SELECT 'Z' col1, Z col2 FROM T
10 UNION ALL
11 SELECT 'A' col1, A col2 FROM T
12 UNION ALL
13 SELECT 'B' col1, B col2 FROM T
14 UNION ALL
15 SELECT 'C' col1, C col2 FROM T
16 UNION ALL
17 SELECT 'D' col1, D col2 FROM T
18 UNION ALL
19 SELECT 'E' col1, E col2 FROM T
20 UNION ALL
21 SELECT 'F' col1, F col2 FROM T
22 UNION ALL
23 SELECT 'G' col1, G col2 FROM T
24 UNION ALL
25 SELECT 'H' col1, H col2 FROM T
26 UNION ALL
27 SELECT 'I' col1, I col2 FROM T
28 /
C COL2
X 0
Y 0
Z 2
A 0
B 0
C 1
D 3
E 0
F 0
G 0
H 0
C COL2
I 0
12 rows selected. -
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 -
Concatenate multiple row values into single column value
Hello,
Can anyone please refresh my memory on how to concatenate multiple row values into a single column value.
In the following query, I will get multiple denial reasons per application and I would rather return all denial reasons on one line.
SELECT a.application_id, a.membership_number,
r.reason_text AS denial_reason,
a.appl_receipt_date AS application_receipt_date,
a.plan_request_1 AS application_plan_code,
a.adjudication_date AS application_denial_date
FROM application a, PLAN p, application_reason ar, reason r
WHERE a.plan_request_1 = p.plan_cd
AND a.application_id = ar.application_id
AND ar.reason_id = r.reason_id
AND a.adjudication_cd = 'D'
AND a.appl_receipt_date BETWEEN '01-jan-2006' AND '31-dec-2006'
AND p.plan_type_id = 12 and a.application_id = :appId
ORDER BY application_id
Any help is greatly appreciated.
Thanks,
-Christinefound the following
SELECT deptno,
LTRIM(MAX(SYS_CONNECT_BY_PATH(ename,','))
KEEP (DENSE_RANK LAST ORDER BY curr),',') AS employees
FROM (SELECT deptno,
ename,
ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) AS curr,
ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) -1 AS prev
FROM emp)
GROUP BY deptno
CONNECT BY prev = PRIOR curr AND deptno = PRIOR deptno
START WITH curr = 1;
DEPTNO EMPLOYEES
10 CLARK,KING,MILLER
20 ADAMS,FORD,JONES,SCOTT,SMITH
30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
3 rows selected.at http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php -
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 -
Merge multiple rows into single row (but multiple columns)
How to merge multiple rows into single row (but multiple columns) efficiently.
For example
IDVal IDDesc IdNum Id_Information_Type Attribute_1 Attribute_2 Attribute_3 Attribute_4 Attribute_5
23 asdc 1 Location USA NM ABQ Four Seasons 87106
23 asdc 1 Stats 2300 91.7 8.2 85432
23 asdc 1 Audit 1996 June 17 1200
65 affc 2 Location USA TX AUS Hilton 92305
65 affc 2 Stats 5510 42.7 46 9999
65 affc 2 Audit 1996 July 172 1100
where different attributes mean different thing for each Information_type.
For example for Information_Type=Location
Attribute_1 means Country
Attribute_2 means State and so on.
For example for Information_Type=Stats
Attribute_1 means Population
Attribute_2 means American Ethnicity percentage and so on.
I want to create a view that shows like below:
IDVal IDDesc IDNum Country State City Hotel ZipCode Population American% Other% Area Audit Year AuditMonth Audit Type AuditTime
23 asdc 1 USA NM ABQ FourSeasons 87106 2300 91.7 46 85432 1996 June 17 1200
65 affc 2 USA TX AUS Hilton 92305 5510 42.7 46 9999 1996 July 172 1100
ThanksHi,
That's called Pivoting . The forum FAQ has a section on this subject: {message:id=9360005}
I hope this answers your question.
If not, post your best attempt, along with a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data. (You did post the results you wanted, but they're very hard to read because they're not formatted. Use \ tags, as described in the forum FAQ, below.)
Explain, using specific examples, how you get the results you want from the data given.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0). This is always important, but especially so with pivots.
See the forum FAQ {message:id=9360002} -
Populate multiple rows and columns based off of a single cell
Does anyone know a way to populate multiple rows and columns based off of the input of a single cell. I'm trying to create a workout tracker that auto populates from a master list of workouts. Example, in A2 the user selects the workout from a drop down menu (St1, St2, St3, etc) and the workout is pulled from the master list and is populated into B2:C5.
I'm using =LOOKUP(A2,Master :: A2:A32,Master :: C2:C32) right now, but it's only drawing the top column. I'm looking for an edit for the formula (or completely new formula) that will place the info from the 1st column of the Master table into the 1st column of the Auto Tracker 1 table and then on down until the 4th column.
Here is a screen shot of what I have now:
And an example of what I'd like to accomplish:
Any and all help is appreciated, and please let me know if there is any more info that I can give that'd help out.Hi Mike,
Your screenshots show that you have merged some cells in Column A of both tables. That may be the reason why the LOOKUP function is having trouble working out which range of cells to examine.
Hope this helps.
Regards,
Ian. -
Select single column but multiple rows in JTable
Hi
I have a jTable and want to be able to select multiple rows but only in a single column.
I've set these properties which makes selection almost the way I would like it.
table1.setCellSelectionEnabled(true);
table1.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);the only problem now is that the user can select multiple columns.
Is there a simple way to restrict selection to single column?
regards
abqtable.setCellSelectionEnabled(true);
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
DefaultListSelectionModel model =
(DefaultListSelectionModel)table.getColumnModel().getSelectionModel();
model.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); -
Need query to convert Single Row Multiple Columns To Multiple rows
Hi,
I have a table with single row like below
Column0 | Column1 | Column2 | Column3 | Column4|
Value0 | Value1 | Value2 | Value3 | Value4 |
Am looking for a query to convert above table data to multiple rows having column name and its value in each row as shown below
Column0 | Value0
Column1 | Value1
Column2 | Value2
Column3 | Value3
Column4 | Value4
Thanks in advance.
MohanHi ykMohan,
Dynamic UNPIVOT can be applied in this case as well.
CREATE TABLE dbo.T(ID INT,Column0 VARCHAR(99),Column1 VARCHAR(99),Column2 VARCHAR(99),Column3 VARCHAR(99),Column4 VARCHAR(99))
INSERT INTO T VALUES
(1,'Value0','Value1','Value2','Value3','Value4'),
(2,'Value0','Value1','Value2','Value3','Value4');
DECLARE @columns VARCHAR(MAX)
SELECT @columns=
STUFF(
SELECT ','+ COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ='T' AND TABLE_SCHEMA='dbo' AND Column_name NOT IN('ID') FOR XML PATH('')
),1,1,'')
DECLARE @Sql NVARCHAR(MAX)
SET @Sql =
'SELECT ID, UPT.col,UPT.val FROM T
UNPIVOT
(val FOR col IN('+@columns+')) AS UPT'
EXEC sp_executeSQL @Sql
DROP TABLE T
If you have any feedback on our support, you can click
here.
Eric Zhang
TechNet Community Support
Maybe you are looking for
-
Files missing from external drive
I have a 4 TB G-Tech RAID drive attached via FireWire 800. It was formerly a backup drive for photos/videos, moved up to a main drive when the old drive it was backing up failed. Problems got in the way of replacing it.... Anyway, all of a sudden, my
-
I recently bought a new windows computer. I downloaded itunes on my new computer. When I went to sync it only allowed my to sync purchased music. Problem is I have about 200 CD's of mine that I downloaded to the itouch on the previous PC. How do
-
Photoshop CS5 Bridge HTML Gallery Questions
Have upgraded to CS5 from CS3 and am trying to create HTML Web Galleries that are similar to what we have been creating in CS3. I no longer have control of the size of the thumbnails created for the Index pages and cannot create less than 3 rows of t
-
Hi to everyone, My situation With Netbeans 6.1 I developed a java application to manage data which uses jre 6u7 and jdbc4 to connect to a postgres 8.2.5 database, in the code I use entity manager and persistence unit. All fine, BUT apparently randoml
-
Hi All, I am trying to use replace to replace a string with some tags as shown below: replace('ADAFETG3B-D0LA,ADAFETG3B-D0WA ', ',' , '</PartName><PartName>') but in the output it replaces the < with '<semicolon' and > with '>semicolon' Is there