[英]Getting MAX value of one field based on 2nd field in one table
I have 2 tables "room" & "building" 我有2个桌子“房间”和“建筑物”
I am trying to get the max(roomPrice) AS "Total Building Price" grouped by building name. 我正在尝试按建筑物名称分组获得最大“房间总价” AS“总建筑物价”。 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.
我知道应该有一个子查询来从roomPrice获取值,因此max(roomprice)可以工作,但我无法正确地做到这一点。
Table1 ( roomNo, buildingNo, roomType, roomPrice )
Table2 ( buildingNo, buldingName, buildingCity )
Sorry just started in SQL and books do not tell all. 抱歉,SQL才刚刚开始,书还没有全部介绍。
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 http://sqlfiddle.com/#!9/143c2/6
JOIN
combine every row of one Table with all rows of a other Table. JOIN
将一个表的每一行与另一表的所有行合并在一起。 If you add a condition with ON
you can reduce the combinations you get. 如果将条件添加为“
ON
,则可以减少得到的组合。 In this case only rows with the same buildingNo
on both tables will be "glued" together. 在这种情况下,只有两个表上具有相同
buildingNo
行才会被“粘合”在一起。 And then we group them by this buildingNo
and only take the row with the MAX
of roomPrice
or simply create the SUM
. 然后,我们根据
buildingNo
它们进行分组,并仅使用具有roomPrice
MAX
的roomPrice
或简单地创建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): 如果只需要知道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()) : 但是,正如您的问题表明总建筑价格一样,您实际上可能指的是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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.