Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是什么导致SQL中出现重复行?_Sql_Postgresql_Duplicates - Fatal编程技术网

是什么导致SQL中出现重复行?

是什么导致SQL中出现重复行?,sql,postgresql,duplicates,Sql,Postgresql,Duplicates,我正在运行一个(很长的)查询,有大量重复的行。是什么条款导致了这一点,什么可以解决它 我已经尝试在select子句中的一列上放置一个独立的 Select Distinct location ,item_no ,order_no from (select * from stockA a join r_except re using (item_no) where re.location =

我正在运行一个(很长的)查询,有大量重复的行。是什么条款导致了这一点,什么可以解决它

我已经尝试在select子句中的一列上放置一个
独立的

    Select Distinct location
        ,item_no
        ,order_no
    from (select *
          from stockA a
          join r_except re using (item_no)
          where re.location = 'locationA'

          UNION

          select * 
          from stockB b
          join r_except re using (item_no) 
          where re.location = 'locationB'
          ) ss

    left join r_except re 
    on ss.item_no = re.item_no

    left join (select ih.invoice_no
                    ,iih.item_no
                    ,sum(shipped * case) tot_shipped
               from invoice_hist ih inner join invoice_item_hist iih
                           on ih.invoice_no = iih.invoice_no
               where ih.invoice_date between (date 'now' - interval '12 months') and date 'now'
                       group by ih.invoice_no, iih.item_no) as ihsum
               on (ss.item_no = ihsum.item_no)


   join (select us.uitem
                  ,sum(ss.on_hand) uoh
                  ,sum(ss.vendor_order) vendor_order
                  ,max(case when ss.last_sale_date = '12/31/9999' then null else ss.last_sale_date end) last_sale_date                        
                  ,max(re.level) r_level
         from () ss inner join universal_no us
         on (ss.uitem = uoh.uitem)

left join (select ss.uitem
                    ,max(po.order_no) order_no
                    ,max(po.order_date) order_date
         from () ss inner join poitem poi
                                on ss.item_no = poi.item_no
                    inner join po
                          on po.order_no = poi.order_no
                    where poi.release_qty != poi.case_qty * poi.order_qty
                    group by ss.uitem
         ) po
         on ss.uitem = po.uitem

这是经过删节的,其中一些可能不在select子句中,但这是因为它太长了。

您可以查看以下两个方面:

您是否正在连接具有一对多关系的表?这通常会导致多次返回相同的行。如果有多个子行连接到父行


另一个更明显的原因可能是您的数据库中实际上有重复的数据。

您可能不理解所涉及的关系的重要性。或者您没有向数据库询问正确的问题。如果不知道您要完成什么,也不知道您的模式,就不可能回答这个问题。如果您向我们显示模式和查询。示例数据和预期/实际结果也很有帮助。好的,就像我说的,查询很长,比如2000行代码,所以我不想在不知道重复的原因的情况下大量使用它。如果您至少发布from子句,我们可能会给您一些错误的地方。好的,我将尝试简化它。如果你愿意的话,我可以添加select子句的其余部分