有人可以告诉我如何将这些查询嵌套到一个完整的查询中吗?现在,我一次只运行一个(最多10个),但我想学习如何一次性运行这个查询。
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 & ""
答案 0 :(得分: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
(当然,您需要删除我为了便于阅读而添加的额外换行符)
答案 1 :(得分:1)
让我们检查这些查询返回的内容:
(所有三个使用相同的组和相同的顺序)
1-返回“areaFooBar”
中的所有“活跃”员工2-返回“areaFooBar”的所有“活跃”员工减去一名员工
3-返回“areaFooBar”的所有“活跃”员工减去两名员工
您可以在代码中执行此类过滤。 您不必运行查询2或查询3。 重新迭代查询1的结果集并过滤您不想包含的员工。
每次都必须从数据库中读取它,如果你这么做就可以使用一个queryString 在查询中再添加一个参数,即:逗号分隔列表。文本
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 & ""