我猜你的查询结构本身有问题,因为它不仅仅是对distinct
.
您的连接表,并为您拥有的每一行检索不同的值,您实际上只想为KOTTAKKAL
和中的每一个提供一个KALPETTA
。
请注意,在您的示例中,尽管distinct
执行了子句的添加,但不会出现任何差异,因为您拥有的行是不同的。341.00
在列平衡中,对于和394.00
的每个条目,您都有KOTTAKKAL
和KALPETTA
。
如果使用 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
问候!