SUM function not working

Hi, I am having problem with SUM that is not being recognized. It’s not blue and its not working. Any advice? Thanks

Hi.

Can you provide more context (so that anyone could help)?

What flavour of SQL is it ?
What tools are you using ?
Can you provide an example ?

Cheers.

Hi, I got help. Person send me this to try and it worked. SUM is still grey but it’s working.

SELECT
‘First half of 2019’ AS date_range,
SUM(invoice_total) AS total_sales,
SUM(payment_total) AS total_payments,
SUM(invoice_total - payment_total) AS what_we_expect
FROM
sql_invoicing.invoices
WHERE invoice_date
BETWEEN ‘2019-01-01’ AND ‘2019-06-30’
UNION
SELECT
‘Second half of 2019’ AS date_range,
SUM(invoice_total) AS total_sales,
SUM(payment_total) AS total_payments,
SUM(invoice_total - payment_total) AS what_we_expect
FROM
sql_invoicing.invoices
WHERE invoice_date
BETWEEN ‘2019-07-01’ AND ‘2019-12-31’
UNION
SELECT
‘Total’ AS date_range,
SUM(invoice_total) AS total_sales,
SUM(payment_total) AS total_payments,
SUM(invoice_total - payment_total) AS what_we_expect
FROM
sql_invoicing.invoices
WHERE invoice_date
BETWEEN ‘2019-01-01’ AND ‘2019-12-31’;

And thank you for sending message.

OK

You can compare your non-working version with the working one to try find what was the cause of the error.

Cheers.

1 Like

Whenever use aggregate function, we must have to use group by clause which seems missing in your case. What exact error you are experiencing?

Not Always.

GROUP BY is when you want to … group by a criteria.
HAVING is the WHERE clause to aggregate functions.

image

I am working with SQL Server while you likely work with MySQL, but that’s broadly the same.

The query above is the working one or the broken one ?

Anyway, next time such situation happen, you could try each query separately to see which one(s) is(are) broken. Then try to fix them individually and put them back when they work.

1 Like