472,328 Members | 1,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

Return first occurance of record

I would like to return the first occurance of a record. The first occurance is date driven. Searched on message boards and internet and saw a few different ways to tackle this. Is using the ROWNUM = 1 the way to do it? If not, what is the recommendation solution?

Expand|Select|Wrap|Line Numbers
  1. SELECT 'BILLING' as "Repository", o.ORDER_ID as "Order ID", O.ORDER_DATE as "Date Ordered", s.SHIP_DATE as "Date Shipped", (s.SHIP_DATE-O.ORDER_DATE) as "Lag Time"
  2. FROM BILLING_DB.ORDERS o, BILLING_DB.SHIPPING s 
  3. WHERE o.ORDER_ID = s.SHIP_ORDER_ID
  4. AND o.ORDER_ID = 4043
Returns this data set:

Repository Order ID Date Ordered Date Shipped Lag Time
BILLING 4043 12/01/2008 00:00:00 12/01/2008 00:00:00 0
BILLING 4043 12/01/2008 00:00:00 12/02/2008 00:00:00 1

But I only want it to return the first occurrence (the first time the order was placed/shipped).

Repository Order ID Date Ordered Date Shipped Lag Time
BILLING 4043 12/01/2008 00:00:00 12/01/2008 00:00:00 0

I tried using min(s.SHIP_DATE) but that still returned the same result set.

Any help is very appreciated.
Thanks.
Dec 2 '08 #1
7 15567
amitpatel66
2,367 Expert 2GB
You can use your lag_time column here:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT repository,order_id,date_ordered,date_shipped,lag_time FROM
  3. (SELECT Repository,order_id,date_ordered,date_shipped,lag_time,row_number() OVER(PARTITION BY order_id ORDER BY lag_time) rn FROM
  4. (SELECT 'BILLING' as Repository, o.ORDER_ID as Order_ID, O.ORDER_DATE as Date_Ordered, s.SHIP_DATE as Date_Shipped, (s.SHIP_DATE-O.ORDER_DATE) as Lag_Time 
  5. FROM BILLING_DB.ORDERS o, BILLING_DB.SHIPPING s  
  6. WHERE o.ORDER_ID = s.SHIP_ORDER_ID 
  7. AND o.ORDER_ID = 4043)) WHERE rn = 1
  8.  
  9.  
Dec 3 '08 #2
Pilgrim333
127 100+
Hi,

The easiest way to do this:

Expand|Select|Wrap|Line Numbers
  1. SELECT 'BILLING' as "Repository"
  2.      , o.ORDER_ID as "Order ID"
  3.      , O.ORDER_DATE as "Date Ordered"
  4.      , s.SHIP_DATE as "Date Shipped"
  5.      , (s.SHIP_DATE-O.ORDER_DATE) as "Lag Time" 
  6. FROM BILLING_DB.ORDERS o
  7.    , BILLING_DB.SHIPPING s  
  8. WHERE o.ORDER_ID = s.SHIP_ORDER_ID 
  9. AND   o.ORDER_ID = 4043 
  10. AND   rownum = 1
  11. order by s.SHIP_DATE
  12.  
If it is not necessary to use select statements in the from clause then don't do it. It makes things too complicated and not harder readible for other programmers. As this has a simple and readible solution, my personal preference goes to that one.

Pilgrim.
Dec 3 '08 #3
amitpatel66
2,367 Expert 2GB
it has to be for multiple order_id and not just one. Remove the condition of order_id and the query with rownum will not work

@OP,

There are many other solutions for your issue and the one that I provided is one way of doing it. There is nothing so COMPLEX in that query and it uses a concept of INLIVE VIEW that is supported by ORACLE which is really very helpful in achieving what we require.

You can remove the ORDER_ID = 4043 condition from the query that I provided to make it work for all the ORDER_ID's
Dec 3 '08 #4
amitpatel66
2,367 Expert 2GB
@OP, Please test the query and let us know the results for multiple order_id
Dec 3 '08 #5
Pilgrim333
127 100+
I did not get it that it should be for multiple id's. But if that is the issue, then my personal preference would still go to the following code

Expand|Select|Wrap|Line Numbers
  1. SELECT 'BILLING' as "Repository" 
  2.      , o.ORDER_ID as "Order ID" 
  3.      , O.ORDER_DATE as "Date Ordered" 
  4.      , s.SHIP_DATE as "Date Shipped" 
  5.      , (s.SHIP_DATE-O.ORDER_DATE) as "Lag Time"  
  6. FROM BILLING_DB.ORDERS o 
  7.    , BILLING_DB.SHIPPING s   
  8. WHERE o.ORDER_ID = s.SHIP_ORDER_ID  
  9. AND   s.SHIP_DATE =
  10.       (select min(t.shipdate)
  11.        from  BILLING_DB.SHIPPING t
  12.        where t.ship_order_id = o.order_id
  13.       )
  14.  
It is much more readible and explainotry, another programmer (or even you after a while) will see in a glimpse what the query will return.

Pilgrim.
Dec 3 '08 #6
Both queries returned the same result set and I learned something new.

I will have to read up on the Oracle OVER (PARITION ..) keywords and the row_number() method and how it's used/processed in oracle-sql.

Expand|Select|Wrap|Line Numbers
  1. row_number() OVER(PARTITION BY TEST_ID ORDER BY lag_time) rn
  2.  
Thanks again!!!
Dec 3 '08 #7
Pilgrim333
127 100+
Good to hear your problem is solved. The use of either queries is depending on your personal preference.

Good luck with the rest of your application.

Pilgrim.
Dec 3 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Stew | last post by:
Hi, I have a table to log sessions and user (connect_id) id's, start time etc - see below ...
2
by: jason | last post by:
Hello. I've got this js function I call from an html input button. It finds the occurance of a string in a textbox, selects it and scrolls into...
5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if:...
20
by: MS | last post by:
Access 97 I want to requery the data being displayed on a form, then I want to return to the record I was in. Why doesn't this code work? ...
4
by: Michael McCarthy | last post by:
I want to test for the ~possible~ occurance of a string within another string... IndexOfAny gives wildly odd results, possibly because it's...
9
by: kd | last post by:
Hi All, I have a .txt file, in which I have to look for the last occurance of a keyword, and process contents of the file from that point...
4
by: Chris | last post by:
Hi, I have imported a spreadsheet into an access databsae and have noticed that there are multiple occurances of exactly the same record. I...
4
by: mercuryshipzz | last post by:
Hi, My objective is to get the line number of the first occurance of the search pattern. my test.txt contains: ..... .....................
2
by: Maxington | last post by:
The problem I am left with is that I need to split a string into substrings and determine the character location of the 30th occurance of "\n"...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.