我有2个表客户和订单,并找到最大订单的客户ID

时间:2016-04-11 05:41:44

标签: sql-server

tbi_Customer             tbl_order
CustId int               orderid int
Customer no              OrderName varchar(50)
                         CustId int

1 个答案:

答案 0 :(得分:0)

试试这个

select top 1 a.customerno,b.totalorder from tbl_Customer as a
join (select custid,count(custid) as totalorder from tbl_order group by custid) b on a.custid=b.custid 
order by b.totalorder desc