2015-11-28 30 views
0

我想从这个SQL中删除重复的行,并且两次产品代码都通过了。我想从这个SQL的公共数据。我在PostgreSQL数据库中运行这个SQL。如何从sql中删除重复的数据

SQL查询是:

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 
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次。我不想要重复的数据。

+0

地狱。使用适当的案例等这太复杂了吗? – frlan

+1

不,不可能出现'select distinct'结果重复的行。你可以请出示一些数据样本。 – Houari

+1

无关,但:请习惯显式的'JOIN's,而不是使用'where'子句中的旧的隐式连接 –

回答

0

试试这个查询,它会帮助你。

​​3210
-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 
+1

代码只有答案质量很低,因为至少应该有一两句话向读者解释你正在回答什么问题,以及该代码以何种方式回答这个问题。见http://meta.stackexchange.com/questions/148272/is-there-any-benefit-to-allowing-code-only-answers-while-blocking-code-only-ques –