0
find_by_sql("SELECT s.productcode, s.description, CAST(SUM(l.amount) as UNSIGNED) AS amount FROM softwares s LEFT JOIN licenses l ON s.id=l.software_id GROUP BY s.productcode, s.description WHERE s.supplier= 'softcat'")

I currently have this, and i get the error ActiveRecord::StatementInvalid in SoftwaresController#export_softcat

I'm trying to add a where clause on the end to only select the records with a certain supplier.

3 Answers 3

3
SELECT s.productcode, s.description, CAST(SUM(l.amount) as UNSIGNED) AS amount
FROM softwares s
    LEFT JOIN licenses l ON s.id=l.software_id
WHERE s.supplier = 'softcat'
GROUP BY s.productcode, s.description

Try it this way

Sign up to request clarification or add additional context in comments.

Comments

2

You'll need to have the WHERE clause before the GROUP BY clause, effectively changing the SQL to be:

SELECT s.productcode, s.description, CAST(SUM(l.amount) as UNSIGNED) AS amount
FROM softwares s 
LEFT JOIN licenses l ON s.id=l.software_id
WHERE s.supplier= 'softcat'
GROUP BY s.productcode, s.description

Comments

2

You have to put the GROUP BY statement after the WHERE clause statement

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.