在我的简单查询中表达式如何无效?

时间:2019-05-01 01:55:29

标签: sql sql-server-2012

我是SQL的新手,它从相同的示例创建一个简单的查询,但不确定为什么1个表达式无效?

我尝试了以下语句:

SELECT      
    A.AccountId,
    A.Address1_City,
    A.Address1_Country,
    A.Address1_PostalCode,
    A.Address1_StateOrProvince,
    A.CreatedOn,
    A.EMailAddress1,
    A.mcs_ABN2,
    A.mcs_AdminContact,
    A.mcs_AdminContactName,
    A.mcs_AreyouinterestedinInternationalDevelopmen,
    A.mcs_BenefitsUtilised,
    A.mcs_BusinessNameOrganisationName,
    A.mcs_doyouprovideaccreditedtraining,
    A.mcs_doyouprovidenonaccreditedtraining,
    A.mcs_DoyouprovideservicesinanyNDISTrialSites,
    A.mcs_DoyouprovideservicestoChildrenYoungPeople,
    A.mcs_DummyRecord,
    A.mcs_EntityStatusCode,
    A.mcs_IndustryOperatingIn,
    A.mcs_IsStreetAddresssameasPostalAddress,
    A.mcs_MembershipNote,
    A.mcs_MultiStateDepartment,
    A.mcs_NotefromApplicant,
    A.mcs_NumberofEmployeesDec,
    A.mcs_OtherTradingNameOrganisationName,
    A.mcs_PartnerDisabilityServiceOrganisations,
    A.mcs_PrimaryProductsServicesToPromote,
    A.mcs_RevenueDisabServiceProvisionTotalAnnual,
    A.mcs_SageERPCustomerCode,
    A.mcs_SageErpExportFlag,
    A.ModifiedOn,
    A.Name,
    A.nds_Account_MembershipNumberCopy,
    A.nds_IsMember,
    A.nds_MainContact,
    A.nds_MainContactName,
    A.nds_MemberCategoryCopy,
    A.PrimaryContactId,
    A.Revenue,
    A.Revenue_Base,
    A.StatusCode,
    A.WebSiteURL
FROM
    account A
WHERE
    A.statuscode = 1
GROUP BY
    A.name

并收到此错误:

  

错误[42000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]选择列表中的“ account.AccountId”列无效,因为它既不包含在聚合函数中也不在GROUP BY子句中。

1 个答案:

答案 0 :(得分:3)

Group By用于聚合函数(Count, Sum, etc),因此,如果使用group by,则需要至少对select SQL中返回的每一列进行分组,否则它将无法进行汇总。看起来您只是想按名称订购,所以如果您替换

GROUP BY          A.name

ORDER BY A.name

它应该工作。如果您不关心顺序或汇总,则只需删除查询的那一部分即可。