group by 與聚合函數(T-sql)

select A.productID,B.title,A.OuterNo,A.CustomerID,ShopID,ISdel,sum(D.Amount),sum(D.Count),Session
 from V5ESB_Customer_CustomerItem A  
 inner join V5ESB_Customer_CustomerChannel B 
 on A.CustomerID =B.CustomerID  
 inner join V5ESB_Order_Order C 
 on  A.CustomerID =C.CustomerID 
 inner join V5ESB_Order_Product D 
 on C.Code=D.OrderCode  where D.productID=1266 GROUP BY a.customerID,A.productID,B.title,A.OuterNo,ShopID,ISdel,Session
 


group by 後面的分組字段要有一一對應關係,才能分成一個分組,不能有一一對應關係的可以使用where過濾之後進行分組;

要select 的字段需要放在group by 中,如果不放的,要使用聚合函數sum, avg,count等等

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章