tsql newbie here。
我有一张桌子,类似于这张桌子:
CarId CarName UserId RentedTimes CrashedTimes
`````````````````````````````````````````````````````````
1 Ferrari 1 2 0
2 DB9 1 5 0
3 Ferrari 2 4 0
4 Audi 3 1 0
5 Audi 1 1 0
假设该表被称为“汽车”,我试图选择每辆汽车租用的总次数。根据上表,法拉利总共租了6次,DB9五次,奥迪两次。
我试过这样做:
select CarName, SUM(RentedTimes)
from Cars
group by CarName, RentedTimes
order by RentedTimes desc
但是,它正在返回两排法拉利,其中2,4个作为租用时间等等。
我如何选择所有车辆,并且每次租车的总时间,请?
由于
<小时/> 编辑了查询以包含排序顺序,抱歉。
答案 0 :(得分:1)
select CarName, SUM(RentedTimes)
from Cars
group by CarName
ORDER BY SUM(RentedTimes) DESC
尝试这种方式..从
中删除组中的RentedTimes