如何获取外键SQL Server的最大数量

时间:2016-01-16 07:27:11

标签: sql-server

我有两张桌子

NoRoom varchar(50) primary key,
kindRoom nvarchar(50),
price float,
priceCurrent float,
R_status nvarchar(50),  

nametour nvarchar(50),
checkIn date,
checkOut date,
company nvarchar(50),
idRoom nvarchar(50) foreign key references Rooms(NoRoom)

这是我的代码

SELECT rooms.noroom, 
       rooms.kindroom, 
       Count(checkintour.idroom) AS numberoforder 
FROM   rooms 
       LEFT JOIN checkintour 
              ON( rooms.noroom = checkintour.idroom ) 
GROUP  BY rooms.noroom, 
          rooms.kindroom 
ORDER  BY numberoforder DESC 

输出就像这样

+-----------+----------+----------------+
|   No Room | KindRoom | NumberOfOrder  |
+-----------+----------+----------------+
|  PO0051     VIP               1 
|  PO0053     Single            1 
|  PO0054     VIP               1
|  PO0055     VIP               1 
|  PO0056     VIP               1 
|  PO0057     VIp               1
|  PO0058     VIP               1
|  PO0059     VIP               .
|  PO0060     .                 ..
|  PO0061     ..                ...
|  PO0062
|           |          |
|           |          |                |
|           |          |                |
+-----------+----------+----------------+

如何获得最大NumberOfOrder? 换句话说,如果房间的类型是VIP我想要总结那个房间的所有订单数量

2 个答案:

答案 0 :(得分:0)

IIllegalException

答案 1 :(得分:0)

好的我找到了

select Rooms.kindRoom,
COUNT(CheckInTour.idRoom) as numberoforder
from Rooms
left join CheckInTour
on(Rooms.NoRoom = CheckInTour.idRoom)
group by
Rooms.kindRoom
order by numberoforder desc