带连接和分组的SQL查询

时间:2016-01-15 09:34:30

标签: mysql sql

我有3张桌子:

enter image description here

enter image description here

enter image description here

我希望获得ConsultantBalance,Amount和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);

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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