MySQL Complex SELECT查询 - JOINS

时间:2012-11-25 12:20:10

标签: mysql select join

请找到SQLfiddle网址:

http://sqlfiddle.com/#!2/c8002/1/0

实际输出应为:

region_id   status1         branches        balance     fses
    status0         discount     branch_names       telecallers
2   NULL    4       400.00  30   KOTTAKKAL  341.00  3   3
2   NULL    4       800.00  31   KALPETTA   394.00  3   3

我得到的重复输出如下:

region_id   status1         branches        balance     fses
    status0         discount     branch_names       telecallers
2   NULL    4       400.00  30   KOTTAKKAL  341.00  3   3
2   NULL    4       400.00  30   KOTTAKKAL  394.00  3   3 
2   NULL    4       800.00  31   KALPETTA   341.00  3   3
2   NULL    4       800.00  31   KALPETTA   394.00  3   3

我该如何改正呢?

谢谢&问候, Manjesh。

3 个答案:

答案 0 :(得分:0)

试试这个::

在您的查询中,只需添加以下部分:

GROUP BY branch_names

或者您可以将SELECT部分​​中的DISTINCT作为:

Select DISTINCT(branch_names) from....

最终查询将是::

SELECT E.`region_id`,a0.status0,a1.status1,k.discount,k.branches
,k.branch_names,BAL.balance,TCLRS.telecallers,FSE.fses

FROM tbl_insurance_excel E


LEFT OUTER JOIN (Select E2.region_id, count(`id`) as status1
from tbl_insurance_excel E2 Left Join  tbl_recipt_general_details on id=insurance_excel_id
JOIN tbl_branches  on policy_closed_branch= branch_id
Where E2.`row_status` =1 AND E2.canceled_status='no' and E2.region_id=2   Group by region_id) a1 ON a1.region_id=E.region_id

LEFT OUTER JOIN (Select E2.region_id, count(`id`) as status0
from tbl_insurance_excel E2 Left Join  tbl_recipt_general_details on id=insurance_excel_id
JOIN tbl_branches  on policy_closed_branch= branch_id
Where E2.`row_status` =0 AND E2.canceled_status='no' and E2.region_id=2   Group by region_id) a0 ON a0.region_id=E.region_id

LEFT JOIN (
SELECT IEX.region_id,B.branch_name as branch_names,B.branch_id as branches, sum( DDT.discounts_amount ) as discount
FROM tbl_insurance_excel IEX
LEFT JOIN tbl_recipt_general_details RGD ON IEX.id = RGD.insurance_excel_id
LEFT JOIN tbl_discounts_details DDT ON RGD.rec_gene_id = DDT.recipt_general_id
LEFT JOIN tbl_branches B ON B.branch_id = RGD.policy_closed_branch
WHERE IEX.region_id =2
GROUP BY B.branch_id)k on k.region_id=E.region_id

LEFT JOIN (
SELECT IEX.region_id,B.branch_id as branches, sum( RGD.`recipt_bal_amount` ) AS balance
FROM tbl_insurance_excel IEX
LEFT JOIN tbl_recipt_general_details RGD ON IEX.id = RGD.insurance_excel_id
LEFT JOIN tbl_branches B ON B.branch_id = RGD.policy_closed_branch
WHERE IEX.region_id =2
GROUP BY B.branch_id)BAL ON BAL.region_id=E.region_id

LEFT JOIN (
SELECT ex.region_id, B.branch_id AS branches, count( F.`fse_id` ) AS telecallers
FROM tbl_branches B
LEFT JOIN tbl_team_leader L ON B.branch_id = L.leader_branch_id
LEFT JOIN tbl_fse F ON L.leader_id = F.leader_id
JOIN (

SELECT region_id
FROM tbl_insurance_excel
WHERE `region_id` =2
GROUP BY region_id
) AS ex ON B.region_id = ex.region_id
WHERE F.fse_category = 'Telecaller'
GROUP BY B.branch_id
) TCLRS ON  TCLRS .region_id=E.region_id


LEFT JOIN (
SELECT ex.region_id, B.branch_id AS branches, count( F.`fse_id` ) AS fses
FROM tbl_branches B
LEFT JOIN tbl_team_leader L ON B.branch_id = L.leader_branch_id
LEFT JOIN tbl_fse F ON L.leader_id = F.leader_id
JOIN (

SELECT region_id
FROM tbl_insurance_excel
WHERE `region_id` =2
GROUP BY region_id
) AS ex ON B.region_id = ex.region_id
WHERE F.fse_category = 'Fse'
GROUP BY B.branch_id
) FSE ON  FSE.region_id=E.region_id



 where E.region_id=2 group by  k.branches

答案 1 :(得分:0)

你是否试图用另一个选择包装你的查询并将其分组为BRANCH_NAMES

