我有一个查询,它返回所有发货部件,工厂部件,每个部件的输入日期以及各自的电子邮件。但是我需要每个人输入的所有船舶部件和工厂部件的总计数,所以我添加了你在下面看到的COUNT函数,但我不断得到这个错误"每个GROUP BY表达式必须包含至少一个不是外部参考。"有什么想法吗?
select Distinct cc.FirstName+' '+cc.LastName AS 'Name', UEmail AS 'Email', sp.SEntered AS 'Date Entered', COUNT(sp.SHIPID) AS 'Shipments', COUNT(pn.PNumber) AS 'Factory Parts'
from R_USERS_Detail U
join R_SHIPMENTS sp on sp.UID=U.UID
join R_SHIPMENTOrders sd on sd.SHIPID=sp.SHIPID
join R_PARTOrders pn on pn.PNumberID=sd.PNumberID
join R_Clients c on c.COClientID=sp.COClientID AND c.COClientAcronym LIKE '%AME%'
join R_Contacts cc on cc.COContactID=u.COContactID
group by u.UEmail, sp.SEntered, 'Name'
答案 0 :(得分:2)
您必须使用group by
claus中的确切表达式,而不是别名。
select cc.FirstName+' '+cc.LastName AS 'Name'
, UEmail AS 'Email'
, sp.SEntered AS 'Date Entered'
, COUNT(sp.SHIPID) AS 'Shipments'
, COUNT(pn.PNumber) AS 'Factory Parts'
from R_USERS_Detail U
join R_SHIPMENTS sp on sp.UID=U.UID
join R_SHIPMENTOrders sd on sd.SHIPID=sp.SHIPID
join R_PARTOrders pn on pn.PNumberID=sd.PNumberID
join R_Clients c on c.COClientID=sp.COClientID AND c.COClientAcronym LIKE '%AME%'
join R_Contacts cc on cc.COContactID=u.COContactID
group by u.UEmail, sp.SEntered, cc.FirstName+' '+cc.LastName