2016-11-16 76 views
0

enter code here我需要按類型配對車輛,然後再按燃料消耗配對。燃料消耗可能不匹配,並且在那種情況下選擇最接近的。同一輛車不能用於配對多次。例如數據我談論的類型低於返回正在搜索的值的最接近的值

create table #table1 
(
vehicleid varchar(2), 
typed varchar(5), 
fuelconsumption int 
) 


create table #table2 
(
vehicleid varchar(2), 
typed varchar(5), 
fuelconsumption int 
) 


INSERT INTO #table1 VALUES('x1','car',5); 
INSERT INTO #table1 VALUES('x2','car',4); 
INSERT INTO #table1 VALUES('x3','car',8); 

INSERT INTO #table2 VALUES('b1','car',7); 
INSERT INTO #table2 VALUES('b2','car',8); 
INSERT INTO #table2 VALUES('b3','car',9); 
INSERT INTO #table2 VALUES('b4','car',10); 
INSERT INTO #table2 VALUES('b5','car',11); 
INSERT INTO #table2 VALUES('b6','truck',15); 
INSERT INTO #table2 VALUES('b7','truck',4); 

將返回輸出是這樣的: enter image description here

+2

嘛,你嘗試過什麼? – OldProgrammer

+3

標記您正在使用的dbms。 (答案可能是產品特定的。) – jarlh

+0

更新了代碼和樣本結果 – user2772056

回答

0

雖然這可能不是一個完整的答案(仍在研究細節),如果在開始使用此您將獲得最匹配的車輛ID的升序排列的列表:

SELECT baseID = ibase.vehicleid, 
      matchID = im.vehicleid, 
      rowNum = ROW_NUMBER() 
        OVER (PARTITION BY ibase.vehicleid 
          ORDER BY ABS(ibase.fuelconsumption - im.fuelconsumption)) 
     FROM #table1 ibase 
      INNER JOIN #table2 im ON ibase.typed = im.typed 

從這裏你應該能夠遍歷拿起「第一」的「b」車的情況下,

完整的答案(注意:我討厭遊標,但這個工程):

DROP TABLE #results 
CREATE TABLE #results (vehicleid1 VARCHAR(2), vehicleid2 VARCHAR(2), diff INT, rownum INT) 

INSERT INTO #results (vehicleid1, vehicleid2, diff, rownum) 
SELECT baseID = ibase.vehicleid, 
     matchID = im.vehicleid, 
     diff = ABS(ibase.fuelconsumption - im.fuelconsumption), 
     rowNum = ROW_NUMBER() 
        OVER (
         PARTITION BY ibase.vehicleid 
         ORDER BY ABS(ibase.fuelconsumption - im.fuelconsumption)) 
    FROM #table1 ibase 
     INNER JOIN #table2 im ON ibase.typed = im.typed 

-- The cursor ensures we get the real best match so we do not accidentally 
-- assign a best match which is a _better_ match for another vehicle 
DECLARE CUR_ CURSOR FOR SELECT vehicleid1 FROM #results ORDER BY diff 
DECLARE @id VARCHAR(2) 

OPEN CUR_ 
FETCH CUR_ INTO @id 
WHILE @@FETCH_STATUS = 0 
BEGIN 

    -- Remove all matches other than the BEST match 
    DELETE r 
     FROM #results r 
    WHERE r.vehicleid1 = @id 
     AND r.rownum <> (SELECT MIN(ir.rownum) FROM #results ir WHERE ir.vehicleid1 = @id) 

    -- Remove all other matches which use the remaining matched vehicle 
    DELETE r 
     FROM #results r 
    WHERE r.vehicleid1 <> @id 
     AND r.vehicleid2 = (SELECT ir.vehicleid2 FROM #results ir where ir.vehicleid1 = @id) 

    FETCH CUR_ INTO @id 
END 
CLOSE CUR_ 
DEALLOCATE CUR_ 

SELECT * FROM #results 

非光標的解決方案:

DROP TABLE #results 
CREATE TABLE #results (vehicleid1 VARCHAR(2), vehicleid2 VARCHAR(2), diff INT, rownum INT) 

INSERT INTO #results (vehicleid1, vehicleid2, diff, rownum) 
SELECT baseID = ibase.vehicleid, 
     matchID = im.vehicleid, 
     diff = ABS(ibase.fuelconsumption - im.fuelconsumption), 
     rowNum = ROW_NUMBER() 
        OVER (
         PARTITION BY ibase.vehicleid 
         ORDER BY ABS(ibase.fuelconsumption - im.fuelconsumption)) 
    FROM #table1 ibase 
     INNER JOIN #table2 im ON ibase.typed = im.typed 

DECLARE @id VARCHAR(2) 
WHILE EXISTS(SELECT vehicleid1 FROM #results GROUP BY vehicleid1 HAVING COUNT(*) > 1) 
BEGIN 

    SELECT TOP 1 
      @id = r.vehicleid1 
     FROM #results r 
      INNER JOIN (SELECT i.vehicleid1 FROM #results i GROUP BY i.vehicleid1 HAVING COUNT(*) > 1) i 
       ON r.vehicleid1 = i.vehicleid1 
    ORDER BY r.diff 

    -- Remove all matches other than the BEST match 
    DELETE r 
     FROM #results r 
    WHERE r.vehicleid1 = @id 
     AND r.rownum <> (SELECT MIN(ir.rownum) FROM #results ir WHERE ir.vehicleid1 = @id) 

    -- Remove all other matches which use the remaining matched vehicle 
    DELETE r 
     FROM #results r 
    WHERE r.vehicleid1 <> @id 
     AND r.vehicleid2 = (SELECT ir.vehicleid2 FROM #results ir where ir.vehicleid1 = @id) 

END 

SELECT * FROM #results 
+0

我明白你在做什麼,但你仍然有問題在表2中選擇同一輛車可能是因爲x1和x2都離b1最近,並且都將其排列爲數字1. – user2772056

+0

我正在使用不支持遊標的pdw。我想我會用一段時間? – user2772056

+0

很多你試圖用這個語句做的事情在pdw中是不可能的。例如。 from子句不允許在刪除語句中 – user2772056