0

My table StoreCodes holds unique products codes (and their availablity) for each store, eg:

category:integer
code:string
available:boolean
store_id:reference

I'm trying to show a simple table with 3 columns showing how many codes are in each category, AND how many of those codes have available = true

Category_number / Count_Codes_in_category / Count_Codes_in_category_available==TRUE

In my controller, the following attempt to COUNT and GROUP on both category and available throws this error:

SQLite3::SQLException: near "WHERE": syntax error: SELECT category, available, COUNT(*) AS instances GROUP BY category, available WHERE (store_id = 8)

My controller code:

@b2 = StoreCode.find_by_sql ['SELECT category, available, COUNT(*) AS instances GROUP BY category, available WHERE (store_id = ?)', @store.id]

2 Answers 2

1

Your SQL is wrong. You have to put the GROUP BY clause after the WHERE clause, and when using find_by_sql you also need to write the complete statement (include the FROMclause)

Like this:

@b2 = StoreCode.find_by_sql ['SELECT category, available, COUNT(*) AS instances 
FROM store_codes WHERE (store_id = ?) GROUP BY category, available', @store.id]
Sign up to request clarification or add additional context in comments.

Comments

1

Your SQL statement has no 'FROM' clause, I also think the 'GROUP BY' should come after the 'WHERE'.

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.