2016-07-04 60 views
1

我的表Ce_Data有一欄「Fut_ID」如何指定列從結果選擇加入

爲什麼我得到第一行的「曖昧」的錯誤? TX。「Fut_ID」無法正常工作或

SELECT "Fut_ID" 
FROM (
SELECT * 
    FROM "Ce_Data" AS M 
    JOIN (SELECT "Fut_ID", MAX("Date") AS "Most_Recent" 
      FROM "Ce_Data" 
     GROUP BY "Fut_ID" 
     ) AS R 
    ON R."Fut_ID" = M."Fut_ID" AND R."Most_Recent" = M."Date" 
) AS TX 
+1

請發佈您收到的確切錯誤消息。這些細節對您來說可能並不意味着什麼,但對於某些人的幫助它們可能會有用。 – IMSoP

回答

1

外部查詢主從內部子查詢Fut_ID。然而,有兩個不同源可供選擇Fut_ID:表Ce_Data(化名M派生表的別名R。您可以解決該問題通過使用別名字段的派生表中:

SELECT "Fut_ID" 
FROM (
SELECT * 
    FROM "Ce_Data" AS M 
    JOIN (SELECT "Fut_ID" AS "Fut_ID2", MAX("Date") AS "Most_Recent" 
      FROM "Ce_Data" 
     GROUP BY "Fut_ID" 
     ) AS R 
    ON R."Fut_ID2" = M."Fut_ID" AND R."Most_Recent" = M."Date" 
) AS TX 
1

你得到一個模棱兩可的錯誤,因爲Fut_ID是兩個表中,所以它的子查詢是不明確的(在*得到所有列名,甚至重複)。剛剛從第二個表中明確列出的列:(我加了TX.*,否則查詢處理沒有意義)

SELECT TX.* 
FROM (SELECT M.* 
     FROM "Ce_Data" M JOIN 
      (SELECT "Fut_ID", MAX("Date") AS "Most_Recent" 
      FROM "Ce_Data" 
      GROUP BY "Fut_ID" 
      ) R 
      ON R."Fut_ID" = M."Fut_ID" AND R."Most_Recent" = M."Date" 
    ) TX; 

或者,更好的是,使用窗口功能:

SELECT TX.* 
FROM (SELECT M.*, MAX("Date") OVER (PARTITION BY "Fut_ID") as "Most_Recent" 
     FROM "Ce_Data" M 
    ) M 
ON "Most_Recent" = "Date"