如何从sql中删除重复数据

时间:2015-11-28 09:03:42

标签: sql postgresql

我正在尝试从此SQL中删除重复行,并且正在传递两次产品代码。我想从这个SQL中获得一个公共数据。我在PostgreSQL数据库中运行这个SQL。





SQL查询是:




  SELECT DISTINCT& #xA; csi.style_item_id AS id,
 csi.item_code AS item_code,
 csi.quantity AS quantity,
 std_unit.stdum_name AS standard_unit,
 csi.total_cost AS total_cost,
 cim.item_name AS item_name,
 pid.product_code AS product_code
 FROM cm_items_master cim,
 cm_style_items csi,
 stdunit_master std_unit,
 cm_style_ccode_master sccm,
 cm_style_designer sd,
 cm_designer_master dm,
 cm_production_info_details pid
 WHERE pid.style_ccode_id = sccm.style_ccode_id
 AND csi.cm_item_id = cim.item_id
 AND csi.std_unit_id = std_unit.id
 AND csi.style_ccode_id = sccm.style_ccode_id
 AND sccm.style_ccode_id = sd.style_ccode_id
 AND sd.designer_id = dm.designer_id
 AND pid.product_code IN(getSplitedPdtCode(productCode))
 AND csi.is_delete ='N'
 AND cim.is_delete ='N'
 AND std_unit.is_delete ='N'
  




此SQL返回同一行4次。我不想要重复的数据。




3 个答案:

答案 0 :(得分:0)

尝试此查询,它会对您有所帮助。

SELECT DISTINCT 
  csi.style_item_id AS id,  
  csi.item_code AS item_code,  
  csi.quantity AS quantity,  
  std_unit.stdum_name AS standard_unit,  
  csi.total_cost AS total_cost,  
  cim.item_name AS item_name,  
  pid.product_code AS product_code  
FROM (cm_items_master cim,  
  cm_style_items csi,  
  stdunit_master std_unit,  
  cm_style_ccode_master sccm,  
  cm_style_designer sd,  
  cm_designer_master dm,  
  cm_production_info_details pid) A
WHERE pid.style_ccode_id = sccm.style_ccode_id  
  AND csi.cm_item_id  = cim.item_id  
  AND csi.std_unit_id  = std_unit.id  
  AND csi.style_ccode_id   = sccm.style_ccode_id  
  AND sccm.style_ccode_id  = sd.style_ccode_id  
  AND sd.designer_id  = dm.designer_id  
  AND pid.product_code     IN (  getSplitedPdtCode(productCode) )  
  AND csi.is_delete        = 'N'
  AND cim.is_delete        = 'N'  
  AND std_unit.is_delete   = 'N'
  AND CTID in (select max(CTID) from A group by id)

答案 1 :(得分:0)

在SQL中删除重复项的最佳方法

List<Point3D> Positions= PositionsString.Value.Split(' ')
                                 .Select(Point3D.Parse)
                                 .ToList();

将以上结果存储在临时表中,并从临时表中选择数据,其中row = 1,这将为您提供by by子句中列出列的唯一行。

答案 2 :(得分:-1)

With CTE_Dup as emp
    (Select *, row_number() over(partition by Empid order by Empid) as rownumber 
    from employee)
Delete from emp
where rownuber > 1