1
SELECT
    count(distinct req.requirementid),
    req.requirementid,
    org.organizationid,
    req.locationofposting,
    org.registereddate
FROM OrganizationRegisteredDetails AS org,
    RequirementsDetailsforOrganization AS req
WHERE org.organizationid = req.requirementid
ORDER BY
    org.RegisteredDate desc

这向我显示了错误:

选择列表中的“RequirementsDetailsforOrganization.RequirementID”列无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。

如何在此查询中执行 'order by org.RegisteredDate desc' ....

请帮帮我.....??????

4

2 回答 2

2

您需要将SELECT列表或子句中引用的所有列添加ORDER BYGROUP BY子句中。

SELECT 
    count(distinct req.requirementid), 
    req.requirementid, 
    org.organizationid, 
    req.locationofposting, 
    org.registereddate 
FROM OrganizationRegisteredDetails AS org, 
    RequirementsDetailsforOrganization AS req 
WHERE org.organizationid = req.requirementid 
GROUP BY
    req.requirementid, 
    org.organizationid, 
    req.locationofposting, 
    org.registereddate 
ORDER BY 
    org.RegisteredDate desc 

尽管在这种情况下,您只会在所有行的第一列中获得值 1,因为您在 req.requirementid 上进行分组!

于 2010-05-14T11:09:30.523 回答
1

存在错误,因为根据规则,当您使用带有 group by 子句的聚合函数时,您必须包含选择列表中的列

以下可能会帮助您实现所需的功能

   (select count(distinct requirementid),requirementid,
     from 
    RequirementsDetailsforOrganization
    group by requirementid) d

    inner join 

    (   SELECT req.requirementid, org.organizationid,

req.locationofposting,org.registereddate FROM OrganizationRegisteredDetails AS org, 

RequirementsDetailsforOrganization AS req WHERE org.organizationid =req.requirementid order by org.RegisteredDate desc) d1
   on 
    d.requirementid= d1.requirementid
于 2010-05-14T11:10:25.583 回答