Solved

Oracle - Query issue

Posted on 2014-02-20
4
304 Views
Last Modified: 2014-02-21
Hi

I have this below query that pulls data from 1 single table:
SELECT   LOAD_ID, STOP_NUM, LOCATION_ID, MILES, MILES_INDICATOR    
    FROM   LOAD_STOP
   WHERE   OPEN_TIME  > TO_DATE (TO_CHAR (SYSDATE, 'YYYYMMDD'), 'YYYYMMDD') - 7
ORDER BY   LOAD_ID, STOP_NUM;

Open in new window


What i'm trying to do is to find every MILES_INDICATOR that equals '4' and then, it will take the LOCATION_ID of the same row as the FROM value, then take the LOCATION_ID of the following row as the TO value.

The data must be sorted as : ORDER BY   LOAD_ID, STOP_NUM.

How can i do that?

In the attachment, you will see 2 sheets:

1- Table extract = an example of the data in the table: LOAD_STOP
2- Final result = what i would like to see as the final result from the query

Thanks again for your help.
Table-result.xls
0
Comment
Question by:Wilder1626
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
Surrano earned 500 total points
ID: 39876157
Something like this?

Select a.location_id "from", b.location_id "to", a.miles_indicator 
from load_stop a, load_stop b
where a.OPEN_TIME  > TO_DATE (TO_CHAR (SYSDATE, 'YYYYMMDD'), 'YYYYMMDD') - 7
  and a.miles_indicator=4
  and a.stop_num+1 = b.stop_num
  and a.load_id = b.load_id
order by load_id, stop_num;

Open in new window

0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 39876519
This is perfect

Thanks for your help
0
 
LVL 34

Expert Comment

by:johnsone
ID: 39876544
Well, I guess I was a little late.

This should do it without having to join the table to itself.  Should perform much better and should handle cases where there are gaps in the STOP_NUM field.

SELECT * 
FROM   (SELECT load_id, 
               stop_num, 
               location_id            "FROM", 
               miles, 
               miles_indicator, 
               Lead(location_id) 
                 over ( 
                   PARTITION BY load_id 
                   ORDER BY stop_num) "TO" 
        FROM   load_stop 
        WHERE  open_time > Trunc(SYSDATE) - 7) 
WHERE  miles_indicator = 4 
ORDER  BY load_id, 
          stop_num; 

Open in new window


There is no OPEN_TIME field in the sample data that you supplied, so I assume it is in that table.  Also, removed the unnecessary conversions on SYSDATE.

There are also syntax errors in the accepted answer.  The ORDER BY has ambiguous column names.  They need to be prefixed with the correct table alias.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 39876777
Hi johnsone

Thanks for still giving me some more ideas. I will test this and come back to you.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now