0

I have the following table :

TNO : can be duplicate
CNo : can be duplicate

I need to select the maximum Tno against the CNo from Table A where a.RNO=B.RNO

Table A

Tno  Rno  name   desc
100  200  adam   aadddddd
100  200  adam   aadddddd
101  201  king   aasdfdsf

Table B

Cno  Rno  
101  200

Can you guys advise the best method please.

I tried the following but not much success

Hi,

Please see the sample data as reqeusted: so the query should

Actual Data :

Rowum   cno     Tno     Rno  
1   24908   24047   22021
1   24909   22424   22022
1   20438   22426   22023

Required Data : it should display the maximun tno against Cno

Rowum   cno     Tno     Rno  
1   24908   24047   22021    
1   20438   22426   22023
3
  • 2
    How do you think it might work? You may find you get a better quality of answer and that people are more willing to help you if you are able to demonstrate that you have tried something for yourself. Commented Jun 10, 2012 at 11:57
  • Also show the result of the example. Commented Jun 10, 2012 at 12:01
  • Since your tables can have duplicate TNo(s), there can be 2 Max value Commented Jun 10, 2012 at 13:10

1 Answer 1

1

One way is using a CTE and ROW_NUMBER function:

WITH CTE AS(
    SELECT RowNum =
       ROW_NUMBER()OVER(PARTITION BY A.Rno ORDER BY Tno DESC)
    , A.Tno,  A.Rno,  name,   [desc]
    FROM TableA A INNER JOIN TableB B ON A.RNO=B.RNO
)
SELECT * FROM CTE
WHERE RowNum = 1
Sign up to request clarification or add additional context in comments.

6 Comments

I'm pretty sure you need the desc column in square brackets ;-)
hi, I tried ti do an oder by cno & Tno but its not supported in CTE ?
hi, i tried ur query but still getting two records for rowid; tno; cno rno 1 2488 22425 20882 1 2488 22411 22005
@user362283: Edit your question and show your sample data and your desired result. Btw, you can always use SELECT TOP 1
hi i haved added the example in question. Thanks
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.