Solved

special join+ms sql server 2005

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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