?
Solved

special join+ms sql server 2005

Posted on 2011-09-06
2
Medium Priority
?
269 Views
Last Modified: 2012-08-14
hi all i have two tables as follow:

Table A:
A_ID  A_Name OrdeNo
1        JAD              1
2        Samir           2

Table B:
B_ID  B_Name OrderNo  Amount  Date
1       jean         1                 20      10/10/2010
1       Khalil        1                 30      10/10/2011  

I want to join the two tables to get as out put:
A_ID    A_Name    Date

but the problem is the join between the two tables is basic according to OrderNo of Max(Amount).

Please advice.

Regards,
0
Comment
Question by:MKItani
[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 Comments
 
LVL 42

Accepted Solution

by:
dqmq earned 1000 total points
ID: 36486879
Select A_ID, A_Name, B.Date
from A inner join
(
Select *, row_number() over(partition by OrderNo order by Amount desc) as seq
  from B
) as C on A.OrderNo = C.OrderNo
WHERE C.seq = 1
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 1000 total points
ID: 36487076

like this, use the row_number() windowing function to order the rows by descending amount per
orderno

select a_id,a_name,b.date
from (
select a_id,a_name,b.date
  ,row_number() over (partition by a.orderno order by b.amount desc) as rn
from tablea as a
lefy outer join tableb as b
on a.orderno=b.orderno
) as x
where rn=1
order by 1

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

777 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