简体   繁体   中英

Getting MAX value of one field based on 2nd field in one table

I have 2 tables "room" & "building"

I am trying to get the max(roomPrice) AS "Total Building Price" grouped by building name. I know there should be a sub-query to get the values from roomPrice, so the max(roomprice) will work, but I just can not get that right.

Table1 ( roomNo, buildingNo, roomType, roomPrice )
Table2 ( buildingNo, buldingName, buildingCity )

Sorry just started in SQL and books do not tell all.

Try this way:

select a.buildingNo,b.buildingName,b.buildingCity ,a.max_room_price from
(select buildingNo,max(roomPrice) as max_room_price from table1 GROUP BY buildingNo) as a

LEFT JOIN
(select buildingNo, buildingName,buildingCity from table2)as b
on a.buildingNo = b. buildingNo

This one works fine according to the output you want. hope it helps

Try this as well:

SELECT t2.buildingName as 'Building Name', MAX(t1.roomPrice) AS 'Total Building Price'
FROM Table2 t2
INNER JOIN Table1 t1 ON t1.buildingNo = t2.buldingNo
GROUP BY t2.buildingName

try this one

SELECT tab1.buildingNo, MAX(tab1.roomPrice)
FROM tab1 JOIN tab2 ON tab1.buildingNo = tab2.buildingNo
GROUP BY  tab1.buildingNo

if you want a sum over a whole building, use this

SELECT tab1.buildingNo, SUM(tab1.roomPrice)
FROM tab1 JOIN tab2 ON tab1.buildingNo = tab2.buildingNo
GROUP BY  tab1.buildingNo

http://sqlfiddle.com/#!9/143c2/6

JOIN combine every row of one Table with all rows of a other Table. If you add a condition with ON you can reduce the combinations you get. In this case only rows with the same buildingNo on both tables will be "glued" together. And then we group them by this buildingNo and only take the row with the MAX of roomPrice or simply create the SUM .

Try this:

create table #Table2(buildingNo int, buldingName nvarchar(50), buildingCity    varchar(50))
  Insert into #Table2 values
  (1,'A','Delhi'),
  (2,'B','Delhi')


   Create Table #Table1  (roomNo int, buildingNo int, roomType  varchar(50), roomPrice int)
   Insert into #Table1 values
   (1,1,'2BHK',50000),
   (2,2,'2BHK',60000),
  (3,1,'1BHK',55000),
  (4,2,'2BHK',65000),
  (4,1,'2BHK',80000),
   (4,2,'2BHK',90000)

 SELECT max(roomPrice) AS [Total Building Price],buldingName FROM #Table1  t1
    JOIN  #Table2 t2
    ON t1.buildingNo=t2.buildingNo
    group by buldingName

You do not need a sub-query if you only need to know max(roomPrice):

SELECT tab2.buildingName, max(tab1.roomPrice) AS TotalBuildingPrice
FROM tab1
JOIN tab2 ON tab1.buildingNo = tab2.buildingNo
GROUP BY tab2.buildingName

However as your question indicates total building price, you might actually mean max(sum()) :

SELECT tab2.buildingName, sum(tab1.roomPrice) AS TotalBuildingPrice
FROM tab1
JOIN tab2 ON tab1.buildingNo = tab2.buildingNo
GROUP BY tab2.buildingName
ORDER BY TotalBuildingPrice DESC
LIMIT 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM