Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Joining tables
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

4 Posts

Posted - 03/07/2013 :  07:20:59  Show Profile  Reply with Quote
I have two tables, which I want to join

Table Deals
* id
* date_date
* ccy_pair
* amount
* rate
* buyOrSell
* order_id

Table Orders
* order_id
* status
* sent_date
* ccy_pair
* buyOrSell
* rate
* amount

For orders that have no deals i.e order with a 'Status' of 'CANCELED', I want to find the next deal (i.e the deal that came after the order sent date) for the same ccy_pair and buyOrSell column values.

select deals.deal_date as d_date, orders.order_id ,orders.buysell, orders.ccy_pair, orders.amount as o_amount, orders.rate as o_rate, 
deals.amount as d_amount, deals.rate as d_rate
from orders 

inner join deals on

and orders.status = 'CANCELED'
--trying to find deal 
and = (SELECT id
        FROM (SELECT id,
               rank() over (partition by ccy_pair,buysell
                                order by deal_date) rnk 
          FROM deals  where  ccy_pair= orders.ccy_pair and deal_date >  orders.sent_date 
          and  buysell = orders.buysell
        WHERE rnk = 1)
order by orders.sent_date desc

This is my beat attempt but I keep getting exceptions. Any help please?

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 03/07/2013 :  08:43:27  Show Profile  Reply with Quote

select o.*,
case when o.status = 'CANCELLED' THEN ELSE END AS DealID,
case when o.status = 'CANCELLED' THEN d1.date_date ELSE d.date_date END AS DealDate,
from Orders o
left join Deals d
ON d.order_id = o.order_id
outer apply (select *
             from Deals
             where ccy_pair = o.ccy_pair
             and  buyOrSell = o.buyOrSell 
             and date_date > o.sent_date
             order by date_date asc)d1

SQL Server MVP

Go to Top of Page

Starting Member

4 Posts

Posted - 03/07/2013 :  09:49:40  Show Profile  Reply with Quote
Thanks for your reply, however I only want canceled orders, i.e d.order_id will never equal o.order_id
Go to Top of Page

Starting Member

4 Posts

Posted - 03/07/2013 :  09:51:52  Show Profile  Reply with Quote
Also can 'apply' be used in oracle databases?
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 03/07/2013 :  09:55:16  Show Profile  Reply with Quote
As far as I know, Oracle does not have CROSS/OUTER APPLY - but then again, I know so little about Oracle.

This forum is for Microsoft SQL Server, so expertise on Oracle would be hard to find.
Go to Top of Page

Starting Member

4 Posts

Posted - 03/07/2013 :  10:05:17  Show Profile  Reply with Quote
Ah ok thanks James, ill ask my question in an oracle forum instead.
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000