Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6355797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:50:18+00:00 2026-05-24T22:50:18+00:00

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

  • 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]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:50:19+00:00Added an answer on May 24, 2026 at 10:50 pm

    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]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which stores the storecodes and their timezone. Now based on
table data of 2 columns category and subcategory i want to get a collection
table { id: long name: string } 1235 Fred 1902 Trever 5123 George 6467
I have a list of storecodes in table Stores and another table StoreClosedDates which
Table defintion is create table users ( serial_no integer PRIMARY KEY DEFAULT nextval('serial'), uid
Table has about 8 million rows. There is a non-unique index for X. Showing
Table Definition Model Definition Code using MyRandomizer.Models; namespace MyRandomizer { class Program { static
Table with Unique Clustered Index and an Indexed View are stored in same way.
Table Category (c) has a 1:many relationship with Table Question: a Category can have
Table one: products Which have fields like products_id products_price master_categories_id Table two: specials .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.