简体   繁体   English

如何使用SQL以整数的总和降序排序

[英]How to order by a summation of integers in descending order with SQL

I'm creating a website in which I have to use PHP to pull out of a MySQL database and for this project I need to SELECT Total sales by customerName ORDER BY Total sales DESCending from my table. 我正在创建一个网站,在该网站中必须使用PHP提取MySQL数据库,对于该项目,我需要从表中SELECT Total sales by customerName ORDER BY Total sales DESCending This is my table (the "sales" table): 这是我的表(“ sales”表): 在此处输入图片说明

I need to have it output this to a table (but it is the actual information not just "Customer" or "Total Sales"): 我需要将其输出到表中(但它是实际信息,而不仅仅是“客户”或“总销售额”):

<tr>
    <td>Customer</td>
    <td>Total Sales</td>
    <td>Time of Sale</td>
    <td>Sales Department</td>
    <td>Item Sold</td>
    <td>Number of Items Sold</td>
    <td>Cost per Item</td>
    <td>Sales Person</td>
</tr>;

So basically I need to know how to have an SQL statement that pulls the information out of the table called "sales" for every saleItem with the customerName first, then the total (summation) (not just the number of items for that specific saleItem , it has to be every item that the customer has bought) numberOfItems that the customer has bought, then the saleDTM (time of the sale), then the saleDept (the sale department), then the saleItem (the item sold), then the numberOfItems (the number of items sold for the type of saleItem ), then the costPerItem (the cost of the saleItem ), then finally the salesPerson (the person that sold the saleItem ). 所以基本上我需要知道如何有拉动出的信息被称为“销售”表中的每一个SQL语句saleItemcustomerName ,然后再总(总和)(不只是物品的具体数量saleItem ,它必须是客户购买的每个商品)客户购买的numberOfItems ,然后是saleDTM (销售时间),然后是saleDept (销售部门),然后是saleItem (已售出的商品),然后是numberOfItems (对于类型售出的商品数量saleItem ),那么costPerItem (的成本saleItem ),然后最后salesPerson (即销售的人saleItem )。 All of that has to be ordered by the total (every item bought by the customer, not just the number of items bought for a specific saleItem ) numberOfItems that the customer has bought. 所有这一切都必须由总(由客户买的每一个项目,而不仅仅是项目数量买了一个特定的,责令saleItemnumberOfItems客户已经买了。 How do I do that SQL statement? 我该如何执行该SQL语句?

You should be able to get the total sales per sales person with something along these lines. 您应该能够通过以下方式获得每个销售人员的总销售额。

select salesPerson, sum(numberOfItems * costPerItem) as total_sales
from sales
group by salesPerson
order by total_sales desc;

But there's no way to join that back to the sales table to give you, say, "time of sale" or "sales department". 但是,无法将其重新加入到销售表中,例如“销售时间”或“销售部门”。 It just doesn't make sense. 只是没有意义。

Total sales per customer is similar, but it's also a different thing. 每个客户的总销售额相似,但这又是不同的。 (In your question, you refer to both "total sales per salesperson" and "total sales per customer".) The individual details--time of sale, sale department, etc--are yet another thing. (在您的问题中,您同时指“每个销售人员的总销售额”和“每个客户的总销售额”。)个人详细信息(销售时间,销售部门等)是另一回事。 You seem to be talking about three different kinds of reports, but maybe you know something the rest of us don't. 似乎在谈论三种不同类型的报告,但是也许您知道我们其他人所不知道的一些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM