簡體   English   中英

SQL選擇順序

[英]SQL select order by desc

這是我的SQL查詢,我只想從銷售表中以降序排列我的SellingPrice需要幫助提前謝謝。

例如

SELECT salesMan.salesmanID, salesMan.name, SUM(sale.sellingPrice)
FROM salesMan 
INNER JOIN sale ON salesMan.salesmanID = sale.salesManID
GROUP BY salesmanID, name;

然后加:

order by SUM(sale.sellingPrice) desc

將別名添加到SUM列,並使用其對結果集進行排序。

Select salesMan.salesmanID,salesMan.name, SUM(sale.sellingPrice) as sellingPriceSum
FROM salesMan INNER JOIN
     sale
     ON salesMan.salesmanID = sale.salesManID
GROUP BY salesmanID,name
ORDER BY sellingPriceSum desc;

這應該工作:

Select salesMan.salesmanID, salesMan.name, SUM(sale.sellingPrice) AS totalSale
FROM salesMan INNER JOIN
     sale
     ON salesMan.salesmanID = sale.salesManID
GROUP BY salesmanID, name
ORDER BY totalSale DESC;

您可以像這樣簡化查詢:

SELECT sm.salesmanID, sm.name, SUM(s.sellingPrice) AS totalSale
FROM salesMan sm 
INNER JOIN sale s ON sm.salesmanID = s.salesManID
GROUP BY sm.salesmanID, sm.name
ORDER BY totalSale DESC;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM