Reputation: 105
I'm struggling with this error when I change the statement from order to group, the goal it's to get the sum f all records that met the query.
I've two tables, ServiceConsumer_T and ServiceUsage_t, in the ServiceConsumer_t are all the consumer details:
ID Name
032159DA-9BD6-49F0-8D12-5806DD17AE4B 40791010
05EECE4D-9E46-420A-9EE8-1605A34A1BE9 20002810
07C9EDB9-CF1C-4F32-83D5-4BE0293E626F 20019010
0AB09B43-F695-4314-9674-A6B34254A1F0 20002700
0FE8F929-D33C-4CA7-A0FA-0C804EDFE29C 20002210
in the ServiceUsage_t are the movements by customers:
CostCenterPath Cardinality
{032159DA-9BD6-49F0-8D12-5806DD17AE4B} 282
{032159DA-9BD6-49F0-8D12-5806DD17AE4B} 9
{032159DA-9BD6-49F0-8D12-5806DD17AE4B} 555
{05EECE4D-9E46-420A-9EE8-1605A34A1BE9} 200
{05EECE4D-9E46-420A-9EE8-1605A34A1BE9} 200
{05EECE4D-9E46-420A-9EE8-1605A34A1BE9} 3
{05EECE4D-9E46-420A-9EE8-1605A34A1BE9} 3
{07C9EDB9-CF1C-4F32-83D5-4BE0293E626F} 1500
{07C9EDB9-CF1C-4F32-83D5-4BE0293E626F} 1
{07C9EDB9-CF1C-4F32-83D5-4BE0293E626F} 1
{07C9EDB9-CF1C-4F32-83D5-4BE0293E626F} 243
This is the query that I'm utilizing:
Select ServiceConsumer_T.Name, ServiceUsage_T.Cardinality from ServiceConsumer_T
left join ServiceUsage_T
on ServiceConsumer_T.ID=ServiceUsage_T.CostCenterPath
order by ServiceConsumer_T.Name
And I get all records ordered by Name, but when I change from order to group I get the error:
Column 'ServiceUsage_T.Cardinality' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Can anyone help me to achieve this?
Upvotes: 0
Views: 415
Reputation: 204854
If you group by a column, all other must be aggregated. You want to sum, so use that aggregate function
Select c.Name, sum(u.Cardinality)
from ServiceConsumer_T c
left join ServiceUsage_T u on c.ID = u.CostCenterPath
group by c.Name
Upvotes: 2