Solved

special join+ms sql server 2005

Posted on 2011-09-06
2
266 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 250 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 250 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

729 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