How to select the first record?
Hi,
Could anyone tell me how to select the first record of every
group?
ex, I want to find out every customer their first purcaseDate.
CustomerID PurcaseDate Region
A00000001 2001/01/01 AA
A00000001 2001/02/02 AA
A00000002 2001/03/01 AC
A00000002 2001/05/07 AC
A00000003 2001/03/05 DD
result:
A00000001 2001/01/01 AA
A00000002 2001/03/01 AC
A00000003 2001/03/05 DD
Thanks
Vincent,
You could do it as Carl suggested, with a couple of
corrections. You would need to include the CustomNo column in
the order by clause of your cursor. You would also need to add
a where clause to your update statement, otherwise everywhere
Region in the table would be updated with the same value of the
last CustomNo in the cursor, regardless of the CustomNo in the
table. See corrected code below:
DECLARE
CURSOR cust
IS
SELECT DISTINCT CustomNo, Region, Purchase_Date
FROM my_table
ORDER BY CustomNo, Purchase_Date, Region;
c_customer VARCHAR2 (9) := '...';
c_region VARCHAR2 (2) := '..';
cntr NUMBER := 0;
BEGIN
FOR x IN cust
LOOP
IF x.CustomNo != c_customer
THEN
c_customer := x.CustomNo;
c_region := x.Region;
ELSE
UPDATE my_table
SET Region = c_region
WHERE CustomNo = c_customer;
cntr := cntr + 1;
IF cntr = 25
THEN
COMMIT;
cntr := 0;
END IF;
END IF;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
Another option is that you could just use one update statement,
like this:
UPDATE my_table a
SET Region =
(SELECT DISTINCT Region
FROM my_table b
WHERE (CustomNo, Purchase_Date) IN
(SELECT CustomNo, MIN (Purchase_Date)
FROM my_table
GROUP BY CustomNo)
AND a.CustomNo = b.CustomNo)
Barbara
Similar Messages
-
Windows Explorer - How to select the first item once we open the explorer?
Hi,
In Windows Explorer - How to select the first item once we open the explorer using keyboard shortcut. I typically use downward arrow, it goes to the 2nd item in the list, then I again have to use upward arrow to go up.
Is there a shortcut to select the first item in the list using keyboard? This is not specific to Windows 7, it has been the behavior even in previous Windows OS versions.
Thanks,
MallikOpening a Microsoft Ticket is probably your best option. Unfortunately the change will not happen immediately and it will require multiple users to send in a ticket. I hope this ends up being resolved though.
Good luck Mallik
Also, don't forget to mark the post as answered so that it does not show up as unanswered to help others who search the forums.
Agreed, MS wont change it easily. :)
Arnav Sharma | http://arnavsharma.net/ Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading
the thread. -
How to select the first row for each pair of values ?
Hi guys, am hoping someone can help me here.
If I have 4 tables - Building, Dept, Emp, Text
B D E T
A 1 Z blah1
A 1 X blah2
A 1 W blah3
A 2 V blah4
A 2 G blah5
A 2 H blah6
B 1 K blah7
B 1 L blah8
B 2 E blah9
B 2 F blah0
I need a query that will bring back the first bit of text for each pair of building and dept records.
So the results I would get would be
A 1 blah1
A 2 blah4
B 1 blah7
B 2 blah9
blah2 wouldnt be returned, because we have already had a record from the Building A, dept 1 pairing.
Likewise we wouldnt get blah5 because we have already had a record from the Building A, dept 2 pairing.
Can this be done in 1 query??
Thanks all.
Message was edited by:
Scott Hillierhere you go
SQL> drop table t;
Table dropped.
SQL> create table t(B char(1), D integer, E char(1), T varchar2(5));
Table created.
SQL> insert into t values('A', 1, 'Z', 'blah1');
1 row created.
SQL> insert into t values('A', 1, 'X', 'blah2');
1 row created.
SQL> insert into t values('A', 1, 'W', 'blah3');
1 row created.
SQL> insert into t values('A', 2, 'V', 'blah4');
1 row created.
SQL> insert into t values('A', 2, 'G', 'blah5');
1 row created.
SQL> insert into t values('A', 2, 'H', 'blah6');
1 row created.
SQL> insert into t values('B', 1, 'K', 'blah7');
1 row created.
SQL> insert into t values('B', 1, 'L', 'blah8');
1 row created.
SQL> insert into t values('B', 2, 'E', 'blah9');
1 row created.
SQL> insert into t values('B', 2, 'F', 'blah0');
1 row created.
SQL> select b,d,t
2 from (
3 select row_number() over(partition by b, d order by b,d) rno,b,d,t
4 from t)
5 where rno = 1;
B D T
A 1 blah1
A 2 blah4
B 1 blah7
B 2 blah9karthick
http://karthickarp.blogspot.com/ -
Hi All,
i have a table like below. i need the next record after the status_code = 5.
pls help
JOB
job_id prnt_job_id status_code
2282 2281 4
2283 2281 4
2284 2281 5
2287 2281 2
2289 2281 2next record after status_code = 5 is
2287 2281 2SELECT next_job_id,
next_prnt_job_id,
next_status_code
FROM
( WITH t1 AS
( SELECT 2282 job_id, 2281 prnt_job_id, 4 status_code FROM dual
UNION ALL
SELECT 2283, 2281, 4 FROM dual
UNION ALL
SELECT 2284, 2281, 5 FROM dual
UNION ALL
SELECT 2287, 2281, 2 FROM dual
UNION ALL
SELECT 2289, 2281, 2 FROM dual
SELECT lead(job_id,1) over(order by rownum) next_job_id,
job_id,
lead(prnt_job_id,1) over(order by rownum) next_prnt_job_id,
prnt_job_id,
lead(status_code,1) over(order by rownum) next_status_code,
status_code
FROM t1
WHERE status_code=5 -
How to Select the duplicate records in the table
Hi
SELECT * FROM gin A WHERE A.sale_order_id IN (
SELECT sale_order_id FROM gin GROUP BY sale_order_id
HAVING COUNT(sale_order_id)>1)
AND A.ROWID!=MIN (ROWID))
ERROR at line 4:
ORA-00934: group function is not allowed here
Thanks
Vikas SinghalTry this out
select *
from gin a
where a.rowid > any (select b.rowid
from gin b
where a.col1 = b.col1
and a.col2 = b.col2);
Regards
Ozy
[www.oraclevillage.com]
Edited by: Ozy on Feb 4, 2009 4:12 AM -
How to get the first and the last record of every month within a time range in sql
I am trying to get the first record and the last record of each and every month in a given date range using sql. I have a sample code
where i have just selected everything within the date range now i have to extract the first and the last records of each and every month.
SELECT PurOrderNum,
OrderDate
FROM Purchasing.PurOrder
WHERE OrderDate >= '2013-02-28'
AND OrderDate <= '2014-12-29'SELECT PurOrderNum,
OrderDate
FROM
SELECT PurOrderNum,
OrderDate,
MAX(OrderDate) OVER (PARTITION BY DATEDIFF(mm,0,OrderDate)) AS MaxDate,
MIN(OrderDate) OVER (PARTITION BY DATEDIFF(mm,0,OrderDate)) AS MinDate
FROM Purchasing.PurOrder
WHERE OrderDate >= '2013-02-28'
AND OrderDate <= '2014-12-29'
)t
WHERE OrderDate = MaxDate
OR OrderDate = MinDate
Please Mark This As Answer if it solved your issue
Please Vote This As Helpful if it helps to solve your issue
Visakh
My MSDN Page
My Personal Blog
My Facebook Page -
How do I blank my BI Pub report parameter from returning the first record when output to HTML?
Hi all.
I have a report that has several parameters A, B, C D.
I include these at the head of the rtf report.
I run the report WITHOUT entering anything for parameters A & B.
When the report generates (returning all the expected correct records), it enters into parameter fields A & B at the top of the report the first records’ A & B fields when they should be blank.
IS there something I need to do in the rtf form field Help text along with the field name?
ThanksForLoooking..
Malcwithin the rtf of the report, within the form-field-help-text, this should work..
<?xdofx:decode(<parameter name>,'','',<parameter name>)?>
if the parameter is left blank, then don't put anything in the rtf field, otherwise put in anything that is selected in the parameter.
or this..
<?xdofx:if <parameter name>='' then 'All Params Included' else <parameter name> end if?>
..yet, when the parameter is left blank, the parameter field in the report generated is populated with whatever is in the first line of the parameter in the report.
I also can't find anything in the old rdf that is causing this either. Help! -
How to take a value of the first record/occurrence and the last record?
Hi experts
Can anyone help me to tell me:
How to make IP can take a value of the first record/occurrence and the last record in CSV file?
I need to take the first and last to put StarTime of first record y StopTime of last record in the target file
This is my Original CSV File
20110820,220DNE0220,140.13 ,0.000 ,E01,0
20110820,240FGC4280,103.80 ,0.000 ,E01,0
20110821,220DNE0220,142.58 ,0.000 ,E01,0
20110821,240FGC4280,88.70 ,0.000 ,E01,0
20110822,220DNE0220,151.92 ,0.000 ,E01,0
20110822,240FGC4280,91.47 ,0.000 ,E01,0
Where:
The firts field is date.
I require it so my Target File
20110820,20110822,140.13 ,0.000 ,E01,0
20110820,20110822,103.80 ,0.000 ,E01,0
20110820,20110822,142.58 ,0.000 ,E01,0
20110820,20110822,88.70 ,0.000 ,E01,0
20110820,20110822,151.92 ,0.000 ,E01,0
20110820,20110822,91.47 ,0.000 ,E01,0
Thaks..Hi lizcam,
A. Use FCC at sender side, it will convert CSV to XML like this
Input XML
<documentName>
<recordset>
<record>
<Time>20110820</Time>
<ID>220DNE0220</ID>
<Quan>140.13</Quan>
<Volume>0.000</Volume>
<Auc>E01</Auc>
<No>0</No>
</record>
</recordset>
</documentName>
Create a target DT like this
Output XML
<recordset>
<record>
<StartTime>20110820</StartTime>
<EndTime>20110822</EndTime>
<Quan>140.13</Quan>
<Volume>0.000</Volume>
<Auc>E01</Auc>
<No>0</No>
</record>
</recordset>
In MM,
1.Time -> CopyValue[0] -> StartTime
2.Time -> below UDF -> EndTime
3.Quan -> Quan
4.Volume -> Volume
5.Auc -> Auc
6.No -> No
UDF u2013 Execution type u2013 All values of Queue
public void getLastTimeValue(String[] inputEndTime, ResultList result, Container container) throws StreamTransformationException{
result.addValue(inputEndTime[inputEndTime.length-1]);
B. At receiver use again FCC to convert XML to CSV.
FYI. If you want to optimize more, you can use
1.globalContainer concept OR
2.u201CAttributes and Methodsu201D, declare are String. Store the EndTime using one UDF and write another UDF to retrieve it.
Regards,
Raghu_Vamsee -
How to view the first and the last frame of a clip or a selected range in the time line?
How to view the first and the last frame of a clip directly or in a selected range in the time line ? Up arrow and down arrow keys changes only between the first frame of the consecutive clips. Even ; and ' does the same. I mean what is the shortcut keys for first to last and last to first frame of a clip? Help please.
SELECT PurOrderNum,
OrderDate
FROM
SELECT PurOrderNum,
OrderDate,
MAX(OrderDate) OVER (PARTITION BY DATEDIFF(mm,0,OrderDate)) AS MaxDate,
MIN(OrderDate) OVER (PARTITION BY DATEDIFF(mm,0,OrderDate)) AS MinDate
FROM Purchasing.PurOrder
WHERE OrderDate >= '2013-02-28'
AND OrderDate <= '2014-12-29'
)t
WHERE OrderDate = MaxDate
OR OrderDate = MinDate
Please Mark This As Answer if it solved your issue
Please Vote This As Helpful if it helps to solve your issue
Visakh
My MSDN Page
My Personal Blog
My Facebook Page -
How to return to the first record of a multiple row cursor (Forms 6i)
Hi all,
I had a bit of a search through here, but couldn't quite find the answer I'm after.
I have a multiple row cursor, which I feed into a multi-row block in Forms 6i. I have the following code:
OPEN CURSOR;
LOOP
FETCH CURSOR INTO :FIELD1, :FIELD2, :FIELD3, :FIELD4;
... do other code not related with cursor
EXIT WHEN CURSOR%NOTFOUND;
NEXT_RECORD;
END LOOP;Now, I use the Forms built-in NEXT_RECORD to move down through the records on the form and fill in each row from the db. However, once the block loads (this works correctly), the current item (ie where the typing cursor is left) is on the last record.
Obviously, I need the current item (after loading) to be on the first record. I tried using the Forms built-in FIRST_RECORD after the END LOOP command, but that gives me an ORA-06511 error (An attempt was made to open a cursor that was already open).
Does anyone know how I might be able to return to the first record of the block correctly?
Thanks in Advance,
Chris.Ok, I feel like a bit of a dolt.
I found that all cursors had to be closed before navigating back to the first record.
Case closed! :) -
How to get the first and last record
Hai All
I have table called T1 and there are more than 8 lakhs records and i have a column called Timestamp so i need to get the first record value and time stampvalue and last record and time stamp value so that i can conclude that For Example
form 13 june to 15 june data are here
Kind Regards
SrikkanthMSomething like this can also indicate the first and last rows as you query...
SQL> select empno, ename, hiredate
2 ,case row_number() over (order by hiredate)
3 when 1 then 'First Row'
4 when count(*) over () then 'Last Row'
5 end as flag
6 from emp;
EMPNO ENAME HIREDATE FLAG
7369 SMITH 17/12/1980 00:00:00 First Row
7499 ALLEN 20/02/1981 00:00:00
7521 WARD 22/02/1981 00:00:00
7566 JONES 02/04/1981 00:00:00
7698 BLAKE 01/05/1981 00:00:00
7782 CLARK 09/06/1981 00:00:00
7844 TURNER 08/09/1981 00:00:00
7654 MARTIN 28/09/1981 00:00:00
7839 KING 17/11/1981 00:00:00
7900 JAMES 03/12/1981 00:00:00
7902 FORD 03/12/1981 00:00:00
7934 MILLER 23/01/1982 00:00:00
7788 SCOTT 19/04/1987 00:00:00
7876 ADAMS 23/05/1987 00:00:00 Last Row
14 rows selected.
SQL> -
How can I select the random records in u201Cstep loopu201D.
Hi, Experts,
I am using step loop to display the data on screen now I want to select the random records from this step loop display. Like 1st 3rd or 5th record, is there any way to select the records,
like I did in Table control there was a filed in internal table named marked char length 1, I gave this field name in u201Cw/ SelColumnu201D it fill that field with u2018Xu2019 from where I can get the info about the selected records, in this way I can easily perform the operation on that internal table with the help of that marked field.
Is there any way to select the records in step loop too ?
Kind Regards,
Faisalthanks for replay shwetali,
but i just gave you example of random records with 1st 3rd and 5th my goal is not select only these records. i want to select the random mean any records from the step loop display.
like we can select from the table control. and when select any record it place 'X' in the given internal table field.
Thanks and kind Regards,
Faisal -
How to populate only the first record by default when the page opens?
Dear Members,
I am using OAF R12 and my requirement is as follows:-
When the user opens the custom OAF R12 page, I just want to query the first record from the underlying VO.
For example, If the table has 10 rows, I just want to query the first row as soon as the page opens.
I have written the below code:-
Process Request Method of the CO:-_
am.invokeMethod("initHeaders");
Code in AM_
public void initHeaders()
HeadersVOImpl hVO = getHeadersVO1();
hVO.initQueryHeaders();
Code in VO_
public void initQueryHeaders()
executeQuery();
As it is very clear that with the above code all the records will be queried. I have to add something before the executeQuery() method in the VO to just query the first record.
Can any one please help me by guiding me what I should add before the executeQuery() method in the VO?
Many thanks in advance.
Regards,
R4S.Hello Gyan,
Many thanks for your reply.
The below line in your code is throwing incompatible type error:
OARow row = vo.first();
Can you please help me.
Regards,
R4S -
How to select the inklist items in the active document?
From the below code, i can get the inklist information. But i could not know how to select the spot(InkType.CUSTOMINK) color items in the illustrator activedocument.
var docRef=app.activeDocument;
var S1 = 0;
for(i=docRef.inkList.length-1;i>=0;i--)
var inkRef=docRef.inkList[i];
var inkRefName=inkRef.name;
if(inkRef.inkInfo.kind=="InkType.CUSTOMINK")
alert(inkRefName);
//Select the items?
Could You please advice me.Hi Giri
>> First define the Posting Period for 2010 and create the numbering series for documents for 2010.
Done.
>> set as default of numbering series for 2010.
Done.
so this series will come defaultyly,if u want to post some document in 2009 then select the numbering series in documents manually,
>> How?
I am trying with a FMS to update the invoice and it's ok, but the journal entry takes 2010 number series.
I have:
- one serie DEC09.
- other JAN10.
I want to record invoices with date in Dec 2009 now and the journal entries with DEC09 serie, and invoices with date in Jan 2010 and the journal entries with JAN10 at the same time.
Is this possible?
Kind regards.
Agustin Marcos Cividanes -
How to handle the bad record while using bulk collect with limit.
Hi
How to handle the Bad record as part of the insertion/updation to avoid the transaction.
Example:
I am inserting into table with LIMIT of 1000 records and i've got error at 588th record.
i want to commit the transaction with 588 inserted record in table and log the error into
error logging table then i've to continue with transaction with 560th record.
Can anyone suggest me in this case.
Regards,
yuva>
How to handle the Bad record as part of the insertion/updation to avoid the transaction.
>
Use the SAVE EXCEPTIONS clause of the FORALL if you are doing bulk inserts.
See SAVE EXCEPTIONS in the PL/SQL Language doc
http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/tuning.htm
And then see Example 12-9 Bulk Operation that continues despite exceptions
>
Example 12-9 Bulk Operation that Continues Despite Exceptions
-- Temporary table for this example:
CREATE TABLE emp_temp AS SELECT * FROM employees;
DECLARE
TYPE empid_tab IS TABLE OF employees.employee_id%TYPE;
emp_sr empid_tab;
-- Exception handler for ORA-24381:
errors NUMBER;
dml_errors EXCEPTION;
PRAGMA EXCEPTION_INIT(dml_errors, -24381);
BEGIN
SELECT employee_id
BULK COLLECT INTO emp_sr FROM emp_temp
WHERE hire_date < '30-DEC-94';
-- Add '_SR' to job_id of most senior employees:
FORALL i IN emp_sr.FIRST..emp_sr.LAST SAVE EXCEPTIONS
UPDATE emp_temp SET job_id = job_id || '_SR'
WHERE emp_sr(i) = emp_temp.employee_id;
-- If errors occurred during FORALL SAVE EXCEPTIONS,
-- a single exception is raised when the statement completes.
EXCEPTION
-- Figure out what failed and why
WHEN dml_errors THEN
errors := SQL%BULK_EXCEPTIONS.COUNT;
DBMS_OUTPUT.PUT_LINE
('Number of statements that failed: ' || errors);
FOR i IN 1..errors LOOP
DBMS_OUTPUT.PUT_LINE('Error #' || i || ' occurred during '||
'iteration #' || SQL%BULK_EXCEPTIONS(i).ERROR_INDEX);
DBMS_OUTPUT.PUT_LINE('Error message is ' ||
SQLERRM(-SQL%BULK_EXCEPTIONS(i).ERROR_CODE));
END LOOP;
END;
DROP TABLE emp_temp;
Maybe you are looking for
-
Disk Volume Needs to be repaired... corrupt files..
Hi, I am using a macbook pro with the latest version of os x 10.6.2 I went on utilities, Disk utilities to verify the disk. After It has been verified..it generated those two message indicating " The volume Macintosh HD was found corrupt and needs to
-
I want to enable an disabled account how do i do that?
i disabled an account that i had about 2 years ago because someone else was trying to get into the account... and now i have realized i bought about $100 dollars worth of music and i want to get some of that music back. how do i enable my account aga
-
Two Manual Tabular forms in a same page
Since i have to create two tabular forms in a same page, i created tabular forms manually. Both the forms are working fine if the columns are not defined as conditional display. Based on item value set by user, few fields should be either displayed a
-
JNI with Korean characters anybody??
Can't handle Korean characters through jstring or jobject. How do I do this? is it a Unicode string or a UTF string? any help would be rewarded!!!
-
Authorization over UIComponents with JSF 2.0
Hi, I'm want to access the components of a requested view and to alter the "rendered" attribute based on authorization rules. Using a Phase Listener I got the components, but only after render response phase, where alterations don't make any effect b