0

请找到 SQLfiddle URL:

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

我该如何纠正?

谢谢和问候,曼杰什。

4

3 回答 3

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
于 2012-11-25T12:24:11.780 回答
0

您是否尝试用另一个选择包装您的查询并按 BRANCH_NAMES 对其进行分组

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

但是如果你像这样把你的结果放在一起,你肯定会在余额列中丢失数据。

于 2012-11-25T12:33:13.420 回答
0

我猜你的查询结构本身有问题,因为它不仅仅是对distinct.

您的连接表,并为您拥有的每一行检索不同的值,您实际上只想为KOTTAKKAL和中的每一个提供一个KALPETTA

请注意,在您的示例中,尽管distinct执行了子句的添加,但不会出现任何差异,因为您拥有的行不同的。341.00在列平衡中,对于和394.00的每个条目,您都有KOTTAKKALKALPETTA

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

您要么必须确定所需的结果,要么应对对 column 进行 sum other 计算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,除了 column 之外,该查询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

问候!

于 2012-11-25T14:09:53.813 回答