Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys i have one view table called VW_VW_Academics

that view consists of

Position   MaxPoints
 1          48
 2          24
 3          16
 4          12


In My Maintable i have fldAvgMarks,fldHouseID table name is tblChampionsThrophy

My Query is

SQL
SELECT A.fldAvgMarks, A.fldHouseID,B.MaxPoints
    FROM (SELECT ROW_NUMBER() OVER (ORDER BY fldAvgMarks DESC) AS Position, *
    FROM RMS.tblChampionsThrophy
    ) AS A inner JOIN (
            SELECT Position, MaxPoints
            FROM VW_Academics) AS B ON A.Position=B.Position


I'M getting output
fldAvgMarks fldHouseID  MaxPoints
40	    HN04	48
40	    HN05	24-----> here it should come 48 only b'coz fldAvgMarks both HN04,5 r 40
20	    HN03	16
10	    HN02	12


Here Output is Partially correct.. How to do that?

Can any Pls Suggest me.
Posted
Updated 2-Apr-13 19:08pm
v4

Hi,

try by making use of MAX function for MaxPoints like below.
SQL
SELECT A.fldAvgMarks, A.fldHouseID,MAX(B.MaxPoints) 'MaxPoints'
    FROM (SELECT ROW_NUMBER() OVER (ORDER BY fldAvgMarks DESC) AS Position, *
    FROM RMS.tblChampionsThrophy
    ) AS A inner JOIN (
            SELECT Position, MaxPoints
            FROM VW_Academics) AS B ON A.Position=B.Position
GROUP BY A.fldAvgMarks, A.fldHouseID


hope it works.
 
Share this answer
 
Comments
Maciej Los 3-Apr-13 1:59am    
See my answer (after update). I use RANK() instead of ROW_NUMBER function ;)
Karthik Harve 3-Apr-13 2:11am    
Yes, that is the option. Thanks for telling me. now i recalled that function.
Maciej Los 3-Apr-13 2:27am    
Karthik, i'm affraid that above query wont work, because MaxPoint are getting from different table. For the second position it returns 24, but should return 48. Please, test it and correct to avoid down-voting.
Karthik Harve 3-Apr-13 2:35am    
Ya i'll do bro. i'll check once again.
Do not repost!!!

Answer is here: sql query to fetch particular field and assign value[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900