2016-01-15 136 views
0

我有3個表:SQL查詢與聯接和GROUP BY

enter image description here

enter image description here

enter image description here

我想ConsultantBalance,金額和TAXAMOUNT GROUP BY ConsultantId的總和。預期結果:

enter image description here

我想不會太複雜的SQL查詢,避免子查詢,如果可能的。

SQL查詢來創建結構:

CREATE TABLE IF NOT EXISTS `ConsultantBalance` (`id` int(11) NOT NULL AUTO_INCREMENT, `ConsultantID` int(11) DEFAULT NULL, `BalanceType` int(11) DEFAULT NULL, `ConsultantBalance` decimal(10,2) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=6 ; 
INSERT INTO `ConsultantBalance` (`id`, `ConsultantID`, `BalanceType`, `ConsultantBalance`) VALUES (1, 1, 1, 100.00), (2, 1, 2, 50.00), (3, 2, 1, 200.00), (4, 2, 2, 230.00), (5, 3, 1, 300.00); 
CREATE TABLE IF NOT EXISTS `ConsultantCredit` (`id` int(11) NOT NULL AUTO_INCREMENT, `ConsultantID` int(11) DEFAULT NULL, `CreditType` char(10) DEFAULT NULL, `Amount` decimal(10,2) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=7 ; 
INSERT INTO `consultantCredit` (`id`, `ConsultantID`, `CreditType`, `Amount`) VALUES (1, 1, 'Deposit', 23.00), (2, 1, 'Cash', 942.00), (3, 2, 'Deposit', 17.00), (4, 2, 'Cash', 932.00), (5, 3, 'Deposit', 125.00), (6, 3, 'Cash', 922.00); 
CREATE TABLE IF NOT EXISTS `ConsultantTax` (`id` int(11) NOT NULL AUTO_INCREMENT, `ConsultantID` int(11) DEFAULT NULL, `TaxName` char(5) DEFAULT NULL, `TaxAmount` decimal(10,2) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=7 ; 
INSERT INTO `ConsultantTax` (`id`, `ConsultantID`, `TaxName`, `TaxAmount`) VALUES (1, 1, 'GST', 7.00), (2, 1, 'HST', 10.00), (3, 2, 'GST', 17.00), (4, 2, 'HST', 150.00), (5, 3, 'GST', 37.00), (6, 3, 'HST', 140.00); 

回答

0

我的SQL查詢看起來相當大的和複雜的,但它的工作原理

SELECT 
ConsultantBalance1.ConsultantId, 
(
    SELECT SUM(ConsultantBalance2.ConsultantBalance) 
    FROM ConsultantBalance AS ConsultantBalance2 
    WHERE ConsultantBalance2.ConsultantId = ConsultantBalance1.ConsultantId 
) AS sumConsultantBalance, 
(
    SELECT SUM(ConsultantCredit2.Amount) 
    FROM ConsultantCredit AS ConsultantCredit2 
    WHERE ConsultantCredit2.ConsultantId = ConsultantBalance1.ConsultantId 
) AS sumAmount, 
(
    SELECT SUM(ConsultantTax2.TaxAmount) 
    FROM ConsultantTax AS ConsultantTax2 
    WHERE ConsultantTax2.ConsultantId = ConsultantBalance1.ConsultantId 
) AS sumTaxAmount 
FROM ConsultantBalance AS ConsultantBalance1 
INNER JOIN ConsultantCredit AS ConsultantCredit1 ON (ConsultantBalance1.ConsultantId = ConsultantCredit1.ConsultantId) 
INNER JOIN ConsultantTax AS ConsultantTax1 ON (ConsultantBalance1.ConsultantId = ConsultantTax1.ConsultantId) 
GROUP BY ConsultantBalance1.ConsultantId 
1

下面的查詢第一聚集的款項爲三個表,然後將三個表連接在一起以獲得所需的結果集。

SELECT t1.ConsultantID, t1.sumConsultantBalance, t2.sumAmount, t3.sumTaxAmount 
FROM 
(
    SELECT ConsultantID, SUM(ConsultantBalance) AS sumConsultantBalance 
    FROM ConsultantBalance 
    GROUP BY ConsultantID 
) t1 
INNER JOIN 
(
    SELECT ConsultantID, SUM(Amount) AS sumAmount 
    FROM ConsultantCredit 
    GROUP BY ConsultantID 
) t2 
ON t1.ConsultantID = t2.ConsultantID 
INNER JOIN 
(
    SELECT ConsultantID, SUM(TaxAmount) AS sumTaxAmount 
    FROM ConsultantTax 
    GROUP BY ConsultantID 
) t3 
ON t2.ConsultantID = t3.ConsultantID 
0

與Tim Biegeleisen的解決方案類似,但只有一個子查詢。在MySQL子查詢中,連接對索引使用不佳。此解決方案避免了1個子查詢: -

SELECT cc.ConsultantID, sub0.sumConsultantBalance, sub1.sumTaxAmount, SUM(cc.Amount) AS sumAmount 
FROM ConsultantCredit cc 
(
    SELECT ConsultantID, SUM(ConsultantBalance) AS sumConsultantBalance 
    FROM ConsultantBalance 
    GROUP BY ConsultantID 
) sub0 
ON cc.ConsultantID = sub0.ConsultantID 
INNER JOIN 
(
    SELECT ConsultantID, SUM(TaxAmount) AS sumTaxAmount 
    FROM ConsultantTax 
    GROUP BY ConsultantID 
) sub1 
ON cc.ConsultantID = sub1.ConsultantID 
GROUP BY cc.ConsultantID, sub0.sumConsultantBalance, sub1.sumTaxAmount