SELECT * FROM
(
    << your query goes here>>
) o
GROUP BY o.BRANCH_NAMES

但是,如果您将结果放在一起,您将在余额栏中明确地放松数据。

答案 2 :(得分:0)

我猜您的查询结构本身存在问题,因为它不仅仅是对distinct的请求。

您的加入表格,并为您拥有的每一行检索不同的值,您实际上只希望每个KOTTAKKALKALPETTA都有一个。

请注意,在您的示例中,虽然执行了distinct子句的添加,但是没有区别,因为的行是不同的。在列余额中,341.00394.00的每个条目都包含KOTTAKKALKALPETTA

如果使用组,是的,您将丢失balance的信息,因为在group by之后返回的结果仅是分组的不同元素中的第一个(如果列具有多个值)

您要么必须确定所需的结果,要么应对在balance列上进行其他计算。

我想结果,以及你愿意做的正确加入是:

SELECT DISTINCT E.`region_id`,a0.status0,a1.status1,k.discount,k.branches
,k.branch_names, k.balance,TCLRS.telecallers,FSE.fses

FROM tbl_insurance_excel E


LEFT OUTER JOIN (SELECT E2.region_id, count(`id`) AS status1
FROM tbl_insurance_excel E2 LEFT JOIN  tbl_recipt_general_details ON id=insurance_excel_id
JOIN tbl_branches  ON policy_closed_branch= branch_id
WHERE E2.`row_status` =1 AND E2.canceled_status='no' AND E2.region_id=2   GROUP BY region_id) a1 ON a1.region_id=E.region_id

LEFT OUTER JOIN (SELECT E2.region_id, count(`id`) AS status0
FROM tbl_insurance_excel E2 LEFT JOIN  tbl_recipt_general_details ON id=insurance_excel_id
JOIN tbl_branches  ON policy_closed_branch= branch_id
WHERE E2.`row_status` =0 AND E2.canceled_status='no' AND E2.region_id=2   GROUP BY region_id) a0 ON a0.region_id=E.region_id

LEFT JOIN (
SELECT IEX.region_id, B.branch_name AS branch_names, B.branch_id AS branches, sum( DDT.discounts_amount ) AS discount, sum( RGD.`recipt_bal_amount` ) AS balance
FROM tbl_insurance_excel IEX
LEFT JOIN tbl_recipt_general_details RGD ON IEX.id = RGD.insurance_excel_id
LEFT JOIN tbl_discounts_details DDT ON RGD.rec_gene_id = DDT.recipt_general_id
LEFT JOIN tbl_branches B ON B.branch_id = RGD.policy_closed_branch
WHERE IEX.region_id =2
GROUP BY B.branch_id)k ON k.region_id=E.region_id

LEFT JOIN (
SELECT ex.region_id, B.branch_id AS branches, count( F.`fse_id` ) AS telecallers
FROM tbl_branches B
LEFT JOIN tbl_team_leader L ON B.branch_id = L.leader_branch_id
LEFT JOIN tbl_fse F ON L.leader_id = F.leader_id
JOIN (

SELECT region_id
FROM tbl_insurance_excel
WHERE `region_id` =2
GROUP BY region_id
) AS ex ON B.region_id = ex.region_id
WHERE F.fse_category = 'Telecaller'
GROUP BY B.branch_id
) TCLRS ON  TCLRS .region_id=E.region_id


LEFT JOIN (
SELECT ex.region_id, B.branch_id AS branches, count( F.`fse_id` ) AS fses
FROM tbl_branches B
LEFT JOIN tbl_team_leader L ON B.branch_id = L.leader_branch_id
LEFT JOIN tbl_fse F ON L.leader_id = F.leader_id
JOIN (

SELECT region_id
FROM tbl_insurance_excel
WHERE `region_id` =2
GROUP BY region_id
) AS ex ON B.region_id = ex.region_id
WHERE F.fse_category = 'Fse'
GROUP BY B.branch_id
) FSE ON  FSE.region_id=E.region_id


  WHERE E.region_id=2 GROUP BY  k.branches

请注意,您正在进行另一个联接,并将查询中应该唯一的结果组合在一起。您已加入来自您命名为k的查询的数据,然后将结果与您命名为BAL的查询一起加入,除了列balance之外,该查询具有相同的条目,所以branch_id's被复制了。

你很快就有了:

k.branch_id LEFT JOIN BAL.branch_id

导致您的行重复,具有不同的balance值。

发布查询获得的结果是:

REGION_ID   STATUS0 STATUS1 DISCOUNT    BRANCHES    BRANCH_NAMES    BALANCE TELECALLERS FSES
2   (null)  4   400 30  KOTTAKKAL   341 3   3
2   (null)  4   800 31  KALPETTA    394 3   3

问候!