如何删除具有不同计数的重复项

问题描述 投票:0回答:5

如果两个表中有相同的部门名称但计数不同,如何删除重复项。如果有相同的部门,程序不应该采用0部门

select  distinct d.fullname,count(h.isn)
from subdept d

left join subhuman h on h.deptisn=d.isn
left join subject j on h.isn=j.isn
left join emilitary em on em.emplisn=j.isn
where   h.sex = 'М' and em.milsign='Y'
group by rollup (d.fullname)

union

select d.fullname, 0 from subdept d

目前的结果是:

预期结果是:

sql
5个回答
1
投票

试试这个

select d.fullname, ISNULL(dd.c,0)  from subdept d
LEFT JOIN 
(
    select  distinct d.fullname,count(h.isn) c
    from subdept d

    left join subhuman h on h.deptisn=d.isn
    left join subject j on h.isn=j.isn
    left join emilitary em on em.emplisn=j.isn
    where   h.sex = 'М' and em.milsign='Y'
    group by rollup (d.fullname)
) dd
ON d.fullname=dd.fullname

0
投票

试试这个

select d.fullname,count(distinct h.isn)
from subdept d

left join subhuman h on h.deptisn=d.isn
left join subject j on h.isn=j.isn
left join emilitary em on em.emplisn=j.isn
where   h.sex = 'М' and em.milsign='Y'
group by rollup (d.fullname)

union

select d.fullname, 0 from subdept d

0
投票

我怀疑你只是想把em.milsign的条件移到on条款。这个条件是将外连接转换为内连接:

select d.fullname, count(h.isn)
from subdept d left join
     subhuman h
     on h.deptisn = d.isn left join
     subject j
     on h.isn = j.isn left join
     emilitary em
     on em.emplisn = j.isn and em.milsign = 'Y'
where h.sex = 'М'
group by rollup (d.fullname);

注意:select distinct几乎不需要group by


0
投票

绝对是你可以以更优雅的方式做到这一点,但至少它是有效的:

select d.fullname,count(h.isn) 
from subdept d
left join subhuman h on h.deptisn=d.isn 
left join subject j on h.isn=j.isn 
left join emilitary em on em.emplisn=j.isn 
where h.sex = 'М' and em.milsign='Y' 
group by rollup (d.fullname) 
having count(h.isn)>0
union
select g.fullname, 0 from 
subdept g
where d.fullname not in(
    select tt1.fullname from (
        select d.fullname,count(h.isn) 
        from subdept d 
        left join subhuman h on h.deptisn=d.isn 
        left join subject j on h.isn=j.isn 
        left join emilitary em on em.emplisn=j.isn 
    where h.sex = 'М' and em.milsign='Y' 
    group by rollup (d.fullname) 
    having count(h.isn)>0) tt1
)

0
投票

where h.sex='m' and and em.milsign='Y'导致“隐式内连接”,如果左连接表中没有匹配的行,则会隐藏某些部门。改为加入条件而不是:

SELECT
      d.fullname
    , COUNT(h.isn)
FROM subdept d
LEFT JOIN subhuman h ON d.isn = h.deptisn AND h.sex = 'М'
LEFT JOIN subject j ON h.isn = j.isn
LEFT JOIN emilitary em ON j.isn = em.emplisn AND em.milsign = 'Y'
GROUP BY ROLLUP
      d.fullname
;

或者,您可以修改where子句以允许通过连接条件重新调整NULL:

SELECT
      d.fullname
    , COUNT(h.isn)
FROM subdept d
LEFT JOIN subhuman h ON d.isn = h.deptisn
LEFT JOIN subject j ON h.isn = j.isn
LEFT JOIN emilitary em ON j.isn = em.emplisn
WHERE ( h.sex = 'М'  OR h.sex IS NULL)
AND (em.milsign = 'Y' OR em.milsign IS NULL)
GROUP BY ROLLUP
      d.fullname
;

或者,在子查询中应用所需的过滤:

SELECT
      d.fullname
    , COUNT(h.isn)
FROM subdept d
LEFT JOIN (
      SELECT
            h.isn
          , h.deptisn
      FROM subhuman h
      LEFT JOIN subject j ON h.isn = j.isn
      LEFT JOIN emilitary em ON j.isn = em.emplisn
      WHERE h.sex = 'М'
      AND em.milsign = 'Y'
) h ON d.isn = h.deptisn
GROUP BY ROLLUP
      d.fullname
;
© www.soinside.com 2019 - 2024. All rights reserved.