0

配送先住所情報と請求先住所情報の両方を含む巨大なテーブルがあります。次の2つの別々のテーブルで、一意の配送先住所と請求先住所を取得できます。

SELECT DISTINCT ShipToName, ShipToAddress1, ShipToAddress2, ShipToAddress3, ShipToCity, ShipToZipCode
FROM Orders
ORDER BY Orders.ShipToName

SELECT DISTINCT BillToName, BillToAddress1, BillToAddress2, BillToAddress3, BillToCity, BillToZipCode
FROM Orders
ORDER BY Orders.BillToName

どうすれば2つの明確な交差点を取得できますか?構文がわかりません。

4

3 に答える 3

2

このようなもの?

SELECT DISTINCT
    toname, addr1,  addr2,  addr3,  city,   zip
FROM
    (SELECT DISTINCT 
        ShipToName AS toName, 
        ShipToAddress1 AS addr1, 
        ShipToAddress2 AS addr2, 
        ShipToAddress3 AS addr3, 
        ShipToCity AS city, 
        ShipToZipCode AS zip
    FROM 
        Orders

    UNION ALL

    SELECT DISTINCT 
    BillToName AS toName, 
    BillToAddress1 AS addr1, 
    BillToAddress2 AS addr2, 
    BillToAddress3 AS addr3, 
    BillToCity AS city, 
    BillToZipCode AS zip
    FROM 
        Orders) o
ORDER BY ToName
于 2013-01-10T19:33:46.730 に答える
2

あなたは「交差点」と言いますが、あなたはユニオンの答えを受け入れたので、あなたはただ欲しいと思いますUNION DISTINCT。派生テーブルと3つのテーブルは必要ありませんDISTINCT。シンプルなものを使用できます。

SELECT 
    ShipToName     AS Name, 
    ShipToAddress1 AS Address1, 
    ShipToAddress2 AS Address2, 
    ShipToAddress3 AS Address3, 
    ShipToCity     AS City, 
    ShipToZipCode  AS ZipCode
FROM 
    Orders

UNION                               --- UNION means UNION DISTINCT

SELECT  
    BillToName 
    BillToAddress1, 
    BillToAddress2,
    BillToAddress3,
    BillToCity,
    BillToZipCode
FROM 
    Orders 

ORDER BY
    Name ;
于 2013-01-10T20:00:58.043 に答える
1

すべてのフィールドで両方のセットを結合できます。これにより、一致するレコードが返されます。

SELECT *
FROM Orders o1
INNER JOIN Orders o2
    ON o1.ShipToName = o2.BillToName
    AND o1.ShipToAddress1 = o2.BillToAddress1
    AND o1.ShipToAddress2 = o2.BillToAddress2
    AND o1.ShipToAddress3 = o2.BillToAddress3 
    AND o1.ShipToCity = o2.BillToCity
    AND o1.ShipToZipCode = o2.BillToZipCode

または、INTERSECTを使用できるはずです。

SELECT ShipToName, ShipToAddress1, ShipToAddress2, ShipToAddress3, ShipToCity, ShipToZipCode
FROM Orders
INTERSECT
SELECT BillToName, BillToAddress1, BillToAddress2, BillToAddress3, BillToCity, BillToZipCode
FROM Orders

または、UNIONクエリ(UNION2つのデータセット間の重複を削除する)もあります。

SELECT ShipToName, ShipToAddress1, ShipToAddress2, ShipToAddress3, ShipToCity, ShipToZipCode
FROM Orders
UNION 
SELECT BillToName, BillToAddress1, BillToAddress2, BillToAddress3, BillToCity, BillToZipCode
FROM Orders
于 2013-01-10T19:20:47.717 に答える