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

时间:2016-04-22 06:42:39

标签: sql sql-server database sql-server-2008

当我尝试使用SELECT子句时,我在ORDER BY查询时收到错误。当我删除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 convert(datetime,Web_Amc_End_Date,106)

获取以下错误

  

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

我哪里出错了。如何纠正?请帮忙。

2 个答案:

答案 0 :(得分:1)

ORDER BY命令也要求精确列也出现在select查询中,因为根据SQL Server中的顺序,order by命令在SELECT命令之后执行。根据您在查询中收集的内容,您已使用:

convert(varchar,Web_Amc_End_Date,106)
select子句中的

convert(datetime,Web_Amc_End_Date,106)

在order by子句中。

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

答案 1 :(得分:0)

您已在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