2015-04-08 39 views
1

我試圖在我的網站上找到最後一個缺失的部分,不幸的是我還需要生成銷售報告。我不知道從哪裏開始。如何根據特定的月份/年選擇記錄

如何顯示特定周,月,年的記錄。和總金額

表:payments.tbl

payment_id amount customer_id product_id trx_id currency payment_date 
21  10470  1  15  5F110606611093636 PHP 2015-03-30 

注:PAYMENT_DATE結構DATE

+1

GROUP BY,結合聚合函數SUM。 – jarlh

+0

你能舉個例子嗎?喜歡這個月 –

+0

哪個dbms是yuo使用的? (我問日期/時間處理往往不符合ANSI SQL標準...) – jarlh

回答

0

你得到特定的記錄與WHERE

您會收到一個總金額爲SUM()

如何處理日期取決於您的DBMS,因爲不同的DBMS具有不同的日期功能。下面是MySQL的一個例子:通過貨幣這裏

select 
    sum(amount) as total_amount, 
    currency, 
    count(distinct customer_id) as number_of_different_customers, 
    count(distinct product_id) as number_of_different_products 
from mytable 
where month(payment_date) = month(curdate()) and year(payment_date) = year(curdate()) 
group by currency; 

I組,讓每一個貨幣的結果記錄,因爲它沒有任何意義,總結不同的貨幣量。

MySQL的日期函數在這裏找到:https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

相關問題