Returning records based on the max timestamp
I am writing a query to return records for each dat with max timestamp. so the query must return 1 record for each day with the latest timestamp. I am doing a partition over the timestamp column and wondering which analytic func gives the fastest result. This is in a data warehouse environment where there may be millions of recs.
thanks
My sample table has 3 columns - deptno, deptdesc, and eff_ts. There may be multiple rows for each day but I want to pick only the first (or last) record for each day.
This is one query I am thinking about using analytic functions.
SELECT deptno, deptdesc, eff_ts from test_dates where eff_ts in (
select FIRST_VALUE(eff_ts)
OVER (PARTITION BY trunc(eff_ts) ORDER BY eff_ts) eff_ts FROM test_dates)
I can also use max (or min) timestamp and do a group by.
SELECT deptno, deptdesc, eff_ts from test_dates where eff_ts in (
select min(eff_ts) FROM test_dates group by trunc(eff_ts))
Which is better (or is there any other better way) to use in a data warehousing env?
Similar Messages
-
SQL Query to retrieve the All records based on the Max Dates.
Hello all,
I am trying to retrieve the newest record based on the date field ( nextDate ).
Currently there are only 4 records in the MC_Maintenance table and two in the Machine table.
Machine table
MC_id EquipID
1 0227
MC_id EquipID
2 0228
MC_Maintenance table
Maint_id MC_id Next_maint
1 2 08/25/2010
2 2 07/01/2010
3 1 06/11/2010
4 1 07/11/2010
What I am trying to accomplish is,
list the two machines from the Machine table with the MAX(Next_maint) controlling the MC_Maintenance output list
These are the records that I would like to Display.
Maint_id MC_id Next_maint
1 2 08/25/2010
4 1 07/11/2010
Below is the SQL Query
SELECT
MC.MC_ID as ID,
MC.complete_Date as completed,
MC.next_maint as nextDate,
MC.maint_notes as Notes,
MC.facility as Facility,
M.EquipId,
M.name as name,
M.SerialNumber as SN,
M.dept as dept,
M.Freq as freq
From MC_Maintenance MC, Machine M
where MC.MC_ID = M.MC_ID
' USING MAX(nextDate )
Any ideas would help.
TJI would have thought that was a simple group by problem?
SELECT M.EquipID, MC.MC_ID, Max(MC.next_maint)
FROM MC_Maintenance MC INNER JOIN Machine M ON MC.MC_ID = M.MC_ID
GROUP BY M.EquipID, MC.MC_ID -
How to create a record based on the name of a file in the file-system?
Hi,
With a lot of pictures I want to have a database to gather some information about these pictures.
First question is how to generate a record based on a file in the file system?
e.g. the pictures are "c:\fotos\2009\01\disc_001.jpg" to "c:\foto\2009\01\dis_98.jpg" .
now i want to create records with as one of the attributes the name of the picture (not the picture itself). how to create these records (based on the information of the file-ssytem). i.e. the number of records should be the same as the number of pictures.
any suggestions?
any reaction will be appreciated.
LeoLink to Create directory
http://www.adp-gmbh.ch/ora/sql/create_directory.html
You can create a list of files in the directory and read the list files from that directory.
[UTL_FILE Documentation |http://download.oracle.com/docs/cd/B14117_01/appdev.101/b10802/u_file.htm#996728]
[Solution using Java|http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:439619916584]
SS -
Retrive Records based on the content in a Clob Feild
Retrive Records based on the content in a Clob Feild
I have a Table NoteFile which contains a Clob Feild NoteFileContent
and is refrenced by PK DocID
i need to Match DocID with other Tables Document and folderdocument
My Query is as Below
SELECT Doc.Pap_Doc_ID, Doc.Pap_Doc_Name , NF.NoteFileContent
From NoteFile NF , Document Doc, folderdocument FD
Where Doc.Pap_Doc_ID = FD.FoldDoc_ID and FD.FoldDoc_ParentID <> 3 and
NF.DocID = DOC.Pap_Doc_ID And NF.NoteFileContent Like '%a%'
The above query throws
ORA-00600: internal error code, arguments: [504], [0x7ADEBE00], [2], [1],
[cache buffers chains], [723], [0], [0x7ADEBE00]Hi,
We can resize it.Always give the height and weight in
percentage format. and turn off the horizontal n vertical scroll
policy in u r main application.
Hopes it helps you... -
Select records based on the closest given time
Dear SQL gurus,
I have a table T1:
Name Null? Type
ID NOT NULL NUMBER(5)
MOMENT NOT NULL DATE [DD.MM.YYYY HH24:MI]
MEASUREMENT NOT NULL NUMBER(8,3)
Example (ID, MOMENT, MEASUREMENT)
-- START OF EXAMPLE --
9380 18.11.2000 03:45 17.6
9380 18.11.2000 04:30 17.3
9380 18.11.2000 05:45 16.8
9380 18.11.2000 06:15 16.8
9380 18.11.2000 07:00 16.2
9380 18.11.2000 07:30 16.2
9380 18.11.2000 08:15 16
9380 18.11.2000 08:45 15.7
9380 18.11.2000 09:30 15.4
9380 18.11.2000 10:00 15.4
9380 18.11.2000 11:15 15.4
9380 18.11.2000 11:45 15.4
9380 18.11.2000 12:30 15.4
9380 18.11.2000 13:00 15.4
9380 18.11.2000 13:45 15.4
--- END OF EXAMPLE --
How to select records based on the:
- time period specified by the day only [DD.MM.YYYY] - CONDITION 1
- with values for 6AM only, and if not available, with values closest to 6AM - CONDITION 2
(if the time gap in MOMENT field is too big, lets say > 5h then choose the average between the value before 6AM (ex. 4:15AM) and the value after the 6AM (ex. 9:45AM))
CONDITION 1 (something like): moment between '01.01.2005' and '31.12.2004' - this is OK
CONDITION 2: I do not know how to formulate, especially if 6AM value is not availabe, and I have to find the closest available value, or get the avergae from the two adjacent values.
Maybe cursor magic??? Thanks a lot for your help.
RadoAbout condition two, would the following select be of use to you? Picking the first record could be achived by rownum, analytic function, etc.
WITH t1 AS (SELECT 9380 id, TO_DATE('18.11.2000 03:45', 'dd.mm.yyyy hh24:mi') moment, 17.6 measurement
FROM dual
UNION
SELECT 9380 id, TO_DATE('18.11.2000 04:30', 'dd.mm.yyyy hh24:mi') moment, 17.3 measurement
FROM dual
UNION
SELECT 9380 id, TO_DATE('18.11.2000 05:45', 'dd.mm.yyyy hh24:mi') moment, 16.8 measurement
FROM dual
UNION
SELECT 9380 id, TO_DATE('18.11.2000 06:15', 'dd.mm.yyyy hh24:mi') moment, 16.8 measurement
FROM dual
SELECT id, moment, measurement, diff
FROM (SELECT id, moment, measurement,
moment - TO_DATE(TO_CHAR(moment, 'dd.mm.yyyy ') || '06:00', 'dd.mm.yyyy hh24:mi') diff
FROM t1
ORDER BY abs(diff) asc, SIGN(diff) desc;
C. -
Web GUI new feature - creating a new record based on the current record
Vincent,
We need new feature in Web GUI:
creating a new record based on the current record with data
from Moscow with moneyIt is already enabled, in the next release.
If you joint TAB program, the march preview build contains it. User can select 1 or multiple users and make a copy. -
Query range must return one record based on the amount passed.
Hi,
Need to return the correct line_number based on the amout that falls within that range.
Example;- if current amount value is 100 then line_number should return 1.
if current amount value is 420 then line_number should return 4.
Line_Num Amount
*1 149.99*
2 300
3 400
*4 500*
5 9999999.99
I need this in a query to resolve this, not PL/SQL.
Thanks in advance.select min(line_num) from yourtable where amount >= &input_amount
-
Deleting records based on date with timestamp
Hi there,
In continuation from my earlier Query. Multiple reccord have inadvertantly been inserted in the LOAN_TXN table. I want to delete all the records, except one, based on date with timestamp.
CREATE TABLE LOAN_TXN
TXN_DATE DATE,
BALANCE NUMBER(10,2),
CODE VARCHAR2(1),
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '02/15/2010 11:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'), 250000, 'D');
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '03/31/2010 11:59:59 AM', 'MM/DD/YYYY HH:MI:SS AM'), 250000, 'B');
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '05/14/2010 11:25:00 AM', 'MM/DD/YYYY HH:MI:SS AM'), 500000, 'D');
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '06/30/2010 12:15:00 PM', 'MM/DD/YYYY HH:MI:SS AM'), 4000, 'R');
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '08/02/2010 10:45:26 AM', 'MM/DD/YYYY HH:MI:SS AM'), 4000, 'R');
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '09/08/2010 02:10:17 PM', 'MM/DD/YYYY HH:MI:SS AM'), 4000, 'R');
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '10/27/2010 04:25:20 PM', 'MM/DD/YYYY HH:MI:SS AM'), 4000, 'R');
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '11/09/2010 10:15:55 AM', 'MM/DD/YYYY HH:MI:SS AM'), 4000, 'R');
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '12/29/2010 03:10:20 PM', 'MM/DD/YYYY HH:MI:SS AM'), 4000, 'R');
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '01/12/2011 01:11:15 PM', 'MM/DD/YYYY HH:MI:SS AM'), 4000, 'R');
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '02/11/2011 12:11:48 PM', 'MM/DD/YYYY HH:MI:SS AM'), 4000, 'R');
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '03/31/2011 11:59:59 PM', 'MM/DD/YYYY HH:MI:SS AM'), 4000, 'R');
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '03/31/2011 11:59:59 PM', 'MM/DD/YYYY HH:MI:SS AM'), 4000, 'R');
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '03/31/2011 11:59:59 PM', 'MM/DD/YYYY HH:MI:SS AM'), 4000, 'R');
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '03/31/2011 11:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'), 4000, 'R');
INSERT INTO LOAN_TXN ( TXN_DATE, BALANCE, CODE) VALUES (TO_Date( '03/31/2011 11:59:59 AM', 'MM/DD/YYYY HH:MI:SS AM'), 460000, 'B');I want to delete mutiple records, except one, with code 'R' and TXN_DATE = '03/31/2011 11:59:59 PM' How do I do that ?user3308033 wrote:
I think your query would delete all the duplicate records. Keeping one, I just want to delete mutiple records with timestamp : 11:59:59.
DELETE FROM loan_txn
WHERE rowid != (
SELECT MIN(rowid)
FROM loan_txn
WHERE txn_date = TO_DATE('03/31/2011 11:59:59 PM','MM/DD/YYYY HH:MI:SS AM')
AND code = 'R'
AND txn_date = TO_DATE('03/31/2011 11:59:59 PM','MM/DD/YYYY HH:MI:SS AM')
AND code = 'R'
/SY. -
Creation of a new record based on the value of a picklist
Hi,
My requirement is whenever i select a picklist value in Opportunity record type based on the value selected a new opprtunity record should get created and get assigned to a role or A User otherwise a lead should get created and should be assigned to a Role or A User.
Can I achieve this in OnDemand?
Kindly help. This is urgent..
Thanks in Advance.Can I achieve this in OnDemand?I think you can better try one of the Oracle CRM On Demand forums @ http://forums.oracle.com/forums/category.jspa?categoryID=162
instead of the Oracle SQL and PL/SQL forum.
This is urgent..The forums are relying on volunteers and it is weekend for most of us now.
If it is really that urgent, call Oracle Support. -
Hi Experts,
Iam doing a Idoc to file scenario.
Based on the sales organization(VKORG),i need to create the multiple records in the file.
For example :
E1MARAM 0..Unbounded
|
|E1MARMM 0..Unbounded
|
E1MARMM
|
E1MARMM
|
E1MARMM
|
E1MVKEM 0..Unbounded
|
E1MVKEM
When E1MVKEM-VKORG = Z200 then i need to create a file with 4 records (for 4 E1MARMM segments 4 records will be created)
When one more E1MVKEM-VKORG = Z201 present then we need to create 4 more records for Z201 in the same file.
So now i will be getting a single file with 8 records.
Each E1MVKEM segment will be having only one VKORG value.
I tried with <b>oneasmany</b> function,but its creating only for VKORG=Z200 with 4 values but its not providing the values for VKORG=Z201.
Please suggest me what to do.. I need to write UDF's or can i achieve without using them.??
PS: we are not using BPM in our scenario.
Thank You.
Regards
Seema.Hi Prabhu,
I need to display the records in the file for each sales organisation (MVKEM-VKORG) .
MARAM is the parent node and MARMM,MVKEM are child nodes.
MARAM
MARMM
MARMM
MARMM
MVKEM
MVKEM
Consider that first MVKEM has VKORG = Z200 then 3 records has to display in file (for 3 MARMM segments 3 records has to display)
Simillarly consider second MVKEM has VKORG = Z201 then 3 more records has to display in file. ( same as above for 3 MARMM segments = 3 records in file)
So for the above structure i will be getting a total of 6 Records in my output file.
Iam trying with different Node functions but it is taking only for first VKORG and not taking the second one.so iam getting only 3 records in my file.
Please help me in cracking this.
Regards
Seema. -
Selecting records based on a max value of a field?
I want to do a select on a table and only get those records that
have the maximum cum order lead time within a supplier_id group.
I want to do this within SQL, not a procedure.
create table bamb.ta_test (
FO_ID NUMBER(10),
CC_ID NUMBER(10),
SUPPLIER_ID NUMBER(10),
CUM_ORDER_LEAD_TIME NUMBER(10),
ORDER_LEAD_TIME NUMBER(10));
insert into bamb.ta_test values(886,550,6,256,13);
insert into bamb.ta_test values(886,550,6,410,10);
insert into bamb.ta_test values(886,550,12,190,10);
insert into bamb.ta_test values(886,550,12,168,48);
insert into bamb.ta_test values(886,550,27,91,22);
insert into bamb.ta_test values(886,550,27,179,17);
FO_ID CC_ID SUPPLIER_ID CUM_ORDER_LEAD_TIME ORDER_LEAD_TIME
886 550 6 256 13
886 550 6 410 10
886 550 12 190 10
886 550 12 168 48
886 550 27 91 22
886 550 27 179 17
6 rows selected.
what I want to end up with is:
FO_ID CC_ID SUPPLIER_ID CUM_ORDER_LEAD_TIME ORDER_LEAD_TIME
886 550 6 410 10
886 550 12 190 10
886 550 27 179 17
How would I do this? [email protected]Hi Timothy!
Try a nested query as:
Greetings Peter
select ta_test.FO_ID, ta_test.CC_ID,
ta_test.SUPPLIER_ID, ta_test.CUM_ORDER_LEAD_TIME,
ta_test.ORDER_LEAD_TIME
from
( select SUPPLIER_ID, max(CUM_ORDER_LEAD_TIME) as max_colt
from ta_test
group by SUPPLIER_ID
) subquery_01,
ta_test
where subquery_01.SUPPLIER_ID = ta_test.SUPPLIER_ID
and
subquery_01.max_colt = ta_test.CUM_ORDER_LEAD_TIME
SQLWKS> select ta_test.FO_ID, ta_test.CC_ID,
2> ta_test.SUPPLIER_ID, ta_test.CUM_ORDER_LEAD_TIME,
3> ta_test.ORDER_LEAD_TIME
4>
5> from
6> ( select SUPPLIER_ID, max(CUM_ORDER_LEAD_TIME) as
max_colt
7> from ta_test
8> group by SUPPLIER_ID
9>
10> ) subquery_01,
11> ta_test
12>
13> where subquery_01.SUPPLIER_ID = ta_test.SUPPLIER_ID
14> and
15> subquery_01.max_colt = ta_test.CUM_ORDER_LEAD_TIME
16>
FO_ID CC_ID SUPPLIER_I CUM_ORDER_ ORDER_LEAD
886 550 6 410 10
886 550 12 190 10
886 550 27 179 17 -
Returning Records Only for the Current Month
Hello,
I have a query that returns records for the current week and I would like to extend it to return records for the current month.
where TRUNC (START_DATE) < NEXT_DAY (TRUNC (CURRENT_TIMESTAMP) - 1, 'Sat') + 1
AND TRUNC (END_DATE) > NEXT_DAY (TRUNC (CURRENT_TIMESTAMP) - 1, 'Sat') - 7
I have two date fields, the first is called 'Start_Date', the second is called 'End_Date'. I would like the query to return all records for the current month including those that may begin prior to or finish post the month as events can begin in one month and end in another. Does anyone know how this is done in a 'Where Clause'?
Kind Regards,
Steve WelchActually LAST_DAY(ADD_MONTHS(TRUNC(sysdate),-1))+1 gives you the starting day of the current month, and LAST_DAY(TRUNC(sysdate)) gives you the last day of the current month.
So the where be
WHERE TRUNC(START_DATE) >= LAST_DAY(ADD_MONTHS(TRUNC(sysdate),-1))+1 OR TRUNC(END_DATE) <= LAST_DAY(TRUNC(sysdate))
Start Date >= 1st Day of Month OR End Date <= Last Day of Month, meaning either date is within the current month. -
Get a value based on the max value
Hi all,
would u be so kind and show me the way to get something (without having select from the main select) from the data listed below? For simplicity i've typed the result i'd like to get
data i have:
day starttime id test_value
'25.5.2007','10:00', 'A', 3
'25.5.2007','10:15', 'A', 2
'25.5.2007','10:35', 'A', 4
'25.5.2007','10:45', 'A', 1
simply get the line where is the max( test_value ) for a whole day:
ID test_value STARTTIME
'A', 4, '10:35'
thanksSQL> create table mytable
2 as
3 select date '2007-05-25' day, '10:00' starttime, 'A' id, 3 test_value from dual union all
4 select date '2007-05-25', '10:15', 'A', 2 from dual union all
5 select date '2007-05-25', '10:35', 'A', 4 from dual union all
6 select date '2007-05-25', '10:45', 'A', 1 from dual
7 /
Tabel is aangemaakt.
SQL> var P_DATE varchar2(10)
SQL> exec :P_DATE := '2007-05-25'
PL/SQL-procedure is geslaagd.
SQL> select day
2 , max(starttime) keep (dense_rank last order by test_value) starttime
3 , max(id) keep (dense_rank last order by test_value) id
4 , max(test_value) test_value
5 from mytable
6 where day = to_date(:P_DATE,'yyyy-mm-dd')
7 group by day
8 /
DAY START I TEST_VALUE
25-05-2007 00:00:00 10:35 A 4
1 rij is geselecteerd.And you should really think about combining the columns day and starttime into one column. A date contains a time component as well.
Regards,
Rob.
Message was edited by:
Rob van Wijk
Misread the question, so posted a new solution. -
Displaying record based on the condition in Crystal report
Hi All,
I want to display certain records in crystal based on some condition.
For example,
If I am having 5 student records like,
Name Mark1 Mark2 Mark3 Total
aaa 40 50 60 150
abc 30 60 40 130
tcr 25 25 25 75
Now in table i have only 4 fields (STUNAME, MARK1, MARK2, MARK3).
I have to calculate the total at runtime and if total is greater than 150, then actual record should display in report.
For example, I can use formula to calculate total but I can calculate total only after each record prints. But i want to calculate total internally only it should display in report if condition satisfies. I dont want to keep one more field for total in crystal report.
Please help me on this.If the filter is not "pushed" down to the database, then Crystal will pull all of the data into memory and then filter it there. This is not a big deal if you only have a couple of records, but it can significantly slow down a report that is pulling in more than a few records.
An alternative, if your report is just linking tables in the Database Expert, would be to create a SQL Expression that adds the three fields together. Using this in the Select Expert WILL push the condition down to the database.
If you're using a command instead of linking table, they you would add this to the Where clause of the comment. If you're using a stored proc instead of tables or a command, you would want to do this filter in the code for the stored proc.
-Dell
Message was edited by: Dell Stinnett-Christy -
Format the returned record out from the result
Hi All,
Please help me again.
i have this query...
Query:
select* from patient where id = '9'
Result:
id fluX fluY fluZ Created_on
1 Y - - 06/15/2007 2:06:05 AM
1 - - - 06/15/2007 2:06:05 AM
1 - - Y 06/15/2007 2:06:05 AM
I want only to return only one row out from the result above.
ID fluX fluY fluZ
1 Y - Y
Thanks in advance for help.
Edited by: jresh on Jun 21, 2009 10:11 PMHi jresh,
Try following
select id, max(fluX), max(fluY), max(fluZ), Created_on
from patient where id = '9'
group by id, Created_on;
Please mark, if it help you
Regards,
Danish
Maybe you are looking for
-
How can I organize my windows file structure to expedite workflow in LR?
Hello! I'm new at Lightroom2 and am getting ready to import all my current photographs on my hard disk to LR. I began doing this but am finding it very tedious. My current folder organization looks somewhat like this: each indent = a subfolder inside
-
Error during Cancellation Of Invoice in MR8M
Dear All, While canceling a MIRO document in MR8M one error is coming as "Inconsistent temporary document number assignment" Message no. F5726. This error is coming now in a production server which has been running from last 6 mon
-
[SOLVED] Mailbox import requests stuck in 'queued' state
We have a small Exchange 2013 environment with one server having all roles. I try to import archive mailboxes from an Exchange 2010 environment. It has worked quite well for a while. However, all import requests are now 'stuck' in 'queued' state. Res
-
WRT400N connection issues with iPod Touch and MacBook
I'm a newbie, so please bear with me. I recently upgraded to the WRT400N from a Linksys BEFW11S4. Since the change, the connection to iPod Touches and MacBooks at the house have been shaky at best. The connection frequently drops out completely on
-
Quick Time Pro Activation Trouble
I ran GT Pro 6 for a long time and decided to upgrade. I downloaded and installed GT 7.6 and bought the new Key. I installed and doublechecked the numbers but I get an Invalid Registration message. I am using Pro to take advantage of the ability to d