Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.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 - Fatal编程技术网

Sql 如何消除左联接中的重复记录?

Sql 如何消除左联接中的重复记录?,sql,postgresql,Sql,Postgresql,我想创建一个名为saledetailfortax的视图,它将由13列组成。它们是saledetaildate、saledetailtime、shopid、productid、unitid、expdate、批次号、mrp、总价、数量、looseqty、priceperunit和taxid 我的问题是: CREATE OR REPLACE VIEW saledetailfortax2 AS select sd.saledetaildate, sd.saledetailtime, sd.sh

我想创建一个名为saledetailfortax的视图,它将由13列组成。它们是saledetaildate、saledetailtime、shopid、productid、unitid、expdate、批次号、mrp、总价、数量、looseqty、priceperunit和taxid

我的问题是:

  CREATE OR REPLACE VIEW saledetailfortax2 AS 
  select sd.saledetaildate, sd.saledetailtime, sd.shopid, sd.productid,
        sd.unitid, sd.expdate, sd.batchno, sd.mrp, sd.totalprice, sd.qty, 
        sd.looseqty, sd.unitprice as priceperunit, ord.taxid 
  from saledetail sd
  left JOIN distinctPriceperunit  ord
      ON  sd.productid = ord.productid
      AND sd.expdate = ord.expdate
      AND sd.batchno = ord.batchno
      AND sd.mrp = ord.mrp
      AND sd.unitprice = ord.priceperunit  
  where sd.saledetaildate >= '2016-04-01'
  order by  sd.saledetaildate , sd.saledetailtime 
问题是当有两个taxid具有相同的productid、expdate、batchno、, mrp和单价则有两条记录是同一件事

假设saledetail表中的一条记录包含相同的productid、expdate、batchno、mrp和unitprice,但productid在distinctPriceperunit表中有两个taxid,那么当发生左连接时,它会显示两条记录。但是只有一个记录显示两辆出租车中的任何一辆

那么如何消除重复记录呢

View distinctpriceperunit(所有都是不同的值):

选择不同的od.productid、od.unitid、od.priceperunit、od.expdate、od.mrp、od.batchno、od.taxid 从orderreceivedetail od 按od.productid、od.unitid、od.priceperunit、od.expdate、od.mrp、od.batchno、od.taxid订购

销售明细表 ( saledetailid字符变化(20)不为空, 销售详情日期, saledetailtime时间戳,不带时区, shopid整数, productid整数, 数量整数, 单位整数, 单价数字, discperc数字, discamt数字, expdate日期, mrp数字, 日期, batchno字符变化(50), 总价格数字, isreturned布尔值, 用户标识整数, saleid字符变化(20), isloose boolean, 数量整数, 约束saledetail_pkey主键(saledetailid) )使用
最高(单价)


这将删除重复项。

您可以按以下列使用分组productid、expdate、batchno、mrp和unitprice。

按分组解决方案:

CREATE OR REPLACE VIEW saledetailfortax2 AS 
select sd.saledetaildate, sd.saledetailtime, sd.shopid, sd.productid,
       sd.unitid, sd.expdate, sd.batchno, sd.mrp, sd.totalprice, sd.qty, 
       sd.looseqty, sd.unitprice as priceperunit, MAX(ord.taxid) 
from saledetail sd
left JOIN distinctPriceperunit ord
    ON  sd.productid = ord.productid
    AND sd.expdate = ord.expdate
    AND sd.batchno = ord.batchno
    AND sd.mrp = ord.mrp
    AND sd.unitprice = ord.priceperunit  
where sd.saledetaildate >= '2016-04-01'
group by sd.saledetaildate, sd.saledetailtime, sd.shopid, sd.productid,
         sd.unitid, sd.expdate, sd.batchno, sd.mrp, sd.totalprice, sd.qty, 
         sd.looseqty, sd.unitprice
order by sd.saledetaildate, sd.saledetailtime
CREATE OR REPLACE VIEW saledetailfortax2 AS 
select sd.saledetaildate, sd.saledetailtime, sd.shopid, sd.productid,
       sd.unitid, sd.expdate, sd.batchno, sd.mrp, sd.totalprice, sd.qty, 
       sd.looseqty, sd.unitprice as priceperunit,
       (select max(taxid) from distinctPriceperunit ord
        WHERE sd.productid = ord.productid
          AND sd.expdate = ord.expdate
          AND sd.batchno = ord.batchno
          AND sd.mrp = ord.mrp
          AND sd.unitprice = ord.priceperunit)
from saledetail sd
where sd.saledetaildate >= '2016-04-01'
order by sd.saledetaildate, sd.saledetailtime 
相关子查询解决方案:

CREATE OR REPLACE VIEW saledetailfortax2 AS 
select sd.saledetaildate, sd.saledetailtime, sd.shopid, sd.productid,
       sd.unitid, sd.expdate, sd.batchno, sd.mrp, sd.totalprice, sd.qty, 
       sd.looseqty, sd.unitprice as priceperunit, MAX(ord.taxid) 
from saledetail sd
left JOIN distinctPriceperunit ord
    ON  sd.productid = ord.productid
    AND sd.expdate = ord.expdate
    AND sd.batchno = ord.batchno
    AND sd.mrp = ord.mrp
    AND sd.unitprice = ord.priceperunit  
where sd.saledetaildate >= '2016-04-01'
group by sd.saledetaildate, sd.saledetailtime, sd.shopid, sd.productid,
         sd.unitid, sd.expdate, sd.batchno, sd.mrp, sd.totalprice, sd.qty, 
         sd.looseqty, sd.unitprice
order by sd.saledetaildate, sd.saledetailtime
CREATE OR REPLACE VIEW saledetailfortax2 AS 
select sd.saledetaildate, sd.saledetailtime, sd.shopid, sd.productid,
       sd.unitid, sd.expdate, sd.batchno, sd.mrp, sd.totalprice, sd.qty, 
       sd.looseqty, sd.unitprice as priceperunit,
       (select max(taxid) from distinctPriceperunit ord
        WHERE sd.productid = ord.productid
          AND sd.expdate = ord.expdate
          AND sd.batchno = ord.batchno
          AND sd.mrp = ord.mrp
          AND sd.unitprice = ord.priceperunit)
from saledetail sd
where sd.saledetaildate >= '2016-04-01'
order by sd.saledetaildate, sd.saledetailtime 

请你把这个查询格式化,使它更具可读性好吗。此外,您能否提供表的定义。选择列表中的分组依据或相关子查询。@Sagi,键入时花了一些时间。我收回这一点:)它为我做到了,相关子查询解决方案。非常感谢。jarlh@jarlh我有一个疑问,在我的想法中,当我们离开一个表与另一个表连接时,然后离开一个表并用记录显示满足哪个条件。但为什么这里的记录比左表多呢?@Avinash,左连接即使在不满足条件的情况下也会返回左表中的行。此关联子查询返回的行数永远不会超过左联接版本。它只能返回更少的行!