Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Table branch


Branch_ID---Branch_Name---Branch_City
SB001------Malleshwaram---Bangalore
SB002------MG Road--------Bangalroe
SB003------MG Road--------Mysore
SB004------Jainagar-------Mysore


table loan
Account_no----Branch_Id----Balance
AE1185698----SB001---------102000
AE8552266----SB003---------40000
AE1003996----SB004---------15000
AE1100996----SB002---------100000


i want the output in the below form

min-------city------max------city
15000----mysore----10200----Blore


i tried this

select min(balance), branch.city, max(balance), branch(city) from loan.



Thanks in advance
Posted
Updated 3-Jun-15 1:34am
v3
Comments
raj@hardWork 3-Jun-15 7:26am    
I'm unable to add the tag MySQL
ZurdoDev 3-Jun-15 7:34am    
You'll need 2 derived tables then. One that returns the min and min City and one that returns the max and max city.

1 solution

This would work ...
SQL
SELECT MINCTE.Balance, MINCTE.Branch_City, MAXCTE.Balance, MAXCTE.Branch_City
FROM (select L.*, B.Branch_City from loan L
    INNER JOIN branch B on L.Branch_Id=B.Branch_ID
    order by Balance LIMIT 1) MINCTE
CROSS JOIN
    (select L.*, B.Branch_City from loan L
    INNER JOIN branch B on L.Branch_Id=B.Branch_ID
    order by Balance desc LIMIT 1) MAXCTE
MINCTE is the derived table to get the minimum value and the associated city name
MAXCTE does the same for the maximum value
Those two derived tables are then CROSS joined
 
Share this answer
 
v2
Comments
raj@hardWork 3-Jun-15 7:59am    
Thank you so much.
raj@hardWork 3-Jun-15 8:02am    
i also did something like this

select b.branchcity,max(L.balance) as max , b.branchcity,min(L.balance) from branch b join loan l on l.branchid=b.branchid group by b.branchcity

and even this is working, but thanks to you so much for the swift response.
CHill60 3-Jun-15 10:39am    
Your query will return the max with city and min with city but on different rows (3 - there is a misspelling of 'Bangalore' in your data). Your expected results implied that you needed Max, city of Max, Min, City of Min in the same row, which is what my solution does
raj@hardWork 8-Jun-15 4:17am    
correct thank you

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