Advertisement

03.01.2008 at 12:26PM PST, ID: 23206573
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

SQL without inner join/subselect

Tags: Oracle, Oracle Database, 10g, SQL
I'm thinking of a solution for following query without explicit inner join or subquery:

Table FXR:
FX_Code|FX_Rate|FX_Date
'USD',1.15,'2008-01-01'
'USD',1.2,'2008-02-01'
'EUR',1.1,'2008-01-15'
'EUR',1.5, '2008-01-17'

I want a query that gives me the latest FX_Rate,FX_Date for each FX_Code:
'USD',1.2,'2008-02-01'
'EUR',1.5,'2008-01-17'

this must be simpler than doing this with a subquery:
select FX_Code,FX_Rate,FX_Date from FXR
where (FX_Code,FX_Date) in (
  select FX_Code,max(FX_Date) from FXR
  group by FX_Code
)

thanks for any hint
michi
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: witty
Solution Provided By: sdstuber
Participating Experts: 4
Solution Grade: A
Views: 121
Translate:
Loading Advertisement...
03.01.2008 at 01:18PM PST, ID: 21023586

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.01.2008 at 01:23PM PST, ID: 21023612

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.01.2008 at 01:24PM PST, ID: 21023615

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.01.2008 at 01:30PM PST, ID: 21023656

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.01.2008 at 02:25PM PST, ID: 21023917

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.01.2008 at 02:50PM PST, ID: 21024022

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.01.2008 at 05:44PM PST, ID: 21024574

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.01.2008 at 09:14PM PST, ID: 21025050

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.02.2008 at 06:33AM PST, ID: 21026071

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.02.2008 at 07:44AM PST, ID: 21026346

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
03.01.2008 at 01:18PM PST, ID: 21023586

Rank: Guru

This question ("find the earliest/latest record for each group") does require a subquery, and as far as I know there is no "simpler" method.

However, depending on the database engine, using Max() and a GROUP BY query might not be the most optimal. I often use on of the following instead. I'm assuming that (FX_Code; FX_Rate) is the key of the table FXR.

select FX_Code,FX_Rate,FX_Date from FXR
where not exists (
  select 1 from FXR Z
  where FX_Code = FXR.FX_Code And FX_Date > FXR.FX_Date
)

[Access syntax don't know the Oracle equivalent of DISTINCTROW:]
select distinctrow FX_Code,FX_Rate,FX_Date
from FXR left join FXR Z
on FXR.FX_Code = Z.FX_Code And FXR.FX_Date < Z.FX_Date
where Z.FX_Code Is Null

select FX_Code,FX_Rate,FX_Date from FXR
where FX_Date = (
  select top 1 FX_Date from FXR Z
  where Z.FX_Code = FXR.FX_Code
)

As you see, not "simpler" at all.

Cheers!
(°v°)
 
03.01.2008 at 01:23PM PST, ID: 21023612

Rank: Guru

Sorry, forgot the *don't*. I often see this query, which is perhaps what you were hoping for:

select FX_Code, Last(FX_Rate), Last(FX_Date)
from FXR
group by FX_Code

This even works e.g. in Access on a freshly compacted database, because the table is resorted and stored in the key order. So Last() does indeed mean "latest". However, unless your database engine honors an ORDER BY clause prior to grouping, the results are unpredictable.

select FX_Code, Last(FX_Rate), Last(FX_Date)
from FXR
"order by before grouping" FX_Code, FX_Date
group by FX_Code

Cheers!
(°v°)
 
03.01.2008 at 01:24PM PST, ID: 21023615

Rank: Sage

You definitely don't want to do it with a join or a sub-query to the same table again.
You will have to use analytics though but the subquery is quite efficient and (in my opinion) simple to read


SELECT fx_code, fx_rate, fx_date
  FROM (SELECT fx_code, fx_rate, fx_date,
               ROW_NUMBER() OVER(PARTITION BY fx_code ORDER BY fx_date DESC)
                                                                           rn
          FROM fxr)
 WHERE rn = 1
Accepted Solution
 
03.01.2008 at 01:30PM PST, ID: 21023656
Is there a reason why you don't want to use a sub query or inner join? SQL Server 2005's TSQL syntax has RANK() function that can do this, but I can't do it in just SQL.

I'll post the SQL Server function in case Oracle has something similar.
1:
2:
3:
4:
5:
6:
7:
SELECT FX_Code, FX_Date, FX_Rate
FROM 
(
	SELECT FX_Code, FX_Date, FX_Rate, RANK() OVER (PARTITION BY FX_Code ORDER BY FX_Date DESC) AS Rank
	FROM @FXR AS fxr
) AS tmp
WHERE Rank = 1
Open in New Window
 
03.01.2008 at 02:25PM PST, ID: 21023917
The question is interesting, since intuitively you *should* be right! :-)

