使用AS的Union Query在SQL中创建了ORDER BY的问题

时间:2018-04-06 15:42:56

标签: sql union ms-access-2013

我正在为另一个部门输出报告,他们需要特定的标题(Excel单元格列标题)。我有一个联合查询来输出信息。

ORDER BY部分外,所有工作正常。

如果我使用完整的tblInventory.[Employee Number] AS [Employee No],我会收到" Missing Operator" 错误,并突出显示AS

如果您只是放ORDER BY [Employee No],那么我需要的DISTINCT克拉存在问题。

关于它需要什么操作符或如何对其进行排序的任何想法?

SELECT DISTINCT tblinventory.[Phone Number] AS [Wireless No],
                tblemployeelist.[Employee Number] AS [Employee No],
                tblemployeelist.[Payroll    First Name] AS [First Name],
                tblemployeelist.[Payroll Last Name] AS [Last    Name],
                tblvendors.[Vendor Name] AS [Wireless Carrier],
                "Company" AS [Acct    Liability]
FROM tblvendors
INNER JOIN (tblemployeelist
            INNER JOIN tblinventory ON tblemployeelist.[Employee Number] = tblinventory.[Employee Number])
AND (tblemployeelist.[Employee Number] = tblinventory.[Employee Number])) ON tblvendors.id = tblinventory.carrier
WHERE (((tblinventory.[Phone Number]) IS NOT NULL)
       AND ((tblvendors.[Vendor    Name]) <>"Roadpost"
            AND (tblvendors.[Vendor Name]) <>"LIVETV Airfone Inc.")
       AND ((tblinventory.[Asset Description]) LIKE "*" & "phone" & "*")
       AND ((tblinventory.disposition) =2)
       AND ((tblinventory.spare) =FALSE)
       AND ((tblemployeelist.[End Date]) NOT LIKE "*"))
ORDER BY ([tblEmployeeList].[Employee Number] AS [Employee No])
UNION
SELECT tblmcpcollated.[Phone Number] AS [Wireless No],
       tblemployeelist.[Employee Number] AS [Employee No],
       tblemployeelist.[Payroll    First Name] AS [First Name],
       tblemployeelist.[Payroll Last Name] AS [Last    Name],
       tblvendors.[Vendor Name] AS [Wireless Carrier],
       "Employee" AS [Acct    Liability]
FROM tblvendors
INNER JOIN (tblemployeelist
            INNER JOIN tblmcpcollated ON tblemployeelist.[Employee Number] = tblmcpcollated.[Employee Number]) ON tblvendors.id = tblmcpcollated.vendor
WHERE (((tblmcpcollated.[Phone Number]) IS NOT NULL)
       AND ((tblmcpcollated.status)="Active")
       AND ((tblmcpcollated.[MCP Program])<>1)
       AND ((tblmcpcollated.[Compensation Amt])>0)
       AND ((tblemployeelist.[End    Date]) NOT LIKE "*"))
  OR (((tblmcpcollated.[Phone Number]) IS NOT NULL)
      AND ((tblmcpcollated.status)="Pending")
      AND ((tblmcpcollated.[MCP Program])<>1)
      AND ((tblmcpcollated.[Compensation Amt])>0)
      AND ((tblemployeelist.[End    Date]) NOT LIKE "*"))
ORDER BY ([tblEmployeeList].[Employee Number] AS [Employee No]);

如果删除ORDER BY,一切正常。我只想在那里使用sort函数。

提前感谢您的精彩知识。

2 个答案:

答案 0 :(得分:1)

您需要从ORDER BY中删除列名称。正如你所说,它围绕关键字AS抛出错误。

你需要:

ORDER BY ([tblEmployeeList].[Employee Number])

ORDER BY ([tblEmployeeList].[Employee Number] AS [Employee No])

答案 1 :(得分:0)

由于我无法读取您的SQL,因此应该这样做并且对查询构建器是透明的:

SELECT * 
FROM
(
    SELECT DISTINCT tblInventory.[Phone Number] AS [Wireless No], 
    tblEmployeeList.[Employee Number] AS [Employee No], tblEmployeeList.[Payroll 
    First Name] AS [First Name], tblEmployeeList.[Payroll Last Name] AS [Last 
    Name], tblVendors.[Vendor Name] AS [Wireless Carrier], "Company" AS [Acct 
    Liability]
    FROM tblVendors INNER JOIN (tblEmployeeList INNER JOIN tblInventory ON    
    tblEmployeeList.[Employee Number] = tblInventory.[Employee Number]) AND 
    (tblEmployeeList.[Employee Number] = tblInventory.[Employee Number])) ON 
    tblVendors.ID = tblInventory.Carrier
    WHERE (((tblInventory.[Phone Number]) Is Not Null) AND ((tblVendors.[Vendor 
    Name])<>"Roadpost" And (tblVendors.[Vendor Name])<>"LIVETV Airfone Inc.") 
    AND ((tblInventory.[Asset Description]) Like "*" & "phone" & "*") AND 
    ((tblInventory.Disposition)=2) AND ((tblInventory.Spare)=False) AND 
    ((tblEmployeeList.[End Date]) Not Like "*"))
    ORDER BY ([tblEmployeeList].[Employee Number] AS [Employee No])
    UNION SELECT tblMCPCollated.[Phone Number] as [Wireless No], 
    tblEmployeeList.[Employee Number] as [Employee No], tblEmployeeList.[Payroll 
    First Name] as [First Name], tblEmployeeList.[Payroll Last Name] as [Last 
    Name], tblVendors.[Vendor Name] as [Wireless Carrier], "Employee" as [Acct 
    Liability]
    FROM tblVendors INNER JOIN (tblEmployeeList INNER JOIN tblMCPCollated ON 
    tblEmployeeList.[Employee Number] = tblMCPCollated.[Employee Number]) ON 
    tblVendors.ID = tblMCPCollated.Vendor
    WHERE (((tblMCPCollated.[Phone Number]) Is Not Null) AND 
    ((tblMCPCollated.Status)="Active") AND ((tblMCPCollated.[MCP Program])<>1) 
    AND ((tblMCPCollated.[Compensation Amt])>0) AND ((tblEmployeeList.[End 
    Date]) Not Like "*")) OR (((tblMCPCollated.[Phone Number]) Is Not Null) AND 
    ((tblMCPCollated.Status)="Pending") AND ((tblMCPCollated.[MCP Program])<>1) 
    AND ((tblMCPCollated.[Compensation Amt])>0) AND ((tblEmployeeList.[End 
    Date]) Not Like "*"))
) t1
ORDER BY t1.[Employee No];