访问查询 - 使用Order By子句时的无效操作

时间:2017-12-06 18:49:14

标签: sql ms-access access-vba

以下查询正常,但只要我添加Order By子句,我就会得到

  

错误 - “无效操作”

任何人都可以帮助我吗?

SELECT dbo_jobs.jobid,
       dbo_jobs.jobrefno,
       dbo_jobs.createdon,
       dbo_jobs.archived,
       dbo_jobs.published,
       dbo_jobs.jobtitle,
       dbo_jobs.statusid,
       dbo_jobs.clientid,
       dbo_clients.company,
       dbo_clientcontacts.clientcontactid,
       dbo_employmenttypes.employmenttypeid,
       dbo_employmenttypes.description,
       dbo_locations.description,
       dbo_jobsectordefinedcolumns.clientname,
       dbo_jobsectordefinedcolumns.natureofcase,
       dbo_jobsectordefinedcolumns.timescaletodeadline,
       dbo_jobs.notes,
       dbo_jobsectordefinedcolumns.status,
       dbo_clientcontacts.contactpersonid,
       dbo_jobsectordefinedcolumns.vat,
       dbo_jobsectordefinedcolumns.postcode,
       dbo_jobsectordefinedcolumns.additionalinfo,
       dbo_jobsectordefinedcolumns.solicitorref,
       dbo_jobconsultants.userid,
       dbo_users.loginname,
       dbo_jobs.positionattributeid,
       dbo_jobsectordefinedcolumns.consultancyservice,
       [dbo_person].[PersonName] & ' ' & [dbo_person].[Surname] AS ClientCon,
       dbo_person.personname,
       dbo_person.surname,
       dbo_jobstatus.description,
       dbo_jobsectordefinedcolumns.priority,
       RevListCount.countofrevids,
       CVCount.cvsentcount
