2013-12-20 115 views
0

有人可以告訴我如何將這些查詢嵌套到一個完整的查詢中嗎?現在,我一次只運行一個(最多10個),但我想學習如何在單個鏡頭中運行此查詢。如何嵌套多個SQL查詢?

Dim queryString1 As String = 
"SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD 
FROM dbo.APE_BUSDRIVER_MAIN 
WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " 
AND ACTIVE = 1 
GROUP BY EMPLOYEE_NAME ORDER BY YIELD " & lblSortOrder.Text & "" 


Dim queryString2 As String = 
"SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD 
FROM dbo.APE_BUSDRIVER_MAIN 
WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " 
AND ACTIVE = 1 
AND EMPLOYEE_NAME <> '" & LblStckRnk1.Text & "' 
GROUP BY EMPLOYEE_NAME 
ORDER BY YIELD " & lblSortOrder.Text & "" 


Dim queryString3 As String = 
"SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD 
FROM dbo.APE_BUSDRIVER_MAIN 
WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " 
AND ACTIVE = 1 
AND EMPLOYEE_NAME <> '" & LblStckRnk1.Text & "' 
AND EMPLOYEE_NAME <> '" & LblStckRnk2.Text & "' 
GROUP BY EMPLOYEE_NAME 
ORDER BY YIELD " & lblSortOrder.Text & "" 

回答

1

我會用幾個UNION語句:

Dim queryString1 As String = "SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD 
    FROM dbo.APE_BUSDRIVER_MAIN WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND ACTIVE = 1 
    GROUP BY EMPLOYEE_NAME ORDER BY YIELD " & lblSortOrder.Text & vbCrLf & _ 

" UNION " & vbCrLf & _ 

"SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD 
    FROM dbo.APE_BUSDRIVER_MAIN 
    WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND ACTIVE = 1 
    AND EMPLOYEE_NAME <> '" & LblStckRnk1.Text & "' 
    GROUP BY EMPLOYEE_NAME ORDER BY YIELD " & lblSortOrder.Text & vbCrLf & _ 

" UNION " & vbCrLf & _ 

"SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD 
    FROM dbo.APE_BUSDRIVER_MAIN 
    WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND ACTIVE = 1 
    AND EMPLOYEE_NAME <> '" & LblStckRnk1.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk2.Text & "' 
    GROUP BY EMPLOYEE_NAME ORDER BY YIELD " & lblSortOrder.Text 

(當然,你將要刪除多餘的換行符,我增加了可讀性)

+0

OK涼爽。我會嘗試的。謝謝! –

+0

問題:如何生成第二個聯合查詢的itemID?我想如果我使用CType(Reader.Item(2),字符串)我將能夠獲得第二個員工,但即時通訊「索引超出數組的界限。」錯誤 –

+0

ItemID不是結果集的一部分。您需要將其添加到三個查詢中的每一個。請記住,Reader.Item(2)引用SELECT語句中的第三項(並且只指定了兩個項目)。 – tgolisch

1

讓我們來看看這是什麼查詢返回:

(所有三個通過使用相同的基團和以相同的順序)

1-返回所有的「活性」員工「areaFooBar」

2-返回在「areaFooBar」所有的「活性」僱員MINUS一個僱員

3-返回所有的「活性」員工「areaFooBar 「減去兩位員工

您可以在代碼中進行這種過濾。 您不必運行查詢2或查詢3. 重新查詢查詢1的結果集並過濾不想包含的員工。

您可以從數據庫中每一次讀它,那麼你可以使用一個的queryString如果你 一個參數添加到您的查詢即:逗號分隔list.Text

Dim queryString1 As String = 
" 
SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD 
FROM dbo.APE_BUSDRIVER_MAIN 
WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " 
AND ACTIVE = 1 
EMPLOYEE_NAME NOT IN (" & comma-separated-list.Text & ") 
GROUP BY EMPLOYEE_NAME ORDER BY YIELD " & lblSortOrder.Text & "" 
+0

能否詳細說明逗號分隔列表。文本部分?所以,這是我需要在查詢中輸入的全部內容? –

+0

聲明一個varchar變量作爲逗號分隔列表。您可以使用「LblStckRnkX.Text」值填充此變量(逗號分隔列表)。即:如果不包含LblStckRnk文本,請將逗號分隔列表設置爲空字符串。如果您將通過LblStckRnk1.Text和LblStckRnk2.Text和LblStckRnk3.Text;然後將逗號分隔列表設置爲[LblStckRnk1.Text +「,」+ LblStckRnk2.Text +「,」LblStckRnk3.Text]。 –