Domanda

It doesn't recognize "sum". why?

SELECT c.c_id, c.c_name, SUM( p.p_sum ) AS sum
FROM clients c, contracts_rent r, payments p
WHERE c.c_id = r.c_id
AND r.contract_id = p.contract_id and sum>1300
GROUP BY c.c_id 
È stato utile?

Soluzione 2

Something like...

SELECT c.c_id, c.c_name, SUM( p.p_sum ) AS sum
FROM clients c, contracts_rent r, payments p
WHERE c.c_id = r.c_id
AND r.contract_id = p.contract_id
GROUP BY c.c_id 
HAVING SUM( p.p_sum ) > 1300

When you want to filter on the aggregate function value, you use the HAVING keyword

Altri suggerimenti

Aggregated columns can't usually be handled within a where clause since that clause happens before aggregation.

What you need is the having clause, which occurs after aggregation.

Compare something like:

select id, something, p_sum from tbl
where p_sum > 100

into:

select id, something, sum(p_sum) from tbl
group by something
having sum(p_sum) > 100

The former selects individual rows where the sum in that row is greater than 100. The latter groups on something then selects the grouped rows that have an aggregate summ greater than 100.

SELECT c.c_id, c.c_name, SUM( p.p_sum ) AS sum
FROM clients c, contracts_rent r, payments p
WHERE c.c_id = r.c_id
AND r.contract_id = p.contract_id 
GROUP BY c.c_id 
HAVING SUM(p.p_sum) > 1300

Make sure that you keep Joins in mind to get appropriate result.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top