Column values separated by ,
Suppose say I have table TAB1 with the following data :
COL1 COL2
1 A
1 B
1 C
2 M
2 N
2 O
2 P
2 Q
3 S
3 T
How would I retrieve the data in the following format ?
1 A,B,C
2 M,N,O,P,Q
3 S,T
Hi Laurent Schneider.
That a good solution.
I researched that.
SQL> select version from v$instance;
VERSION
10.2.0.1.0
SQL> create table IDTable(ID,Val) as
2 select 10,'abc' from dual union all
3 select 10,'abc' from dual union all
4 select 10,'def' from dual union all
5 select 10,'def' from dual union all
6 select 20,'ghi' from dual union all
7 select 20,'jkl' from dual union all
8 select 20,'mno' from dual union all
9 select 20,'mno' from dual;
SQL> col enames for a40
SQL> select ID,wmsys.wm_concat(Val) as enames
2 from IDTable
3 group by ID
4 order by ID;
ID ENAMES
10 abc,abc,def,def
20 ghi,jkl,mno,mno
SQL> select ID,wmsys.wm_concat(distinct Val) as enames
2 from IDTable
3 group by ID
4 order by ID;
ID ENAMES
10 abc,def
20 ghi,jkl,mno
SQL> select ID,wmsys.wm_concat(Val order by Val) as enames
2 from IDTable
3 group by ID
4 order by ID;
select ID,wmsys.wm_concat(Val order by Val) as enames
ORA-00907:
SQL> select ID,Val,wmsys.wm_concat(Val) over(partition by ID) as enames
2 from IDTable
3 order by ID;
ID VAL ENAMES
10 abc abc,abc,def,def
10 abc abc,abc,def,def
10 def abc,abc,def,def
10 def abc,abc,def,def
20 ghi ghi,jkl,mno,mno
20 jkl ghi,jkl,mno,mno
20 mno ghi,jkl,mno,mno
20 mno ghi,jkl,mno,mno
SQL> select ID,Val,wmsys.wm_concat(Val) over(order by ID,Val) as enames
2 from IDTable
3 order by ID;
ID VAL ENAMES
10 abc abc,abc
10 abc abc,abc
10 def abc,abc,def,def
10 def abc,abc,def,def
20 ghi abc,abc,def,def,ghi
20 jkl abc,abc,def,def,ghi,jkl
20 mno abc,abc,def,def,ghi,jkl,mno,mno
20 mno abc,abc,def,def,ghi,jkl,mno,mno
Similar Messages
-
How can i select other column values('-' separated) in group by function
CREATE TABLE EMP (
EMPNO NUMBER(4) NOT NULL,
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
SAL NUMBER(7)
INSERT INTO EMP(EMPNO, ENAME, JOB, SAL) VALUES (7369, 'SMITH', 'CLERK', 800);
INSERT INTO EMP(EMPNO, ENAME, JOB, SAL) VALUES (7499, 'SMITH', 'SALESMAN', 1600);
INSERT INTO EMP(EMPNO, ENAME, JOB, SAL) VALUES (7521, 'ALLEN', 'SALESMAN', 2400);
In Output I want 3 columns : EMP,SUM(SAL),JOB(hyphenSeparated)
Means i want my output like
First row : SMITH,2400,CLERK-SALESMAN
Second row : ALLEN,2400,SALESMAN
I tried to write following sql : select ename,sum(sal) from emp group by ename
But i want other colummn value in '-' separated. but group by is only allowing agreegated function.
How can i select other column value using group by function.SQL> select ename,sum(sal), listagg(job, '-') within group (order by job) as job from emp group by ename;
ENAME SUM(SAL) JOB
ALLEN 2400 SALESMAN
SMITH 2400 CLERK-SALESMANnote: LISTAGG is a feature of 11.2 -
i have 3 rows in a single column
table a
===========
column1
-500000-40000
400001-80000
800001-
i want to separate it into 2 parts
col1 col2
-500000 40000
400001 80000
800001Not very clear about the data.. still something like..
SQL> select * from test;
C1
-500000-40000
400001-80000
800001-
-1000--2000
SQL> select regexp_substr(c1,'^-?[^\-]+',1,1) c1,
2 substr(regexp_substr(c1,'-*[^\-]+',1,2),2) c2
3 from test;
C1 C2
-500000 40000
400001 80000
800001
-1000 -2000 -
Output as a single row with values separated by comas
I have a query that returns one column as:
Col1
a
b
c
I want the output as a single row with values separated by comas:
Col1
a,b,c
How do I do it in SQL?Or... build your own aggregation function :
SQL> create or replace type AggregateStr as object
2 (
3 tag VARCHAR2(1),
4 str VARCHAR2(4000), -- concatenation string
5 static function ODCIAggregateInitialize(sctx IN OUT AggregateStr) return number,
6 member function ODCIAggregateIterate(self IN OUT AggregateStr, value IN VARCHAR2) return number,
7 member function ODCIAggregateTerminate(self IN AggregateStr, returnValue OUT VARCHAR2, flags IN number) return number,
8 member function ODCIAggregateMerge(self IN OUT AggregateStr, ctx2 IN AggregateStr) return number
9 );
10 /
Type created.
Elapsed: 00:00:00.00
SQL>
SQL> create or replace type body AggregateStr is
2 static function ODCIAggregateInitialize(sctx IN OUT AggregateStr) return number is
3 begin
4 sctx := AggregateStr(',','');
5 return ODCIConst.Success;
6 end;
7
8 member function ODCIAggregateIterate(self IN OUT AggregateStr, value IN VARCHAR2) return number is
9 begin
10 self.str:=self.str||value||self.tag;
11 return ODCIConst.Success;
12 end;
13
14 member function ODCIAggregateTerminate(self IN AggregateStr, returnValue OUT VARCHAR2, flags IN number) return number is
15 begin
16 returnValue := rtrim(self.str,',');
17 return ODCIConst.Success;
18 end;
19
20 member function ODCIAggregateMerge(self IN OUT AggregateStr, ctx2 IN AggregateStr) return number is
21 begin
22 self.str := ctx2.str;
23 return ODCIConst.Success;
24 end;
25 end;
26 /
Type body created.
Elapsed: 00:00:00.00
SQL>
SQL> CREATE OR REPLACE FUNCTION ConcateStr (input VARCHAR2) RETURN VARCHAR2
2 PARALLEL_ENABLE AGGREGATE USING AggregateStr;
3 /
Function created.
Elapsed: 00:00:00.00
SQL>
SQL> select max(concate_string) keep (dense_rank last order by length(concate_string)) as concate_string
2 from (select ConcateStr(ename) over (order by ename) concate_string
3 from emp);
CONCATE_STRING
ADAMS,ALLEN,BLAKE,CLARK,FORD,JAMES,JONES,KING,MARTIN,MILLER,SCOTT,SMITH,TURNER,WARD
Elapsed: 00:00:00.00Or if you doesn't matter of the order :
SQL> select ConcateStr(ename)
2 from emp;
CONCATESTR(ENAME)
SMITH,ALLEN,WARD,JONES,MARTIN,BLAKE,CLARK,SCOTT,KING,TURNER,ADAMS,JAMES,FORD,MILLERNicolas.
adding the second query.
Message was edited by:
N. Gasparotto -
How to select each character of column value
Hi All,
How can i get each character separately of a column value in a select statement.
LIKE i emp table if ename='Test' then i want
a select statement that can give me the result like this
T,e,s,t
Regards,
Anil Ror this?
SQL> create table mytable
2 as
3 select 'test' text from dual union all
4 select 'a text, containing two comma''s (,)' from dual union all
5 select 'a text ending with a comma,' from dual
6 /
Tabel is aangemaakt.
SQL> select text
2 , substr(regexp_replace(text,'(*?)',',\1'),2,length(text)*2-1) with_commas
3 from mytable
4 /
TEXT WITH_COMMAS
test t,e,s,t
a text, containing two comma's (,) a, ,t,e,x,t,,, ,c,o,n,t,a,i,n,i,n,g, ,t,w,o, ,c,o,m,m,a,',s, ,(,,,)
a text ending with a comma, a, ,t,e,x,t, ,e,n,d,i,n,g, ,w,i,t,h, ,a, ,c,o,m,m,a,,
3 rijen zijn geselecteerd.Regards,
Rob.
Message was edited by:
Rob van Wijk
Slight modification to cater for strings beginning and ending with commas. -
hello every one.
this is IBRAN, i am using datagrid in my flex application now i want to add only one column data and i want to display it in textinput,
please help me i am trying this from last 7 days please please help me,
this is my datagrid that how i defined-
<mx:DataGrid id="neworderdetails" width="839" height="400" editable="true" visible="false" itemClick="editablefunc(event)" variableRowHeight="true"
draggableColumns="false" sortableColumns="false" >
<mx:columns>
<mx:DataGridColumn headerText="Product Barcode" dataField="ABarcode"/>
<mx:DataGridColumn headerText="Product Description" dataField="BDescription"/>
<mx:DataGridColumn headerText="Ordering Quantity" dataField="COrderingQty"/>
<mx:DataGridColumn headerText="Max Quantity" dataField="DMaxQty"/>
<mx:DataGridColumn headerText="Current Quantity" dataField="ECurrentQty"/>
<mx:DataGridColumn headerText="Cost" dataField="FCost"/>
<mx:DataGridColumn headerText="MRP" dataField="GMrp"/>
<mx:DataGridColumn headerText="Total Discount" dataField="HDiscount"/>
<mx:DataGridColumn headerText="VAT" dataField="IVat"/>
<mx:DataGridColumn headerText="Amount" dataField="GAmount" labelFunction="{Amount}"/>
</mx:columns>
</mx:DataGrid>
and one more thing, I am using labelFunction Amount i am calculating amount by multiplying two columns of datagrid this is working fine
now i want to add the only Amount column values one by one and i want to display result as total amount in one textInput.
please please help me for this problem.
thank you.Hey
to get the multiple selected row use the property datagrid.selectedindices this will return a string with all the selected index with (,) separated .
then get the data of selcted index and put it in to database.
regards.
gajanan hiroji | [email protected] | www.isacglobal.com -
Get column values from list of values programmatically
hi all
how i get column values from list of values programmatically in the
returnPopupDataListener methodIf this answers your question , please close this thread by marking it as answered.
Thanks -
SSRS - Expression to color column value dynamically in Matrix
Hi ,
I have a matrix which looks like :
The <<Expr>> value can be 1 /0 /"-" .
The Expr value is being calculated dynamically.
The data set query I am using has a column called due_days.
In the color expression of the <<Exp>> box I am using the expression as :
=IIf(Fields!Due_Days.Value>14 and Fields!Notes_Count.Value>0,"Blue",(Iif(Sum(Fields!Notes_Count.Value)=0 ,"Red","Black")))
My requirement is if the Due_Days column value is >14 then I need to highlight the value as blue else black and if value is 0 then red. When I use the above query it is just highlighting the color blue for 1st column only. Eg: 4th row . Due days for month
of Oct and Nov is > 14 but it shows blue only for month of oct.
How can i resolve the issue?In select query i have 5 columns:
Due days(Which is difference between 2 dates) ,
Notes count (Which is just a count of notes entered or not having value 0/1 and value '-' if another column CRD is greater than the matrix month and year.)Eg: below date 11/12/2014 is greater than Oct 2014 hence Oct 2014 should have "-"
Month Name , Year , Month Nbr (last 6 months which I cross joined with the table to get counts for each month)
The matrix has year and last 6 month as column groups
Color coding should be if notes count is 0 then red ,if notes count is 1 and due_days> 14 then blue else black . When i try to use expression for color as i mentioned above, it colors only 1st colum.eg: 2nd row
Nov 2014 is blue but for jan 2014 also it should show blue as due days>14 .
Is there any way i can do that ??
Eg: data set returns value as :
Due Days CRD Month
Month_Nbr Year Notes _Count
5 2014-11-28 00:00:00.000 December 12 2014
0
5 2014-11-28 00:00:00.000 February 2 2015
0
5 2014-11-28 00:00:00.000 January 1 2015
0
5 2014-11-28 00:00:00.000 November 11 2014 1
5 2014-11-28 00:00:00.000 October 10 2014 0
5 2014-11-28 00:00:00.000 September 9 2014 0
Matrix is of the form :
YEAR
MONTH
CRD Notes_count -
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 to tell if column value has changed for use in workflow actions
Hello,
I am using Sharepoint 2010 and for one of my Lists, I am using a general list workflow. What I need to be able to do is determine if a column value has change (say an "Assigned To" field) because I only want to take some action if that particular
value has changed. I want to be able to have a workflow action that would be something like:
If Current Item: Assigned To not equals [OLD VALUE]
I have found some web searches that talk about creating a duplicate list or duplicate (but hidden) column but that doesn't seem to be the way to go. I have document versioning set but don't if that can be used to help with this. One possible
thought (although I haven't tried it to see if it works) is to create local variables and have the values in the variables be the "old value". Just not sure if there is a best practices for doing this.
Thanks for any thoughts - PeterHelen,
Not sure I fully understand your goal. We don't use "tasks" at all but if you are looking to have your workflow check certain valus and be able to send email messages to people based on whatever, then you can certainly do that (as long as your Sharepoint
has the email setup. We do this for alot of workflow tasks.
So, in the workflow you can have a blanket statement like what I previously listed:
if Current Item:hiddenStatus not equals Current Item:Status
.... do something
or you can do something like:
if Current Item:hiddenStatus equals "In-Progress"
.... do something
Else if Current Item:hiddenStatus equals "Completed"
.... do something
or combine the two and do nested "if" statements. Then you add an email statement wherever you need it like:
if Current Item:hiddenStatus equals "Completed"
then email "these users"
To add the email part, just type in "email" on the line where you want to add a statment. There is only one option to choose from. That will display the line "then email these users". The "these users" will be a link. When you
click it you will get a popup to add the email info. We typically will send the email to a user (or users) that are already listed in one of the PeoplePicker fields. On the email form, you can type in your own text, designate that a value is based
on a column value (like our PeoplePicker), designate that a value is based on a workflow variable, add a link to the current item, etc. To get to these options you will click the button to the right of the fields or use the "Add or Change Lookup" button
in the bottom-left for the text area. There is alot you can set in the mail.
Does this help answer your question?
- Peter -
How to insert column values into database as rows
Hi,
I have 8 columns and some not null columns. Based on not null columns I want to insert into table as rows. The 8 columns may contain values or no value. If the first column contains data, then I have to insert into one row. if the second column contains data I have to insert a row and in second column. respectively...So How can I insert column values into rows. Can I write 8 insert statements. (OR) is it possible to insert data from columns using where clause.
Please help me out....
Thanks in AdvanceLines Table:
line_id, orcl_bank_account_num, product_type, service_type, lease_type,
funding_type, cpi, billing_frequency_unit_cd , annual_due_date ,
pricing_start_date, pricing_end_date, install_date, contract_end_date ,
prdct_replacement_cost_amt, cradle_replacement_amt, supranet_contract,
issuance_fee, board_inactive_date, header_id, creation_date, last_modified_date,
created_by_nam, modified_by_nam, activeinactive_flg, prdct_bill_amt_yr1,
prdct_bill_amt_yr2, prdct_bill_amt_yr3, prdct_bill_amt_yr4, prdct_bill_amt_yr5,
prdct_bill_amt_yr6, prdct_bill_amt_yr7, prdct_bill_amt_yr8, activation_fee_yr1,
activation_fee_yr2, activation_fee_yr3, activation_fee_yr4, activation_fee_yr5,
activation_fee_yr6, activation_fee_yr7, activation_fee_yr8,
In this table the columns structure is :
-- PRDCT_BILL_AMT_YR (1 to 8) NUMBER(14,4)
-- ACTIVATION_FEE_YR (1 to 8) NUMBER(8,2)
I have one more table:
PRDCT_INS_AMT NUMBER(14,4)
ACTIVATION_FEE_AMT NUMBER(14,4)
I want to insert prdct_bill_amt_yr (1 to 8) columns data into PRDCT_INS_AMT column. similarly activation_fee (1 to 8) columns data.
But the data should be inserted based product_type, service_type, lease_type columns values. (These 3 columns may contain upto 45 combinations). -
How to avoid to check if a column value is NULL?
Hi, I'm a newbee in Oracle.
I have a procedure like this:
create or replace
PROCEDURE get_employee
v_first_name IN VARCHAR2 DEFAULT NULL ,
v_middle_name IN VARCHAR2 DEFAULT NULL ,
v_last_name IN VARCHAR2 DEFAULT NULL ,
To select rows with matching multiple column values, I can simply do this:
SELECT *
FROM employee
WHERE first_name = v_first_name
AND middle_name = v_middle_name
AND last_name = v_last_name
The problem is v_middle_name can be NULL. This means,
I need check if v_middle_name is NULL, and if it is, I need use "IS NULL" instead, like this:
SELECT *
FROM employee
WHERE first_name = v_first_name
AND middle_name IS NULL
AND last_name = v_last_name
It seems very cumbersome to do a check for each column that can be null.
Is there a way that I do not need to do a check for every column?
or is it better to avoid having NULL values in those columns (and replace them with, say a space) ?
Thanks in advance.
SimonNormally, you would do something like
SELECT *
FROM employee
WHERE first_name = NVL( v_first_name, first_name )
AND middle_name = NVL(v_middle_name, middle_name )
AND last_name = NVL(v_last_name, last_name )Of course, if you can ensure that NULL data is not allowed (without creating phony non-NULL data), that is a good thing. In most systems, for example, it is probably reasonable to require a non-NULL first and last name. But you almost certainly cannot require a middle name.
Justin -
Referencing Aggregated Column Value in Where Clause
Hello -
I'm trying to determine how I can accomplish the following in the most straightforward, efficient way.
Among other things, I'm selecting the following value from my table:
max(received_date) as last_received_dateI also need to evaluate the "last_received_date" value as a condition in my where clause. However, I can't reference my aliased "last_received_date" column value, and when I try to evaluate max(received_date) in the where clause, I get the "group function is not allowed here" error.
Does anyone know of a good workaround?
Thanks,
ChristineHi,
Column aliases can be used in the ORDER BY clause: aside from that, they cannot be used in the same query where they are defined. The workarounds are
(a) define the alias in a sub-query, and use it in a super-query, like Someoneelse did, or
(b) repeat the aliased expression, as in the HAVING-clause, below.
Aggregate functions are computed after the WHERE-clause. (That explains why you can do things like
SELECT MAX (received_date) last_received_date_2008
FROM table_x
WHERE TO_CHAR (received_date, 'YYYY') = '2008';).
The HAVING-clause is like the WHERE-clause, but it is applied after the aggregate functions are computed, e.g.
SELECT deptno
, MAX (recieved_date) AS last_received_date
FROM table_x
GROUP BY deptno
HAVING MAX (received_date) > SYSDATE - 7 -- Only show deptartments with activity in the last week
; -
Re-enable column values in tabular form before submit
Hi,
I am using Apex 4.1. I have a tabular form. I created a dynamic action to disable some of the columns on page load.
It works fine and I am able to add new rows. But when I click submit, all the column values are null and it is throwing validation error.
I need to enable all the columns before submit. I invoked a javascript to enable all the fields, but still i get the validation error.
I also tried creating dynamic action to enable the values but still I get the validation error becuase the values are all null.
Please help me to resolve this issue.
Thanks
SRPer HTML standards, disabled items are not submitted. Therefore if they are disabled at the point of submit, their corresponding session state values will be null (as you discovered). Apex validates against what is is session state, not against what is on your screen, so since the values didn't get submitted (per standards for HTML disabled items) that's why Apex thinks they are null.
Before submit, re-enable the items/columns and it should work fine.
Edit: Sorry, looks like you are doing this already. But are you doing it in the right order?
How I have done it is:
1. Change the save/apply changes button (whatever is doing the submit) to call a URL.
2. Make the URL call javascript, such as:
javascript:formSave('SAVE');
3. Put a javascript function on your region or page (hypothetical example):
<script type="text/javascript">
function formSave(pRequest)
{ //Put all checking and processing prior to form save here.
var vRequest="SAVE"; //default value taken from orig. "apply changes" button
if(pRequest)
vRequest=pRequest;
enableAllCells(gMaintActionCol); //Put whatever code you have to re-enable your tabular form items here, must fire before apex.submit.
//Do the save
apex.submit(vRequest);
</script>Edited by: gti_matt on Dec 1, 2011 3:04 PM -
Custom row-fetch and how to get column values from specific row of report
Hi -- I have a case where a table's primary key has more than 3 columns. My report on the
table has links that send the user to a single-row DML form, but of course the automatic
fetch won't work because 1) I can't set more than 3 item values in the link and 2) the
auto fetch only handles 2 PK columns.
1)
I have written a custom fetch (not sure it's the most elegant, see second question) that is working
for 3 or few PK columns (it references the 1-3 item values set in the link), but when there are
more than 3, I don't know how to get the remaining PK column values for the specific row that was
selected in the report. How can I access that row's report column values? I'll be doing it from the
form page, not the report page. (I think... unless you have another suggestion.)
2)
My custom fetch... I just worked something out on my own, having no idea how this is typically
done. For each dependent item (database column) in the form, I have a source of PL/SQL
function that queries the table for the column in question, using the primary key values. It works
beautifully, though is just a touch slow on my prototype table, which has 21 columns. Is there
a way to manually construct the fetch statement once for the whole form, and have APEX be smart
about what items get what
return values, so that I don't have to write PL/SQL for every item? Because my query data sources
are sometimes in remote databases, I have to write manual fetch and dml anyway. Just would like
to streamline the process.
Thanks,
CarolHI Andy -- Well, I'd love it if this worked, but I'm unsure how to implement it.
It seems I can't put this process in the results page (the page w/ the link, that has multiple report rows), because the link for the row will completely bypass any after-submit processes, won't it? I've tried this in other conditions; I thought the link went directly to the linked-to page.
And, from the test of your suggestion that I've tried, it's not working in the form that allows a single row edit. I tried putting this manually-created fetch into a before header process, and it seems to do nothing (even with a hard-coded PK value, just to test it out). In addition, I'm not sure how, from this page, the process could identify the correct PK values from the report page, unless it can know something about the row that was selected by clicking on the link. It could work if all the PK columns in my edit form could be set by the report link, but sometimes I have up to 5 pk columns.
Maybe part of the problem is something to do with the source type I have for each of the form items. With my first manual fetch process, they were all pl/sql functions. Not sure what would be appropriate if I can somehow do this with a single (page level?) process.
Maybe I'm making this too hard?
Thanks,
Carol
Maybe you are looking for
-
Creating variables in AS3 that Captivate can read.
I have a Flash project written in AS3 and I am colaborating with someone that is writing a Captivate training program. The Flash program is a little game and I need to store a couple integers readable by Captivate. I would like to just create the
-
Why is there no "back" arrow to go back to the previous viewed site? == This happened == Every time Firefox opened
-
Help my gophone won't work?
I was on the 45 monthly plan and it renews on the 3rd. And I was on the gophone site looking at rate plans and I hit the back button and it changed me to the 60 dollar plan and then I changed if back and now it says I have to pay 45 dollars to activa
-
Purchase requisition date in SNP planning book and product view not in sync
Hello Experts, We have an issue with display of purchase requisition date in SNP planning book. We are manually entering the PR's for a perticular week from Monday - Sunday(01.09.2014- 07.09.2014), when the last PR is entered on 07.09.2014, it does n
-
ITunesHelper.ex.exe error message
Every time I start up my computer I get the following Program error message: "iTunesHelper.ex.exe has generated errors and will be closed by Windows. You will need to restart the program. An error log has been generated" I have Windows Professional 2