FROM   (((SELECT dbo_jobs.jobid,
                dbo_jobs.jobrefno,
                dbo_jobs.createdon,
                dbo_jobs.archived,
                dbo_jobs.published,
                dbo_jobs.jobtitle,
                dbo_jobs.statusid,
                dbo_jobs.clientid,
                dbo_clients.company,
                dbo_clientcontacts.clientcontactid,
                dbo_employmenttypes.employmenttypeid,
                dbo_employmenttypes.description,
                dbo_locations.description,
                dbo_jobsectordefinedcolumns.clientname,
                dbo_jobsectordefinedcolumns.natureofcase,
                dbo_jobsectordefinedcolumns.timescaletodeadline,
                dbo_jobs.notes,
                dbo_jobsectordefinedcolumns.status,
                dbo_clientcontacts.contactpersonid,
                dbo_jobsectordefinedcolumns.vat,
                dbo_jobsectordefinedcolumns.postcode,
                dbo_jobsectordefinedcolumns.additionalinfo,
                dbo_jobsectordefinedcolumns.solicitorref,
                dbo_jobconsultants.userid,
                dbo_users.loginname,
                dbo_jobs.positionattributeid,
                dbo_jobsectordefinedcolumns.consultancyservice,
                [dbo_person].[personname] & ' ' & [dbo_person].[surname] AS
                 ClientCon,
                dbo_person.personname,
                dbo_person.surname,
                dbo_jobstatus.description,
                dbo_jobsectordefinedcolumns.priority
         FROM   ((((((((dbo_employmenttypes
                        INNER JOIN dbo_jobs
                                ON dbo_employmenttypes.employmenttypeid =
                                  dbo_jobs.employmenttypeid)
                       INNER JOIN dbo_clients
                               ON dbo_jobs.clientid = dbo_clients.clientid)
                      INNER JOIN dbo_clientcontacts
                              ON dbo_jobs.clientcontactid =
                                 dbo_clientcontacts.clientcontactid)
                     LEFT JOIN dbo_jobsectordefinedcolumns
                            ON dbo_jobs.jobid =
dbo_jobsectordefinedcolumns.jobid)
                    INNER JOIN dbo_person
                            ON dbo_clientcontacts.contactpersonid =
                   dbo_person.personid)
                   LEFT JOIN dbo_jobconsultants
                          ON dbo_jobs.jobid = dbo_jobconsultants.jobid)
                  LEFT JOIN dbo_users
                         ON dbo_jobconsultants.userid = dbo_users.userid)
                 LEFT JOIN dbo_jobstatus
                        ON dbo_jobs.statusid = dbo_jobstatus.jobstatusid)
                LEFT JOIN dbo_locations
                       ON dbo_jobs.locationid = dbo_locations.locationid
         WHERE  ( ( ( dbo_jobs.archived ) = 'N' )
                  AND ( ( dbo_employmenttypes.employmenttypeid ) <> 12 )
                  AND ( ( dbo_jobconsultants.userrelationshipid ) = 9 )
                  AND
         ( ( dbo_jobstatus.description ) <> 'Filled'
           AND ( dbo_jobstatus.description ) <> 'Filled - Ongoing' ) )) AS
        AllJobs
         LEFT JOIN (SELECT dbo_reviewlists.jobid,
                           Count(dbo_reviewlistapplicants.reviewlistid) AS
                           CountOfRevIDs
                    FROM   dbo_reviewlists
                           LEFT JOIN dbo_reviewlistapplicants
                                  ON dbo_reviewlists.reviewlistid =
                                     dbo_reviewlistapplicants.reviewlistid
                    GROUP  BY dbo_reviewlists.jobid) AS RevListCount
                ON AllJobs.jobid = RevListCount.jobid)
        LEFT JOIN (SELECT dbo_applicantactions.jobid,
                          Count(dbo_applicantactions.statusid) AS CVSentCount
                   FROM   dbo_applicantactions
                   WHERE  (( ( dbo_applicantactions.statusid ) = 29 ))
                   GROUP  BY dbo_applicantactions.jobid) AS CVCount
               ON AllJobs.jobid = CVCount.jobid)
ORDER  BY dbo_jobs.jobid DESC  

2 个答案:

答案 0 :(得分:1)

您没有为最外面的查询块指定别名,并且您尝试通过dbo_Jobs.JobId而不是alias_name.JobId

进行排序

(或者,或者不需要group by和order by子句之间的括号 - 我丢失了括号!)

答案 1 :(得分:0)

神圣的无关紧要,蝙蝠侠!这应该工作。看起来您需要将AllJobs的外表别名应用于外部select语句的列。我还清理了所有不必要的括号。

SELECT
    AllJobs.jobid
    ,AllJobs.jobrefno
    ,AllJobs.createdon
    ,AllJobs.archived
    ,AllJobs.published
    ,AllJobs.jobtitle
    ,AllJobs.statusid
    ,AllJobs.clientid
    ,AllJobs.company
    ,AllJobs.clientcontactid
    ,AllJobs.employmenttypeid
    ,AllJobs.description
    ,AllJobs.description
    ,AllJobs.clientname
    ,AllJobs.natureofcase
    ,AllJobs.timescaletodeadline
    ,AllJobs.notes
    ,AllJobs.status
    ,AllJobs.contactpersonid
    ,AllJobs.vat
    ,AllJobs.postcode
    ,AllJobs.additionalinfo
    ,AllJobs.solicitorref
    ,AllJobs.userid
    ,AllJobs.loginname
    ,AllJobs.positionattributeid
    ,AllJobs.consultancyservice
    ,AllJobs.ClientCon
    ,AllJobs.personname
    ,AllJobs.surname
    ,AllJobs.description
    ,AllJobs.priority
    ,RevListCount.CountOfRevIDs
    ,CVCount.CVSentCount
