How to pick max value from a column of a table using cursor and iteration
Hello Everybody
I have a table loan_detail
and a column in it loan_amount
now i want to pick values from this table using cursor and then by using iteration i want to pick max value from it using that cursor
here is my table
LOAN_AMOUNT
100
200
300
400
500
5600
700i was able to do it using simple loop concepts but when i was trying to do this by using cursor i was not able to do it .
Regards
Peeyush
SQL> SELECT MAX(sal) Highest_Sal,MIN(sal) Lowest_Sal FROM emp;
HIGHEST_SAL LOWEST_SAL
5000 800
SQL> set serverout on
SQL> DECLARE
2 TYPE tmp_tbl IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3 sal_tbl tmp_tbl;
4 CURSOR emp_sal IS
5 SELECT sal FROM emp;
6 counter INTEGER := 1;
7 BEGIN
8 FOR i IN emp_sal LOOP
9 sal_tbl(i.sal) := counter;
10 counter := counter + 1;
11 END LOOP;
12 DBMS_OUTPUT.put_line('Lowest SAL:' || sal_tbl.FIRST);
13 DBMS_OUTPUT.put_line('Highest SAL:' || sal_tbl.LAST);
14 END;
15 /
Lowest SAL:800
Highest SAL:5000
PL/SQL procedure successfully completed.
SQL> Even smaller
SQL> DECLARE
2 TYPE tmp_tbl IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3 sal_tbl tmp_tbl;
4 CURSOR emp_sal IS
5 SELECT sal FROM emp;
6 counter INTEGER := 1;
7 BEGIN
8 FOR i IN emp_sal LOOP
9 sal_tbl(i.sal) := 1;
10 END LOOP;
11 DBMS_OUTPUT.put_line('Lowest SAL:' || sal_tbl.FIRST);
12 DBMS_OUTPUT.put_line('Highest SAL:' || sal_tbl.LAST);
13 END;
14 /
Lowest SAL:800
Highest SAL:5000
PL/SQL procedure successfully completed.
SQL> Edited by: Saubhik on Jan 5, 2011 4:41 PM
Similar Messages
-
How can I take data from specific column in a table using VBA
Hi
I do not know how to take the values from the last column in a table, I know how to look over each row but I cannot take the last column values for each row I pass by.
This is the table for example:
| Date | Account1 | Account2 | Description | Amount |
|12/03/2008 | 123-32 | 325-78 | None | $50 |
|12/03/2008 | 123-32 | 325-78 | None | $20 |
|12/03/2008 | 123-32 | 325-78 | None | $10 |
|12/03/2008 | 123-32 | 325-78 | None | $40 |
I am using this code as a reference that User FiveNines gave me to loop through all the rows in the table, so What I need is to take the values of the column Amount for each row I go through.
Private Sub RSWVBAPage_afterPlay()
Dim tbl As HTMLTable
Dim tblRow As HTMLTableRow
Dim tblCell As HTMLTableCell
Dim strValue As String
Dim rButton As HTMLInputElement
' ******** This would be your global variable. I put this so that values are seperated by a semicolin you can use what ever format works for you.
strValue = "03/22/2008;03/22/2008;*************1977;*************1977;$25.25;Jan, Jun, Jul, Dec"
' Strip out the ; for inner text comparison
strValue = Replace(strValue, ";", "")
' This will get the table but can be modifoed to be more specific
Set tbl = RSWApp.om.FindElement(, "TABLE")
' This loops through all the rows in the table until a match to the strValue is found
For Each tblRow In tbl.rows
If tblRow.innerText = strValue Then
RSWApp.WriteToLog "Tables", "Passed", "Row is Present", True
End If
Next
End SubHi Lippix.
This is code that will loop the rows then loop the cells and check for "$" which will return dollar values. I also attached a script.
Private Sub RSWVBAPage_afterPlay()
Dim tbl As HTMLTable
Dim tblRow As HTMLTableRow
Dim tblCell As HTMLTableCell
Dim strValue As String
' This will get the table but can be modifoed to be more specific
Set tbl = RSWApp.om.FindElement(, "TABLE")
' This loops through all the rows in the table until a match to the strValue is found
For Each tblRow In tbl.rows
For Each tblCell In tblRow.cells
' cycle through the cells and only report those with a dollar value by searching for $
If InStr(1, tblCell.innerText, "$") <> 0 Then
RSWApp.WriteToLog tblCell.innerText
End If
Next
Next
End Sub -
How to get a value from a column inside a table
Hi,
I have got the following problem. I have got a table with some data inside. And a new column, which is not in the dataprovider. Now i search for an opportunity to go through the rows of the table and check the value of this column. I cant do this with the dataprovider or the rowset. My question is now how can i do this? The table object doesnt seem to have a corresponding method.
Thanks in advance for help
Acinonyxthis is some code you can use (based on Winston's and others' tips):
put this in you page bean:
private HashSet selectedRows = new HashSet();
public boolean isSelected() {
TableRowDataProvider trdp = (TableRowDataProvider)getBean("currentRow");
if (trdp == null) {
return false;
RowKey rowKey = trdp.getTableRow();
if (this.selectedRows.contains(rowKey.getRowId()))
return true;
else
return false;
public void setSelected(boolean b) {
TableRowDataProvider trdp = (TableRowDataProvider)getBean("currentRow");
RowKey rowKey = trdp.getTableRow();
if (checkbox1.isChecked()) {
this.selectedRows.add(rowKey.getRowId());
Object v = this.t_fotoDataProvider.getValue("fieldName", rowKey);
} else {
this.selectedRows.remove(rowKey.getRowId());
and then bind the "selected" property of the checkbox column to the "selected" property of the page bean.
Now, eveytime the page is submitted, you can do something useful, for example, in the setSelected() method (the row that starts with Object v = ... get's the value of some field corresponding to the checked row)
Mauro -
How to get max value of a column in VO?
Hi ,
i need to find the max value of a column named insured value in VO.
Please help!!Hi,
If this value is cuming from the Query then u can easily go for MAX function. If its a user enterable value then u need to iterate through all the rows for that column to find the max of them.
Let me know.
Regards,
Gyan -
How to find max value from a list of numbers stored in a varray?
hi,
Can any body help me to get the max value from a list of numbers stored in a varray?
thanks in advance....
regards
LaxmiNarsimhaYes. Could you post what you have tried before we start helping you in this?
-
How do I add more than one column to a table using SQL?
Hi
I need to add 3 columns to a table using SQL
the syntax
"ALTER TABLE TEST ADD COLUMN newcol1 float";
works fine - for adding one coumn only.
For multiple columns I tried various permutations along the lines of
"ALTER TABLE TEST ADD (COLUMN newcol01 float, COLUMN new2 float,COLUMN new3 float)";
"ALTER TABLE TIPSTEST ADD COLUMN new1 float"
"ALTER TABLE TIPSTEST ADD COLUMN new2 float"
"ALTER TABLE TIPSTEST ADD COLUMN new3 float"
etc., but this doesn't work.
From a web search it sounds like SQL can only add one column at a time.
I have a workaround : create intermediate temporary tables , copying data and adding
one column at each stage. It seems a fairly awkward way of programming though.
Am I missing something simple : is there a way to add multiple columns in one go?
ThanksOK : solved an underlying problem with this one myself
for the code
String createString;
createString = "select COFFEES.* INTO NEWCOFFEES FROM COFFEES"; // example
Statement stmt;
try {
stmt = a_Globals.database1Connection.createStatement();
stmt.executeUpdate(createString);
stmt.close();
a_Globals.database1Connection.close();
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
} commenting out the line:
a_Globals.database1Connection.close();
Allowed the subsequent SQL statement(s) to work OK. Looks like this was the cause of several
difficulties, preventing me doing several SQL instructions in turn.
Thanks for the responses.
Mike2z -
Hi All,
Am using OPENROWSET to load the file data into table, here the problem is i need to map same input value to two different columns of table, As format file doesn't allow the duplicate numbers am unable to insert same value to two columns, please help me to
find a solution for this.
i can use only OPENROWSET because i need to insert some default values also which come based on file. only the problem is how to map same input value to two different columns of table. please give me the suggestions.
Thanks,
SudhakarFrom what you say:
INSERT tbl(col1, col2)
SELECT col1, col1
FROM OPENROWSET(....)
But I guess it is more difficult. You need to give more details. What sort of data source do you have? What does your query look like? The target table?
Erland Sommarskog, SQL Server MVP, [email protected]
Hi Erland,
Thanks for your response
my source file is text file with | symbol separate for ex:
1002|eTab |V101|eTablet|V100|Logic|LT-7|Laptops|SCM
Database table have columns like
column1,column2,column3...etc, now i need to insert same value from input file into two columns for ex:
the eTab value from text file has to be insert into column2 and column3 of
table
we cannot change format file like below one
for the above situation how can we insert eTab into column2 and column3
Thanks,
Sudhakar. -
Hi Team,
Am using BULK INSERT Format file option to load data into table from .txt file here am facing an issue i.e ibased on input file i need to insert default values to some columns of table so we can not declare it on table level, Can we give default values in
format file ? if we can give how it is ? or Any alternate possibilities to this scenario instead of BULK INSERT ?
Thanks,
SudhakarThanks for your response, here i don't have any rights to change table structure the table is created by different team, my work is to load data from file to table. Is there any chance to supply default values by*XML* format file instead of *.fmt* file
please let me know the possibility.
Again, no. If you want to supply default values that are not present in DEFAULT constraints in the table definition, you will need to write your own code. There are plentyfull of options, and I have mentioned some already.
Here are some more:
* Table-valued parameters, see here for examples:
http://www.sommarskog.se/arrays-in-sql-2008.html
* Use BULK INSERT to load data to a staging table, and then apply the default values when you copy from staging to target.
Erland Sommarskog, SQL Server MVP, [email protected] -
Return max value from one column from similar values in another
Hi folks. I've got a heap of temperature data to analyse and am looking for an easy way to extract a maximum internal temperature from a group of readings with the same external temperature. E.g. When the external temperature is 25.0 there are say 20 different internal temperature values that correspond with the different dates and times that this external temperature occurs. I'm looking for a formula I can use in a separate table with the external temperatures in a header column that will refer to the header column and extract the data from the original table. Any help appreciated! E.g. below...
Internal (°C)
Loc 4-Ambient
Index
Date
TimeValue
Time
External (°C)
22.7
1.70
465
11/10/12
0.33
8:00:00 am
21
22.7
0.90
466
11/10/12
0.35
8:30:00 am
21.8
24.5
2.10
417
11/9/12
0.33
8:00:00 am
22.4
23.4
1.00
513
11/11/12
0.33
8:00:00 am
22.4
22.9
0.20
467
11/10/12
0.38
9:00:00 am
22.7
25.8
2.90
370
11/8/12
0.35
8:30:00 am
22.9
25.7
2.80
371
11/8/12
0.38
9:00:00 am
22.9
25.7
2.80
372
11/8/12
0.40
9:30:00 am
22.9
25.6
2.70
373
11/8/12
0.42
10:00:00 am
22.9
24.5
1.60
418
11/9/12
0.35
8:30:00 am
22.9
25.6
2.50
369
11/8/12
0.33
8:00:00 am
23.1
max temp
21.0
21.1
21.2
21.3
21.4
21.5Hi Barry,
thanks for the response. Sorry, my example was not exactly clear. I'll take out the superfluous data and try and clarify my problem further. Based on the table below, I have sorted the data based on ascending external temperature (At these low external temperatures there are not so many data points, but when we get into the late 20s and 30s there can be hundreds of corresponding points). What I want to create is a separate table that I can graph, with the X axis showing the external temperature and the Y axis showing the maximum internal temperature that corresponds with each 0.1 degree increment of the external temperature.
external
internal
16.5
26.7
16.5
26.4
17
26.6
17.1
26.8
17.2
26.9
17.3
27.3
17.3
26.4
17.5
26.7
17.6
28.1
17.7
27.4
17.8
26.7
17.9
27.4
18
28.1
18
27.4
18.1
27
18.1
26.9
18.1
26.5
18.1
28
18.2
28.1
18.2
27.2
18.3
28.1
18.4
27.5
18.4
27.3
So my second table would just be two columns (I had 3 in the first example because I am doing a before and after painting the roof white comparison but I'm simplifying now) with my external temperature scale in column A in 0.1 degree increments. I am looking for a formula for column B that will give me the maximum internal temperature from my range of temperatures in the first table.
18.0
18.1
18.2
18.3
18.4
18.5
18.6
18.7
etcetera. The max for 18.1, for example, would turn out 28 based on the 4 corresponding data points in the first table.
Ideally I would like a formula using the MAX equation because I'm hoping that I can do a similar table for getting the average internal temperature for each of the external temperature increments too, but I didn't want to overcomplicate matters.
Cheers,
Neewok -
How to pick up value from list item
I created one form with list item(used to display all customer name).
From this i want to select one customer name and stored in a variable.. how can i do this..Use the Get_List_Element_Value function e.g.:
declare
v_customer_name VARCHAR2(50);
Begin
v_customer_name:= Get_List_Element_Value('customer_list', 1);
End; -
How to extract month number from a column in a table
select extract(MONTH FROM DATE to_char(a.creationdate)) from mytable a doesn't work . plz tell me what is wrong with this. is there another way to get the month in a numeric form apart from this.
Hi
SQL> SELECT TO_NUMBER(TO_CHAR(TO_DATE('March', 'Month'), 'MM')) MONTH_NUM FROM DUAL;
MONTH_NUM
3
SQL> SELECT TO_NUMBER(TO_CHAR(TO_DATE('December', 'Month'), 'MM')) MONTH_NUM FROM DUAL;
MONTH_NUM
12
SQL> SELECT TO_NUMBER(TO_CHAR(TO_DATE('August', 'Month'), 'MM')) MONTH_NUM FROM DUAL;
MONTH_NUM
8
This is what you want to do?
Hope it helps
Regards,
Tony G. -
How to pick the value of the line item in table control
My screen.
I have designed table control by referring internal table without using wizard.
I have placed button inside of that table control.
my requirement is by clicking the button it ll show the value of the line inside of the table control.
I have used "get cursor...".
But the problem is "it ll show only the cursor value of the front screen, if suppose i scroll the value of table control at the time also, it ll show the cursor value of the screen, it ll not show the exact value of the cursor inside of the table control.Wrong Forum Anand......!
-
How to select multiple values from the parameters in BI Publisher report
How to select multiple values from the parameter drop down in BI Publisher, and how to handle this mulitple values from the report sql...
Hi kishore,
I have used all the steps as you mentioned in your previous reply....including checking Mulitple Selection Check Box..
Iam able to get the results when I am selecting one value..
and also I am able to handle multiple values the in the query by using IN :Parameter, but seems when we select more than one value from the parameter drop down i think the Bi Publisher is sending the values in concatenated form something ilke
ex: "'ACCOUNT','HR','SALES'" ,and when trying to display the parameters values in the output, its throwing the error as 'missing right paranthesis' ....on the whole do you have any solution which would handle
1.Single selection.
2.Multiple selection.
3.'ALL' Values.
4.Separating the concatenated string into individual strings and dispaly them on the output of the report..etc..in case of Mulitple selection.
Ex:
Concatenated String from BI Publisher:"'ACCOUNT','HR','SALES'"
Expected Output on the report:ACCOUNT,HR,SALES
reply to this would be much appreciated....
thanks,
manoj -
From two given tables, how do you fetch the values from two columns using values from one column(get values from col.A if col.A is not null and get values from col.B if col.A is null)?
Hi,
Use NVL or COALESCE:
NVL (col_a, col_b)
Returns col_a if col_a is not NULL; otherwise, it returns col_b.
Col_a and col_b must have similar (if not identical) datatypes; for example, if col_a is a DATE, then col_b can be another DATE or it can be a TIMESTAMP, but it can't be a VARCHAR2.
For more about NVL and COALESCE, see the SQL Language manual: http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions119.htm#sthref1310
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
See the forum FAQ: https://forums.oracle.com/message/9362002 -
How to retrieve the max value from a cursor in procedure
Hi,
In a procedure, I defined a cursor:
cursor c_emp is select empno, ename, salary from emp where ename like 'J%';
but in the body part, I need to retrieve the max(salary) from the cursor.
could you please tell me how I can get the max value from the cursor in the procedure.
Thanks,
PaulHere is one sample but you should just get the max directly. Using bulk processing should be a last resort.
DECLARE
CURSOR c1 IS (SELECT * FROM emp where sal is not null);
TYPE typ_tbl IS TABLE OF c1%rowtype;
v typ_tbl;
max_sal number;
BEGIN
OPEN c1;
max_sal := -9999999999999;
LOOP --Loop added
FETCH c1 BULK COLLECT INTO v LIMIT 3; -- process 3 records at a time
-- process the records
DBMS_OUTPUT.PUT_LINE('Processing ' || v.COUNT || ' records.');
FOR i IN v.first..v.last LOOP
if v(i).sal > max_sal then
max_sal := v(i).sal;
end if;
DBMS_OUTPUT.PUT_LINE(v(i).empno);
END LOOP;
EXIT WHEN c1%NOTFOUND;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Max salary was: ' || max_sal);
END;
Processing 3 records.
7369
7499
7521
Processing 3 records.
7566
7654
7698
Processing 3 records.
7782
7788
7839
Processing 3 records.
7844
7876
7900
Processing 2 records.
7902
7934
Max salary was: 5000
Maybe you are looking for
-
Try to load new iPad Mini with reserve copy from my iMac
Hi, Try to load new iPad Mini with reserve copy from my iMac coming from my iPhone 4S. When selecting the copy I can not find the just made copy from my iPhone 4S. I see tree others, but not the latest from iPhone 4S. Is there a max on copies showed
-
Hello, I am using the SAX parser to parse a xml file which can be downloaded from the http://archive.godatabase.org/latest-termdb/go_daily-termdb.obo-xml.gz. The file is a gene ontology. My problem is that from the 25000 terms described in this file,
-
Will i lose my double data 4 gig for price of 2 gig if i renew/upgrade plan and phone?
i currently have a plan expiring in a week and would like to purchase an upgrade 4g phone from amazon but i have double data, 4 gig for 2 gig 30$ on my plan and dont want to lose this special..... will i lose my 4 gig data for 30$ if i renew and upgr
-
Just this morning, my BlackBerry Curve 8530 started to display unhandled exception messages, freeze almost all buttons/icons, and cease to function anywhere near normally. I'm being told all apps are having this error and they will terminate. How can
-
HELP!!