2017-02-09 80 views
-2

我試圖使用UNION獲取兩個具有相同字段的查詢。我需要的是重複的行被刪除,但它們不是。這兩個查詢都包含國家,Idcustomer,類型和成本。下面我舉一個例子:使用UNION刪除重複的行

Country | Idcustomer | Type | Cost        
Brazil  123   1  3,2 
Brazil  212   1  4,1 


Country | Idcustomer | Type | Cost 
Brazil  123   2  5,5 
Brazil  212   2  4,3 
Brazil  543   2  4,2 

提取後,我需要的是,如果Idcustomer是在兩個查詢,優先於第一個。我認爲聯盟會解決這個問題,因爲它會刪除重複的值,但不是。

Country | Idcustomer | Type | Cost 
Brazil  123   1  3,2 
Brazil  212   1  4,1 
Brazil  543   2  4,2 

任何想法?非常感謝!

SELECT A.* 
FROM (
    SELECT Country, Idcustomer, type, cost 
    FROM qry_cost 
    WHERE type = 1 
) AS A, 
    (
    SELECT qry_cost.Idcustomer, MIN(qry_cost.cost) AS cost_min 
    FROM qry_cost 
    WHERE type = 1 
    GROUP BY qry_cost.Idcustomer 
) AS B 
WHERE A.Idcustomer = B.Idcustomer 
    AND A.cost = B.cost 
UNION 
SELECT A.* 
FROM (
    SELECT Country, Idcustomer, type, cost 
    FROM qry_cost 
    WHERE type = 2 
) AS A, 
    (
    SELECT qry_cost.Idcustomer, MIN(qry_cost.cost) AS cost_min 
    FROM qry_cost 
    WHERE type = 2 
    GROUP BY qry_cost.Idcustomer 
) AS B 
WHERE A.Idcustomer = B.Idcustomer 
    AND A.cost = B.cost; 
+1

你能發佈您的查詢嗎? –

+1

您的數據不包含重複的行。 「提取後,我需要的是,如果Idcustomer同時處理這兩個問題,請優先考慮第一個問題,我認爲UNION正在解決這個問題」......不。這不是'工會'的工作方式。完全一樣。 – 2017-02-09 17:52:59

+0

SELECT A. * FROM(SELECT Country,Idcustomer,type,cost FROM qry_cost WHERE type = 1)AS A,(SELECT qry_cost.Idcustomer,MIN(qry_cost.cost)AS cost_min FROM qry_cost WHERE type = 1 GROUP BY qry_cost.Idcustomer )AS B WHERE A.Idcustomer = B.Idcustomer AND A.cost = B.cost UNION SELECT A. * FROM(SELECT Country,Idcustomer,type,cost FROM qry_cost WHERE type = 2)AS A,(SELECT qry_cost。 Idcustomer,MIN(qry_cost.cost)AS cost_min FROM qry_cost WHERE type = 2 GROUP BY qry_cost.Idcustomer)AS B WHERE A.Idcustomer = B.Idcustomer AND A.cost = B.cost; –

回答

0
 create table #Country1 
(
    country nvarchar(50), 
    idcustomer int, 
    type int, 
    cost decimal(19,6) 

) 
    create table #Country2 
    (
     country nvarchar(50), 
     idcustomer int, 
     type int, 
     cost decimal(19,6) 

    ) 
    create table #Country3 
    (
     country nvarchar(50), 
     idcustomer int, 
     type int, 
     cost decimal(19,6) 

    ) 


    insert into #Country1(country,idcustomer,type,cost) Values('Brazil', 123,1,3.2) 

    insert into #Country2(country,idcustomer,type,cost) VALUEs ('Brazil', 212,1,4.1) 

    insert into #Country3 (country,idcustomer,type,cost)VALUES ('Brazil', 123,2,5.5) 
    insert into #Country3(country,idcustomer,type,cost)VALUEs ('Brazil', 212,2,4.3) 
    insert into #Country3 (country,idcustomer,type,cost)VALUES ('Brazil', 543,2,4.2) 
-- YOUR ANSWER IS AS FOLLOWS  
    select * from #Country1 
    UNION 
    select * from #Country2 
    UNION 
    select * from #Country3 where cost= 4.2 


-- DROP IT  
    DROP TABLE #Country1 
    DROP TABLE #Country2 
    DROP TABLE #Country3 


--RESULT if you run ABOVE QUERY 

Reult

+0

您只需要三行查詢UNION和帶有where子句的最後一個選擇查詢。希望它有效。我已經創建了臨時表來證明結果的工作原理,因爲我已附加結果JPG文件 –

+0

,感謝Udeep Dhungel,但是我擁有與世界各國一樣多的國家.. Johnny Bones,請您舉例說明WHERE子句可以包含? –

0

我認爲你需要確定哪些記錄保持在一個 「外」 查詢。所以,我的意思是將它全部包裝在一個新層中。例如:

SELECT * 
FROM (your entire query above) 
WHERE (your logic as to which records to keep) 
+0

事情是我需要查詢從第一個表中獲取所有行,並從表2中加入它們並且沒有任何重複值(如果IdCustomer是相同的,則重複一行)。這將是類似於LEFT JOIN表1 + RIGHT JOIN表 - INNER JOIN WHERE表1。 IdCustomer = Table2.IdCustomer ..抱歉那個愚蠢的'代碼',但我不知道如何用SQL實現它。 –