2017-02-09 8 views
-2

私は、UNIONを使用して同じフィールドで2つのクエリをフェッチしようとしています。私が必要とするのは、重複した行が削除されるということですが、重複した行は削除されません。両方のクエリには、Country、Idcustomer、Type、およびCostが含まれています。私は例を示して下:フェッチ後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は、両方のクエリーにある場合、最初のものをpriorizeをということです。私はUNIONが重複した値を取り除くので、それを解決しようとしていたと思った。

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は" ...いいえ。それは「組合」の仕組みではありません。まったく。 –

+0

SELECT A. * FROM qry_cost WHERE type = 1 SELECT qry_cost.Idcustomer、MIN(qry_cost.cost)AS cost_min qry_cost WHERE type = 1 GROUP BY qry_cost.Idcustomer )AS B WHERE A.Idcustomer = B.IdcustomerとA.cost = B.cost UNION SELECT A. * FROM(SELECT国、Idcustomer、タイプ、コスト、qry_cost where type = 2)AS A、(SELECT qry_cost。 IDCustomer、MIN(qry_cost.cost)AS cost_min 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さん、ありがとうございました。世界の国々と同じくらい多くの国があります.John Bones、WHERE句の例を教えてください。可能性がありますか? –

0

私は、あなたが "外側" のクエリに維持するレコードを決定する必要があると思います。それで、私は新しい層にすべてを包み込むことを意味します。次のようなもの:

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

問題は、最初のテーブルからすべての行を取得し、テーブル2のすべての重複していない値(IdCustomerが同じ場合は行が複製されます)と結合する必要があることです。LEFT JOINテーブル1 +右ジョイントテーブル - テーブルジョイントテーブル1。 IdCustomer = Table2.IdCustomer ..その愚かな「コード」は申し訳ありませんが、SQLで実装する方法はわかりません。 –