zoukankan      html  css  js  c++  java
  • 3.7 检测两个表中是否有相同的数据

    问题:要知道两个表或视图中是否有相同的数据(基数和值)。考虑这个视图:

    create view V
    as 
        select * from emp where deptno !=10
            union all
        select * from emp where ename = 'WARD'   

    现要检测这个视图与表emp中的数据是否完全相同。员工“WARD”行重复,说明解决方案不仅要显示不同行,还要显示重复行。

    解决方案:
    1.首先,查找出表emp中存在而试图v中没有的行
    2.然后合并(union all)在视图V中存在,而在表emp中没有的行。

    select * from 
        (
            select e.empno,e.ename,e.job,e.mgr,e.hiredate,
                e.sal,e.comm,e.deptno,count(*)  as cnt
            from emp e
            group by empno,ename,job,mgr,hiredate,sal,comm,deptno
        ) e
    where not exists
        (
        select null from
            (
                select v.empno,v.ename,v.job,v.mgr,v.hiredate,v.sal,v.comm,v.deptno
                    count (*) as cnt
                from v
                group by empno,ename,job,mgr,hiredate,sal,comm,deptno
            )v
            where v.empno = e.empno
                and v.ename = e.ename
                and v.job = e.job
                and v.mgr = e.mgr
                and v.hiredate = e.hiredate
                and v.sal = e.sal
                and v.deptno = e.deptno
                and v.cnt = e.cnt
                and coalesce(v.comm.0) = coalesce(e.comm,0)
        )
        union all

        select * from 
        (
            select e.empno ,e.ename,e.ename,e.job,e.mgr,e.hiredate,e.sal ,e.comm,e.deptno,count(*) as cnt
            from v 
            group by empno,ename,job,mgr,hiredate,sal,comm,deptno
        )v
        where not exists (
            select null from (
                select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,e.deptno,count(*) as cnt
                from emp e
                group by empno,ename,job,mgr,hiredate,sal,comm,deptno
                )e
            where v.empno = e.empno
                and v.ename = e.ename
                and v.job = e.job
                and v.mgr = e.mgr
                and v.hiredate = e.hiredate
                and v.sal = e.sal
                and v.deptno = e.deptno
                and v.cnt = e.cnt
                and coalesce(v.comm.0) = coalesce(e.comm,0)

        );

  • 相关阅读:
    耐性4/21
    吃枸杞上火4/11
    metro style app 的程序构成 以c# 为例 GIS
    ListView 和 GridView ————转 GIS
    最大程度地利用像素,适应视图状态的变更___转 GIS
    metro style 里面的控件一览 以 Windows.UI.Xaml.Controls空间 GIS
    Windows 8里的标准化输入 GIS
    漫游应用程序数据 GIS
    FlipView 知识准备 GIS
    Data Binding Notifications绑定通知 GIS
  • 原文地址:https://www.cnblogs.com/l10n/p/7518471.html
Copyright © 2011-2022 走看看