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

时间:2017-12-15 11:35:38

标签: sql

如果两个表中有相同的部门名称但计数不同,如何删除重复项。如果有相同的部门,程序不应该使用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

目前的结果是:

in this screenshot

预期结果是:

expected result

5 个答案:

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

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

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

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

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