2016-04-22 704 views
1

我在嘗試使用ORDER BY子句時在SELECT查詢中收到錯誤。當我刪除ORDER BY它工作正常。ORDER BY子句中的列無效,因爲它不包含在聚合函數或GROUP 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子句。

我在哪裏出錯了。如何糾正它?請幫忙。

+0

@marc_s我已經在集團採取的每列By子句 – Navy

回答

1

ORDER BY命令需要確切的列存在於選擇查詢,以及,按照自成在SQL Server中按順序執行命令是在SELECT命令之後執行的。從我可以在查詢中收集,你已經使用:

convert(varchar,Web_Amc_End_Date,106) 
SELECT子句中

,並

convert(datetime,Web_Amc_End_Date,106) 

在ORDER BY子句。

考慮其更改爲相同的格式,您的查詢將工作

+1

「當GROUP BY(和UNION和SELECT DISTINCT)ORDER BY命令需要確切的列存在時...」 – jarlh

+0

Thanx @Shubham Pandey現在工作。 – Navy

+0

@Navy Please upvote if helpful,Thanks –

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 
+0

但我需要結束日期轉換成特定的格式。以便它以正確的順序顯示結果。 – Navy

相關問題