![](/img/trans.png)
[英]MS Access SQL: Using SELECT INTO with a UNION ALL query
[英]MS Access Query SQL Union Select returns -1
我有一个带有 SQL Union Select 的 MS Access 查询,它在我的报告中返回 -1。 我假设 -1 是因为查询返回 null 值,但我无法弄清楚如何使用 IFF() 语句停止在报告中显示 -1。 这是 SQL 查询:
SELECT [Employees].[Last Name] & ", " & [First Name] AS EmployeeName, Employees.[Active Employee], Employees.[Active Employee], Employees.[Active Employee]
FROM Employees
WHERE (((Employees.[Active Employee])=True))
ORDER BY [Employees].[Last Name] & ", " & [First Name];
UNION SELECT [Employees].[Last Name] & ", " & [First Name] AS EmployeeName, TypeOfAbsence.TypeOfAbsence, [Vacation Calendar].[Date Used], [Vacation Calendar].Time
FROM TypeOfAbsence INNER JOIN (Employees INNER JOIN [Vacation Calendar] ON Employees.ID = [Vacation Calendar].EmployeeID) ON TypeOfAbsence.ID = [Vacation Calendar].TypeID
WHERE ((([Vacation Calendar].[Date Used])<[Forms]![Week Ending Report Form]![End_Date] And ([Vacation Calendar].[Date Used])>[Forms]![Week Ending Report Form]![Start_Date]));
考虑使用 Null 作为 UNION 的字段占位符。 这将从重复的 [Active Employee] 字段中消除 -1 值。 删除嵌入的分号,因为这将导致 UNION 失败。 ORDER BY 子句可能没用,因为排序应该由报表管理,如果设置了报表排序和分组,查询顺序肯定会被忽略。 第一个 SELECT 行定义字段数据类型和名称。 不需要在后续的 SELECT 行中重复别名字段名称。
SELECT [Last Name] & ", " & [First Name] AS EmployeeName, Null AS TOA, Null AS DU, Null AS TU
FROM Employees
WHERE (((Employees.[Active Employee])=True))
UNION SELECT [Last Name] & ", " & [First Name], TypeOfAbsence, [Date Used], [Time]
FROM TypeOfAbsence
INNER JOIN (Employees INNER JOIN [Vacation Calendar]
ON Employees.ID = [Vacation Calendar].EmployeeID)
ON TypeOfAbsence.ID = [Vacation Calendar].TypeID
WHERE ((([Vacation Calendar].[Date Used])<[Forms]![Week Ending Report Form]![End_Date] And ([Vacation Calendar].[Date Used])>[Forms]![Week Ending Report Form]![Start_Date]));
还要考虑这个过滤条件:
WHERE [Date Used] BETWEEN [Forms]![Week Ending Report Form]![Start_Date] AND [Forms]![Week Ending Report Form]![End_Date]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.