dames
dames

Reputation: 1481

Sum of a summation in mysql

I have the following query, in the top select statement (sum(l.app_ln_amnt)/count(l.app_ln_amnt)) works well but in the union I want to find the total of (sum(l.app_ln_amnt)/count(l.app_ln_amnt)) query from the top select statement However my solution seems to be off I need some help please

select 
(sum(l.app_ln_amnt)/count(l.app_ln_amnt)),


from receipt_history l

     UNION

select 
SUM(sum(l.app_ln_amnt)/count(l.app_ln_amnt)),


from receipt_history l

THIS is what the initial table looks like

id  app_ln_amnt
1   2
1   2
1   2
1   2
2   5
3   7
4   9


id  app_ln_amnt
1   2
2   5
3   7
4   9
total   23

Now my table looks like the second one but the total is 29 and im trying to get it to be 23

Upvotes: 0

Views: 109

Answers (1)

ESG
ESG

Reputation: 9425

You could use a subquery rather than a union

SELECT SUM(Value) FROM 
  (select 
  (sum(l.app_ln_amnt)/count(l.app_ln_amnt)) AS value
  from receipt_history l ) t

Upvotes: 1

Related Questions