簡體   English   中英

SQL 查詢每月獲得凈銷售額

[英]SQL query to get Net Salse by every month

我正在尋找一個查詢來獲得到目前為止我嘗試過的每月凈銷售額,但我無法得到我想要的。 這是我的Order

+----------+-----------+--------+------------+---------------+-------------+-----------+---------+---------+
| orderID  | custID    | userID | orderDate  | paymentMethod | grossAmount | netAmount | cash    | balance |
+----------+-----------+--------+------------+---------------+-------------+-----------+---------+---------+
| INV-0001 | CUST-0001 | U-001  | 2020-05-01 | Cash Pay      |      525.00 |    525.00 |  550.00 |   25.00 |
| INV-0002 | CUST-0001 | U-001  | 2020-05-01 | Cash Pay      |      240.00 |    240.00 |  250.00 |   10.00 |
| INV-0003 | CUST-0001 | U-001  | 2020-05-01 | Cash Pay      |      220.00 |    220.00 |  250.00 |   30.00 |
| INV-0004 | CUST-0001 | U-001  | 2020-04-30 | Cash Pay      |      895.00 |    895.00 | 1000.00 |  105.00 |
| INV-0005 | CUST-0001 | U-001  | 2020-04-30 | Cash Pay      |      300.00 |    300.00 |  500.00 |  200.00 |
| INV-0006 | CUST-0001 | U-001  | 2020-04-30 | Cash Pay      |      230.00 |    230.00 |  250.00 |   20.00 |
+----------+-----------+--------+------------+---------------+-------------+-----------+---------+---------+

這是我的CustomerReturn

+-------+----------+------------+--------+------------+-----------+-----------+-------------+
| retID | orderID  | itemCode   | userID | retDate    | returnQty | unitPrice | totalAmount |
+-------+----------+------------+--------+------------+-----------+-----------+-------------+
|     1 | INV-0001 | 1800232050 | U-001  | 2020-05-01 |      1.00 |    100.00 |      100.00 |
|     2 | INV-0002 | 1909873674 | U-001  | 2020-05-01 |      2.00 |     55.00 |      110.00 |
|     3 | INV-0004 | 1800232050 | U-001  | 2020-04-30 |      1.00 |    100.00 |      100.00 |
+-------+----------+------------+--------+------------+-----------+-----------+-------------+

公式為(每月賬單總額( Order .netAmount)- 每月總回報( CustomerReturn .totalAmount))

每年每個月都需要獲得凈銷售額。

select orderDate,sum(netAmount)-sum(totalAmount) from `Order` o,CustomerReturn r where o.orderID=r.orderID GROUP BY orderDate;

當我運行此查詢時,它向我顯示了這個

+------------+---------------------------------+
| orderDate  | sum(netAmount)-sum(totalAmount) |
+------------+---------------------------------+
| 2020-04-30 |                          795.00 |
| 2020-05-01 |                          555.00 |
+------------+---------------------------------+

但應該是這樣

+------------+---------------------------------+
| orderDate  | sum(netAmount)-sum(totalAmount) |
+------------+---------------------------------+
| 2020-04-30 |                         1425.00 |
| 2020-05-01 |                          775.00 |
+------------+---------------------------------+

請幫我。 謝謝。!

您的查詢很好,當表 CustomerReturn 中的 OrderId 匹配時,它會獲取所有記錄,並按照您的要求進行求和,但是訂單INV-0003沒有返回,所以這個條件o.orderID=r.orderID當涉及到該記錄時, o.orderID=r.orderID無效並且它忽略了該數據。 進行左連接將解決此問題。

select 
    o.orderDate, 
    sum(o.netAmount)-sum(case when cr.totalAmount is null then 0 else cr.totalAmount end)
from 
    Orders o 
left join 
    CustomerReturn cr
on 
    o.orderID = cr.orderID
group by 
    o.orderDate

左連接將導致cr.totalAmount具有 null 值,以防o.orderID=r.orderID不匹配,然后我們使用這部分; case when cr.totalAmount is null then 0 else cr.totalAmount end以修復 null 問題。

因為您是在日期加入,所以您沒有得到正確答案,因為訂單日期和退貨日期可能有不同的月份。 如果您提取月份然后按照下面的查詢所示進行求和,那就更好了,這里是演示

select
    o.mm as month,
    sum(total_net_amount - total_amount) as total
from
(
  select
    month(orderDate) as mm,
    sum(netAmount) as total_net_amount
  from Orders
  group by
      month(orderDate)
) o
join
(
  select
    month(retDate) as mm,
    sum(totalAmount) as total_amount
  from CustomerReturn
  group by
      month(retDate)
) cr
on o.mm = cr.mm
group by
    o.mm

Output:

*--------------*
|month | total |
*--------------*
| 5    |  775  |
| 4    |  1325 |
*--------------*

學習使用正確的、明確的、標准的、可讀的JOIN語法。 正如另一個答案中所指出的,您需要一個LEFT JOIN 也就是說,編寫邏輯的更簡單方法是:

select o.orderDate, 
       sum(o.netAmount)- coalesce(sum(cr.totalAmount, 0)) as net_amount
from Orders o left join 
     CustomerReturn cr
     on o.orderID = cr.orderID
group by o.orderDate;

暫無
暫無

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

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