Sql 列在ORDER BY子句中无效,因为它未包含在聚合函数或GROUP BY子句中

Sql 列在ORDER BY子句中无效,因为它未包含在聚合函数或GROUP BY子句中,sql,sql-server,database,sql-server-2008,Sql,Sql Server,Database,Sql Server 2008,当我试图使用orderby子句时,我在SELECT查询中遇到错误。当我按删除订单时,它工作正常 我的问题是: Select tbl_User.CompanyName as CompanyName, tbl_User.ResellerName as ResellerName, tbl_New_Web_Amc.Domain_Name, tbl_Website_Type.Website_type as [Type], convert(varchar,Web_Amc_S

当我试图使用
orderby
子句时,我在
SELECT
查询中遇到错误。当我按删除订单时,它工作正常

我的问题是:

Select  tbl_User.CompanyName as CompanyName,
    tbl_User.ResellerName as ResellerName,
    tbl_New_Web_Amc.Domain_Name,
    tbl_Website_Type.Website_type as [Type],
    convert(varchar,Web_Amc_Start_Date,106) as StartDate,
    convert(varchar,Web_Amc_End_Date,106) as EndDate,
    tbl_New_Web_Amc.Web_Amc_Amount as Amount,
    tbl_Billing.CompanyName as BillingCompany,
    tbl_Billing.ContactPerson1,
    tbl_Billing.Mobile1,
    tbl_Billing.ContactPerson2,
    tbl_Billing.Mobile2,
    tbl_Billing.ContactPerson3,
    tbl_Billing.Mobile3,
    tbl_Billing.Telephone1,
    tbl_Billing.Telephone2,
    tbl_Billing.Telephone3,
    tbl_Billing.EmailId1,
    tbl_Billing.EmailId2,
    tbl_Billing.EmailId3
from tbl_New_Web_Amc 
   join tbl_User on tbl_New_Web_Amc.Customer_Id=tbl_User.UserId 
   join tbl_Domain_Details on tbl_New_Web_Amc.Customer_Id=tbl_Domain_Details.Customer_Id
   join tbl_Billing on tbl_New_Web_Amc.Customer_Id=tbl_Billing.User_ID
   join tbl_Website_Type on tbl_Website_Type.Type_Id=tbl_New_Web_Amc.Web_Amc_Site_Type
   where WebsiteAmc_Id is not null
group by tbl_User.CompanyName ,
    tbl_User.ResellerName ,
    tbl_New_Web_Amc.Domain_Name,
    tbl_Website_Type.Website_type,
    convert(varchar,Web_Amc_End_Date,106),
    convert(varchar,Web_Amc_Start_Date,106),
    tbl_New_Web_Amc.Web_Amc_Amount,
    tbl_Billing.CompanyName,
    tbl_Billing.ContactPerson1,
    tbl_Billing.ContactPerson2,
    tbl_Billing.ContactPerson3,
    tbl_Billing.Telephone1,
    tbl_Billing.Telephone2,
    tbl_Billing.Telephone3,
    tbl_Billing.Mobile1,
    tbl_Billing.Mobile2,
    tbl_Billing.Mobile3,
    tbl_Billing.EmailId1,
    tbl_Billing.EmailId2,
    tbl_Billing.EmailId3
    order by convert(datetime,Web_Amc_End_Date,106)
获取以下错误

列“tbl_New_Web_Amc.Web_Amc_End_Date”在ORDER BY子句中无效,因为它未包含在聚合函数或GROUP BY子句中


我错在哪里。如何纠正它??请提供帮助。

您已经在SELECT语句中使用了该表达式,并在ORDER BY子句中使用了别名

Select  tbl_User.CompanyName as CompanyName,
    tbl_User.ResellerName as ResellerName,
    tbl_New_Web_Amc.Domain_Name,
    tbl_Website_Type.Website_type as [Type],
    convert(varchar,Web_Amc_Start_Date,106) as StartDate,
    convert(varchar,Web_Amc_End_Date,106) as EndDate,
    tbl_New_Web_Amc.Web_Amc_Amount as Amount,
    tbl_Billing.CompanyName as BillingCompany,
    tbl_Billing.ContactPerson1,
    tbl_Billing.Mobile1,
    tbl_Billing.ContactPerson2,
    tbl_Billing.Mobile2,
    tbl_Billing.ContactPerson3,
    tbl_Billing.Mobile3,
    tbl_Billing.Telephone1,
    tbl_Billing.Telephone2,
    tbl_Billing.Telephone3,
    tbl_Billing.EmailId1,
    tbl_Billing.EmailId2,
    tbl_Billing.EmailId3
from tbl_New_Web_Amc 
   join tbl_User on tbl_New_Web_Amc.Customer_Id=tbl_User.UserId 
   join tbl_Domain_Details on tbl_New_Web_Amc.Customer_Id=tbl_Domain_Details.Customer_Id
   join tbl_Billing on tbl_New_Web_Amc.Customer_Id=tbl_Billing.User_ID
   join tbl_Website_Type on tbl_Website_Type.Type_Id=tbl_New_Web_Amc.Web_Amc_Site_Type
   where WebsiteAmc_Id is not null
group by tbl_User.CompanyName ,
    tbl_User.ResellerName ,
    tbl_New_Web_Amc.Domain_Name,
    tbl_Website_Type.Website_type,
    convert(varchar,Web_Amc_End_Date,106),
    convert(varchar,Web_Amc_Start_Date,106),
    tbl_New_Web_Amc.Web_Amc_Amount,
    tbl_Billing.CompanyName,
    tbl_Billing.ContactPerson1,
    tbl_Billing.ContactPerson2,
    tbl_Billing.ContactPerson3,
    tbl_Billing.Telephone1,
    tbl_Billing.Telephone2,
    tbl_Billing.Telephone3,
    tbl_Billing.Mobile1,
    tbl_Billing.Mobile2,
    tbl_Billing.Mobile3,
    tbl_Billing.EmailId1,
    tbl_Billing.EmailId2,
    tbl_Billing.EmailId3
    order by EndDate

ORDER BY命令也要求select查询中出现精确的列,因为按照SQL Server中的顺序,ORDER BY命令在select命令之后执行。根据我在您的查询中收集到的信息,您使用了:

convert(varchar,Web_Amc_End_Date,106)
在select子句中,以及

convert(datetime,Web_Amc_End_Date,106)
在ORDERBY子句中


考虑将它们更改为相同的格式,您的查询将起作用

@marc\s我已按clauseb获取了Group中的每一列,但我需要将EndDate转换为特定格式。当GROUP BY(以及UNION和SELECT DISTINCT)order BY命令要求显示准确的列时…@NAVICE,ohh ok:D刷新延迟应归咎于:D