La colonne n'est pas valide dans la clause ORDER BY, car elle n'est contenue ni dans une fonction d'agrégat ni dans la clause GROUP BY

J'obtiens une erreur sur une requête SELECT lorsque j'essaie d'utiliser la clause ORDER BY . Quand j'enlève ORDER BY cela fonctionne très bien.

Voici ma requête:

 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) 

Obtenir l'erreur suivante

La colonne "tbl_New_Web_Amc.Web_Amc_End_Date" n'est pas valide dans la clause ORDER BY car elle n'est contenue ni dans une fonction d'agrégat ni dans la clause GROUP BY.

Où je vais mal. Comment le rectifier ?? S'il vous plaît aider.

La command ORDER BY requirejs également la présence de la colonne exacte dans la requête select, puisque, conformément à l'ordre dans SQL Server, la command order by est exécutée après la command SELECT. De ce que je peux rassembler dans votre requête, vous avez utilisé:

 convert(varchar,Web_Amc_End_Date,106) 

dans la clause select, et

 convert(datetime,Web_Amc_End_Date,106) 

dans la clause de l'ordre.

Envisager de les changer au même format, votre requête fonctionnera

Vous avez déjà cette expression dans l'instruction SELECT et utilisez l'alias dans la clause 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