2009-12-08 97 views
0

我試圖得到一個在MySQL 5.0.81中工作的查詢,它將提取尚未支付的發票上的到期金額。MySQL帳戶老化報告

我有一個看起來像這樣三個表:沒有支付基於CLIENT_ID(invoices_client_id) 拉發票(invoices_status = 0):

CREATE TABLE `CLMS_invoices` (
    `invoices_id` mediumint(8) NOT NULL auto_increment, 
    `invoices_client_id` mediumint(8) NOT NULL default '0', 
    `invoices_datetimestamp` varchar(50) collate latin1_german2_ci NOT NULL default '', 
    `invoices_description` varchar(100) collate latin1_german2_ci NOT NULL default '', 
    `invoices_discount` decimal(12,2) NOT NULL default '0.00', 
    `invoices_note` text collate latin1_german2_ci NOT NULL, 
    `invoices_status` tinyint(1) NOT NULL default '0', 
    PRIMARY KEY (`invoices_id`) 
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci AUTO_INCREMENT=5 ; 

CREATE TABLE `CLMS_invoices_payments` (
    `invoices_payments_id` mediumint(8) NOT NULL auto_increment, 
    `invoices_payments_invoice_id` mediumint(8) NOT NULL default '0', 
    `invoices_payments_datetimestamp` varchar(50) collate latin1_german2_ci NOT NULL, 
    `invoices_payments_type` mediumint(8) NOT NULL default '0', 
    `invoices_payments_paid` decimal(12,2) NOT NULL default '0.00', 
    PRIMARY KEY (`invoices_payments_id`) 
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci AUTO_INCREMENT=10 ; 

CREATE TABLE `CLMS_invoices_products` (
    `invoices_products_id` mediumint(8) NOT NULL auto_increment, 
    `invoices_products_invoice_id` mediumint(8) NOT NULL default '0', 
    `invoices_products_name` varchar(50) collate latin1_german2_ci NOT NULL default '', 
    `invoices_products_price` decimal(12,2) NOT NULL default '0.00', 
    `invoices_products_profit` decimal(12,2) NOT NULL default '0.00', 
    `invoices_products_qty` mediumint(8) NOT NULL default '0', 
    `invoices_products_shipping` decimal(12,2) NOT NULL default '0.00', 
    PRIMARY KEY (`invoices_products_id`) 
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci AUTO_INCREMENT=12 ; 

我需要查詢做到以下幾點。

老化應該如下: 不到30天 31 - 60天 61 - 90天 超過90天

它也需要返回與該下降的數額相關聯的客戶端id進入老齡化羣體。

在組中的美元價值的計算方法如下: 發票產品成本 - 發票貼現 - 已付金額至今

發票產品成本的計算公式爲

SUM((invoices_products_price + invoices_products_profit + invoices_products_shipping)* invoices_products_qty)WHERE invoices_products_invoice_id = invoices_id

發票折扣存儲在invoices_discount

金額到目前爲止付費被計算爲

SUM(invoices_payments_paid)WHERE invoices_payments_invoice_id = invoices_id

最終的結果看起來應該像這樣:

client_id pastDue1 pastDue2 pastDue3 pastDue4 
1 12.00 0.00 0.00 15.00 
5 2.00 60.00 80.00 32.00 
etc 

有人可以幫我建立這個查詢嗎?

+2

此列命名風格讓我感到噁心。 – cherouvim 2009-12-08 22:26:48

+2

我很樂意幫忙!我應該在哪裏發送我的諮詢服務建議? – JohnFx 2009-12-08 22:32:00

+1

@cherouvim:等到你看到一個3表連接完全合格的字段名稱... – gahooa 2009-12-08 22:34:13

回答

0

如果我理解正確 - 這裏的關鍵問題是將所有用戶分成不同的年齡段。

這可以通過IF(condition, then, else)聲明在MySQL如下實現:

把第一次查詢到一個臨時表 -

CREATE TABLE `tmeporary_invoices` 
SELECT invoices.`invoices_client_id`, 
     SUM((products.`invoices_products_price` + products.`invoices_products_profit` + products.`invoices_products_shipping`) * products.`invoices_products_qty`) AS invoice_product_cost, 
     SUM(payments.`invoices_payments_paid`) AS amount_paid_so_far, 
     IF(DATEDIFF(NOW(), invoices.`invoices_datetimestamp`) <= 30 , 'pastDue1', IF(DATEDIFF(NOW(),invoices.`invoices_datetimestamp`) <= 60, 'pastDue2', 'pastDue2')) AS age 
FROM `CLMS_invoices` invoices 
    LEFT JOIN `CLMS_invoices_payments` payments 
      ON payments.`invoices_payments_invoice_id` = invoices.`invoices_id` 
    LEFT JOIN `CLMS_invoices_products` products 
      ON products.`invoices_products_invoice_id` = invoices.`invoices_id` 
WHERE invoices.`invoices_status = 0 

事後從臨時表中選擇上面如下 -

SELECT `invoices_client_id`, 
     SUM(`invoice_product_cost`) AS invoice_product_cost, 
     SUM(`amount_paid_so_far`) AS amount_paid_so_far, 
     SUM(IF(`age` = 'pastDue1',1,0) AS `pastDue1`, 
     SUM(IF(`age` = 'pastDue2',1,0) AS `pastDue2`, 
     SUM(IF(`age` = 'pastDue3',1,0) AS `pastDue3`, 
FROM `tmeporary_invoices` 
GROUP BY `invoices_client_id` 

該查詢應該作爲一般指導方針來說明如何完成所需的結果(只需複製/粘貼它們就可以了在命名約定/語法方面存在一些錯誤,因爲我目前無法測試它們。

祝你好運!