D
D
Damir Shaniyazov2021-09-28 12:42:28
SQL Server
Damir Shaniyazov, 2021-09-28 12:42:28

How to find the sum of averages?

How to get the amount in one line for one field (budget). But there is a problem, in the table the projects are repeated several times, only the contract changes, but the budget remains the same. When I use order by iid (by project), I get the sum of project budgets

select AVG([budg]) as iid_avg 
  from [projects] 
  where budg is not null 
  group by [iid]

and only then I get the project => budget table. How do I add up the budgets of all projects now?
spoiler
6152e37941d2f203647520.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2021-09-28
@shaniyazovdamir

From what I understood:

select sum(budget) from (
  select distinct project, budget from projects
) p;

SQL fiddle

K
Konstantin Tsvetkov, 2021-09-28
@tsklab

budget stays the same
Why calculate the average for the same values? The average will be this value. So you need to take one line for each iid.
SELECT SUM(one_bug)
  FROM( SELECT DISTINCT iid, bugs AS one_bug FROM projects ) AS DB

Your question is the consequences of breaking the normal form. The subquery is the "Project" table. Contracts require a separate table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question