Solved

Oracle - filter sequal issue

Posted on 2012-12-20
4
419 Views
Last Modified: 2012-12-20
Hello all

I have a problem with a SQL.

I have this SQL bellow that i use to pull resources from a table.

The thing is that in TL_RESOURCE_TRLR A i may have multiple A.TRLR_TYPE_ID based on A.EFFECTIVE

So i only want to extract the most updated effective date per TRLR_TYPE_ID and not all the effective dates even the old one. Just the very last one

How can i update this?

Thanks again for your help


  SELECT   DISTINCT A.CARRIER_ID,
                    A.EFFECTIVE,
                    A.TRLR_TYPE_ID,
                    A.CURRENT_NUM_VEHICLES
    FROM   TL_RESOURCE_TRLR A, CARRIER_EQUIP B
   WHERE       A.CARRIER_ID = B.CARRIER_ID(+)
           AND A.TRLR_TYPE_ID = B.TRLR_TYPE_ID(+)
           AND B.CARRIER_ID IS NULL -- EXCLUDE ROWS ADDED BECAUSE  OF THE OUTER JOIN
ORDER BY   A.CARRIER_ID ASC;

Open in new window

0
Comment
Question by:Wilder1626
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 38710343
Try this:

select carrier_id, effective, trlr_type_id, current_num_vehicles
from
(
SELECT   A.CARRIER_ID,
                    A.EFFECTIVE,
                    A.TRLR_TYPE_ID,
                    A.CURRENT_NUM_VEHICLES,
row_number() over(partition by carrier_id order by carrier_id, effective desc) myrn
    FROM   TL_RESOURCE_TRLR A, CARRIER_EQUIP B
   WHERE       A.CARRIER_ID = B.CARRIER_ID(+)
           AND A.TRLR_TYPE_ID = B.TRLR_TYPE_ID(+)
           AND B.CARRIER_ID IS NULL -- EXCLUDE ROWS ADDED BECAUSE  OF THE OUTER JOIN
)
where myrn=1;


or a simple max?

SELECT   DISTINCT A.CARRIER_ID,
                    MAX(A.EFFECTIVE),
                    A.TRLR_TYPE_ID,
                    A.CURRENT_NUM_VEHICLES
    FROM   TL_RESOURCE_TRLR A, CARRIER_EQUIP B
   WHERE       A.CARRIER_ID = B.CARRIER_ID(+)
           AND A.TRLR_TYPE_ID = B.TRLR_TYPE_ID(+)
           AND B.CARRIER_ID IS NULL -- EXCLUDE ROWS ADDED BECAUSE  OF THE OUTER JOIN
group by carrier_id, trlr_type_id, current_num_vehicles
ORDER BY   A.CARRIER_ID ASC;
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38710428
Hi slightwv

Let me look at this, i'm not having the same result in both SQL
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38710457
I might have fat-fingered when I retyped your SQL.

If you can provide some sample data and expected results, I can create an actual working example.
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 38711087
Hi again,

do you know what, i like the second one.

smaller but exactly what i need.

Thanks again for your help
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

726 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