473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 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 15829
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 +---------------+-------------+------+-----+---------+-------+ | Field | Type | Null |...
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 view. JS newbie question, considering it's a...
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: foo = new Array(); foo = , 5, , 9, 10]; ...
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? Private Sub CmdRefsh_Click()
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 expecting the string to be there... This is probably...
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 onwards. How can this be achieved? Can anybody guide...
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 would like to know if there is a way of deleting the...
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: ..... .................. total rows.... ................... ..
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" string. I have a string that has "\n" within it and I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.