曼尼,你將無法加入這兩個沒有共同領域的兩個查詢。如果你有一個共同的領域,那麼你可以建立每個臨時表並加入第三個查詢。它看起來像FoodTable中的'Food ID'(來自第一個查詢)。如果存在於SellerTable上,那麼您可以加入這兩個(並且加入MarketTable以從該表中獲取行)。
這將是這個樣子:
create temp table food_nums_temp as
SELECT TOP (150) FoodID, COUNT(*) AS NumberOfFood
FROM FoodTable
WHERE FoodID IS NOT NULL
AND FoodDate >= '2015-10-01'
GROUP BY FoodID
ORDER BY NumberOfFood DESC
;
Create temp table seller_temp as
SELECT FoodSellerID, FoodID,
Market1,
SellerLastName,
SellerFirstName,
PrimaryAddress1,
PrimaryAddress2,
PrimaryCity,
PrimaryState,
PrimaryZip
FROM SellerTable, MarketTable
WHERE Market1= MarketTable.MarketID
;
select a.*, b.SellerLastName
, b.SellerFirstName, b.PrimaryAddress1, b.PrimaryAddress2
, b.PrimaryCity, b.PrimaryState, b.PrimaryZip
from food_nums_temp a
inner join seller_temp b on a.FoodID = b.FoodID
編輯:明知完全DB訪問(創建臨時表)是不允許的,允許使用「與」(又名公共表表達式或CTE),以現階段的數據在我們所需的查詢之前:
with
food_nums_temp as (
SELECT TOP (150) FoodID, COUNT(*) AS NumberOfFood
FROM FoodTable
WHERE FoodID IS NOT NULL
AND FoodDate >= '2015-10-01'
GROUP BY FoodID
ORDER BY NumberOfFood DESC
),
seller_temp as
SELECT FoodSellerID, FoodID,
Market1,
SellerLastName,
SellerFirstName,
PrimaryAddress1,
PrimaryAddress2,
PrimaryCity,
PrimaryState,
PrimaryZip
FROM SellerTable, MarketTable
WHERE Market1= MarketTable.MarketID
)
select a.*, b.SellerLastName
, b.SellerFirstName, b.PrimaryAddress1, b.PrimaryAddress2
, b.PrimaryCity, b.PrimaryState, b.PrimaryZip
from food_nums_temp a
inner join seller_temp b on a.FoodID = b.FoodID
請格式化您的查詢,現在完全無法讀取。 – HoneyBadger
請僅標記相關的DBMS – JohnHC
您應該開始使用ANSI-92樣式的連接......它們現在已經使用了超過25年。這意味着使用join關鍵字,而不是在where語句中使用equals謂詞。 –