FROM(
        SELECT
            dbo_jobs.jobid
            ,dbo_jobs.jobrefno
            ,dbo_jobs.createdon
            ,dbo_jobs.archived
            ,dbo_jobs.published
            ,dbo_jobs.jobtitle
            ,dbo_jobs.statusid
            ,dbo_jobs.clientid
            ,dbo_clients.company
            ,dbo_clientcontacts.clientcontactid
            ,dbo_employmenttypes.employmenttypeid
            ,dbo_employmenttypes.description
            ,dbo_locations.description
            ,dbo_jobsectordefinedcolumns.clientname
            ,dbo_jobsectordefinedcolumns.natureofcase
            ,dbo_jobsectordefinedcolumns.timescaletodeadline
            ,dbo_jobs.notes
            ,dbo_jobsectordefinedcolumns.status
            ,dbo_clientcontacts.contactpersonid
            ,dbo_jobsectordefinedcolumns.vat
            ,dbo_jobsectordefinedcolumns.postcode
            ,dbo_jobsectordefinedcolumns.additionalinfo
            ,dbo_jobsectordefinedcolumns.solicitorref
            ,dbo_jobconsultants.userid
            ,dbo_users.loginname
            ,dbo_jobs.positionattributeid
            ,dbo_jobsectordefinedcolumns.consultancyservice
            ,[dbo_person].[personname] & ' ' & [dbo_person].[surname] AS ClientCon
            ,dbo_person.personname
            ,dbo_person.surname
            ,dbo_jobstatus.description
            ,dbo_jobsectordefinedcolumns.priority
        FROM dbo_employmenttypes
        INNER JOIN dbo_jobs ON dbo_employmenttypes.employmenttypeid = dbo_jobs.employmenttypeid
        INNER JOIN dbo_clients ON dbo_jobs.clientid = dbo_clients.clientid
        INNER JOIN dbo_clientcontacts ON dbo_jobs.clientcontactid = dbo_clientcontacts.clientcontactid
        LEFT JOIN dbo_jobsectordefinedcolumns ON dbo_jobs.jobid = dbo_jobsectordefinedcolumns.jobid
        INNER JOIN dbo_person ON dbo_clientcontacts.contactpersonid = dbo_person.personid
        LEFT JOIN dbo_jobconsultants ON dbo_jobs.jobid = dbo_jobconsultants.jobid
        LEFT JOIN dbo_users ON dbo_jobconsultants.userid = dbo_users.userid
        LEFT JOIN dbo_jobstatus ON dbo_jobs.statusid = dbo_jobstatus.jobstatusid
        LEFT JOIN dbo_locations ON dbo_jobs.locationid = dbo_locations.locationid
        WHERE dbo_jobs.archived = 'N'
            AND dbo_employmenttypes.employmenttypeid <> 12
            AND dbo_jobconsultants.userrelationshipid = 9
            AND (
                    dbo_jobstatus.description <> 'Filled'
                    AND dbo_jobstatus.description <> 'Filled - Ongoing'
                )
    )      AS AllJobs
LEFT JOIN(
             SELECT
                 dbo_reviewlists.jobid
                 ,COUNT(dbo_reviewlistapplicants.reviewlistid) AS CountOfRevIDs
             FROM dbo_reviewlists
             LEFT JOIN dbo_reviewlistapplicants ON dbo_reviewlists.reviewlistid = dbo_reviewlistapplicants.reviewlistid
             GROUP BY dbo_reviewlists.jobid
         ) AS RevListCount ON AllJobs.jobid = RevListCount.jobid
LEFT JOIN(
             SELECT
                 dbo_applicantactions.jobid
                 ,COUNT(dbo_applicantactions.statusid) AS CVSentCount
             FROM dbo_applicantactions
             WHERE dbo_applicantactions.statusid = 29
             GROUP BY dbo_applicantactions.jobid
         ) AS CVCount ON AllJobs.jobid = CVCount.jobid
ORDER BY AllJobs.jobid DESC;