2013-01-16 128 views
0

我有一張存儲發票數據的表。一旦它被支付is_paid將被標記爲1計算組計數,使用mysql的給定日期的總和

那麼什麼IM試着做的是讓所有的未付發票(where is_paid =0)爲每個用戶給定日期,總未付金額爲每一位員工和待開發票的計數一起該期間

SELECT 
       receivable.amount, 
       receivable.admission, 
receivable.grouped, 
       receivable.month, 
       emp.name 
      FROM 
       receivable JOIN students ON receivable.emp_id = emp.id 
where receivable.is_paid='0' 
group by receivable.emp_id 
ORDER BY CAST(receivable.grouped AS UNSIGNED) , receivable.grouped 

如何更改上面的代碼,以計算待開發票的計在給定期間,對每個用戶未決量和總的這一期間分組(的總和用戶輸入的時間,以便記錄需要在包括該日期之前顯示的所有內容)

以下是該報告的結構

enter image description here

任何幫助將不勝感激

+0

你能澄清什麼'grouped','admission'和'月應收賬款表中? – PinnyM

回答

0

猜測month指的是第一個月的 - 如果你能澄清這一點我可以寫一些東西更準確。從本質上講,你需要使用SUM和COUNT聚合函數,並宣佈每隔一列被返回作爲集團的一部分BY子句:

SELECT 
    receivable.emp_id, 
    emp.name, 
    receivable.grouped, 
    receivable.month, 
    SUM(receivable.amount) as totalAmount 
    COUNT(*) as totalCount 
FROM receivable 
JOIN emp ON receivable.emp_id = emp.id 
WHERE receivable.is_paid='0' 
    AND receivable.month = '2013-01-01' 
GROUP BY receivable.emp_id, emp.name, receivable.grouped, receivable.month 
ORDER BY CAST(receivable.grouped AS UNSIGNED), receivable.grouped