简体   繁体   English

在一张表中基于第二个字段获取一个字段的MAX值

[英]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 MAXroomPrice或简单地创建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.

相关问题 如何将数据从一个表插入到另一个表,并且在第二个表中匹配该字段 - how to insert data from one table to another table and in 2nd table matches the field 查询以根据一个字段的最大值和另一字段的特定值查找数据 - query to find data based on max of one field and specific value of another 将具有唯一字段的记录移到第二个表中 - Move records with unique field into 2nd table 如何基于另一个表中的值设置一个表的字段 - How to set one table's field based on value in another table 在MySQL GROUP BY中,如何选择第n个值(例如,不是max或min,而是第2个)? - In a MySQL GROUP BY, how can one select the nth value --not the max or min, but 2nd, for example? 用于根据不同表中的不同字段的值更改一个表上的字段值的SQL代码 - SQL code to change value of a field on one table based on value of a different field in a different table MySQL:基于同一表的一条记录中一个选定字段中的值删除表中的多行 - MySQL: Delete multiple rows in a table based on a value in one selected field in one record of the same table MySQL-基于一个字段的表中的列总和 - mySQL - Sum columns in a table based on one field 基于另一个字段获取一个字段的不同mysql结果 - getting distinct mysql result of one field based on another field 在一个表中添加第二个外键-MySql - adding 2nd foreign keys in one table - MySql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM