I am trying to write a SQL query for calculating sum without success.
Let's say that we have:
- table
Awith columnsidandtype - table
Bwith columnsid,a_id(relation to tableA) andamount
I succeed to calculate number of records by type like in the following example:
SELECT DISTINCT
type,
COUNT(A.id) OVER (PARTITION BY type) AS numOfRecords
FROM A;
How to calculate sum of amounts also per type (to sum up all amounts from table B for all distinct types in A)?
SUM? Why are you usingDISTINCTandOVERwhen clearly what you really want is aGROUP BYclause?