However, if you think in set algebra, you need to do this:

- First, go through all rows grouped by code, and determine which are the ones that have the max(date) for each equivalence class
- Then, revisit those rows to get some other fields' data (you can make it faster by using rowid access, but you'll still make a second pass)

Now you'll rightfully say... why can't you get my rates when you make the groups? I suppose it's just a SQL limitation: you can only project either grouped things, or aggregates on non-grouped things :-/

I've included the ROWID trick to ease the pain. Just for the record: you could try using analytics' MAX(FX_DATE) over(partition by FX_CODE), but that will also make another pass -look at the execution plan and you'll see...
1:
2:
3:
4:
5:
select FX_Code,FX_Rate,FX_Date from FXR
where (rowid,FX_Code,FX_Date) in (
  select rowid,FX_Code,max(FX_Date) from FXR
  group by FX_Code
)
Open in New Window
 
03.01.2008 at 02:50PM PST, ID: 21024022

Rank: Sage

I would definitely avoid the requery to FXR, even with rowid's.    Imagine if FXR has a lot of data.  Even rowid access isn't fast enough.
Just use analytics,  this kind of query is exactly what they were built for
 
03.01.2008 at 05:44PM PST, ID: 21024574
@Akenathon:
but wouldn't I have to include rowid also in the groupby?
Otherwise I would get the a record-specific result with a aggregated value -> exactly what I need, but what I would not expect here to work ...

@sdstuber:
sounds logical to me - but internally oracle has to do "something like self join" (perhaps more performant) - right?

thx michi
 
03.01.2008 at 09:14PM PST, ID: 21025050

Rank: Sage

No, analytics don't use self joins.  At least not in this case.  It uses a sort.

In this case it's sorting by fx_code, fx_date.  

Do an explain plan on the query and you'll see a sort step included.
That's how it finds which row comes first within each fx_code

Then it's just a matter keeping only the first row of each fx_code.
Assisted Solution
 
03.02.2008 at 06:33AM PST, ID: 21026071
Yes, you are right... you can't do the rowid thing without another access/join :-/

Now, the analytics won't do any self join. Analytics are applied after the result set is determined, partitioning+sorting+calculating the funcion in memory. So, it will make another pass, but not one which needs another access to the blocks. You'll see it as "WINDOW SORT" in your execution plan.

I think I'd go with sdstuber's solution for this one... it makes one pass accessing the blocks, then one pass in memory for grouping (i.e. "partitioning") and sorting. The only weak scenario is when each partition (i.e. group) is very large: I don't think the CBO is clever enough to propagate the "outer" predicate rn=1 to the subquery, so other analytics like MAX and FIRST_VALUE could also be explored... no time now, but I'll check this thread when I'm back :-)
 
03.02.2008 at 07:44AM PST, ID: 21026346

Rank: Sage

actually you "can" get "stopkey" steps in your explain plan.
Where the optimizer decides to push the predicate, but when it will do that and when it won't aren't something I know how to determine.
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628