GROUP BY error message with IF statement

GROUP BY error message with IF statement

Hi, 

I'm reasonably new to coding so this may be a really simple fix (i hope anyway!)

I'm building a query table to identify cases for inclusion in a healthcare quality indicator as well as produce an output column that provides a true or false statement as to whether the indicator has been met. Query 1 below works fine but when I want to add another OR statement in the IF statement (shown in query 2 below query1) I get this: 
Error Details:

Query Error: Improper usage of GROUP BY clause.
Please ensure that all non-aggregate columns used in the SELECT clause are also used in GROUP BY clause.

**Query 1 - this works**

SELECT
jobs.id AS job_id,
patients.id AS patient_id,
jobs."date" AS "date",
strokes.id,
strokes."fast" AS "fast",
strokes.fast_neg AS fast_neg,
strokes.weak_side AS weakness,
strokes.face_side AS face_weakness,
strokes.resolved AS resolved,
strokes.speech AS speech,
SUM(observations.bm) AS total_bm,
MAX(bloodtests.value) AS bm,
SUM(observations.sbp) AS total_sbp,
primaries.total_gcs AS primary_gcs,
patients.mechanism_id AS mechanism_id,
jobs.cad_cat_id AS cadcat_id,
jobs.job_type AS job_type,
strokes.cb_inclusion AS cb_inclusion,
IF((strokes."fast"  = 'true'
OR strokes.weak_side  IS NOT NULL
OR strokes.face_side  IS NOT NULL
OR strokes.speech  IS NOT NULL)
AND ((SUM(observations.bm)  > 1)
OR MAX(bloodtests.value)  > 0)
AND (SUM(observations.sbp)  > 1), 'Complete CB', 'Incomplete CB') AS CB_completion
FROM  patients
JOIN jobs ON patients.job_id  = jobs.id 
LEFT JOIN strokes ON patients.id  = strokes.patient_id 
LEFT JOIN observations ON patients.id  = observations.patient_id 
LEFT JOIN bloodtests ON patients.id  = bloodtests.patient_id 
LEFT JOIN primaries ON patients.id  = primaries.patient_id  
WHERE (patients.mechanism_id  = '336'
 OR strokes.id  IS NOT NULL)
 AND (jobs."job_type"  != 'Interhospital transfer'
 AND jobs.cad_cat_id  != 44)
 AND strokes.cb_inclusion  != 'exclude'
 AND patients.id  NOT IN ( 2066  , 2115  , 3547  , 4045  , 4050  , 5782  , 5836  , 5910  )
GROUP BY jobs.id,
patients.id,
jobs."date",
strokes.id,
strokes."fast",
strokes.fast_neg,
strokes.weak_side,
strokes.face_side,
strokes.resolved,
strokes.speech,
primaries."total_gcs",
patients.mechanism_id,
jobs.cad_cat_id,
jobs.job_type,
  strokes.cb_inclusion 
ORDER BY jobs.id 


**Query 2: this produces an error**

SELECT
jobs.id AS job_id,
patients.id AS patient_id,
jobs."date" AS "date",
strokes.id,
strokes."fast" AS "fast",
strokes.fast_neg AS fast_neg,
strokes.weak_side AS weakness,
strokes.face_side AS face_weakness,
strokes.resolved AS resolved,
strokes.speech AS speech,
SUM(observations.bm) AS total_bm,
MAX(bloodtests.value) AS bm,
SUM(observations.sbp) AS total_sbp,
primaries.total_gcs AS primary_gcs,
patients.mechanism_id AS mechanism_id,
jobs.cad_cat_id AS cadcat_id,
jobs.job_type AS job_type,
strokes.cb_inclusion AS cb_inclusion,
IF((strokes."fast"  = 'true'
OR strokes.weak_side  IS NOT NULL
OR strokes.face_side  IS NOT NULL
OR strokes.speech  IS NOT NULL
         OR primaries.total_gcs <14)
AND ((SUM(observations.bm)  > 1)
OR MAX(bloodtests.value)  > 0)
AND (SUM(observations.sbp)  > 1), 'Complete CB', 'Incomplete CB') AS CB_completion
FROM  patients
JOIN jobs ON patients.job_id  = jobs.id 
LEFT JOIN strokes ON patients.id  = strokes.patient_id 
LEFT JOIN observations ON patients.id  = observations.patient_id 
LEFT JOIN bloodtests ON patients.id  = bloodtests.patient_id 
LEFT JOIN primaries ON patients.id  = primaries.patient_id  
WHERE (patients.mechanism_id  = '336'
 OR strokes.id  IS NOT NULL)
 AND (jobs."job_type"  != 'Interhospital transfer'
 AND jobs.cad_cat_id  != 44)
 AND strokes.cb_inclusion  != 'exclude'
 AND patients.id  NOT IN ( 2066  , 2115  , 3547  , 4045  , 4050  , 5782  , 5836  , 5910  )
GROUP BY jobs.id,
patients.id,
jobs."date",
strokes.id,
strokes."fast",
strokes.fast_neg,
strokes.weak_side,
strokes.face_side,
strokes.resolved,
strokes.speech,
primaries."total_gcs",
patients.mechanism_id,
jobs.cad_cat_id,
jobs.job_type,
  strokes.cb_inclusion 
ORDER BY jobs.id 

Please can you advise why it's giving me the error message?

    Access your files securely from anywhere

        All-in-one knowledge management and training platform for your employees and customers.







                              Zoho Developer Community




                                                    • Desk Community Learning Series


                                                    • Digest


                                                    • Functions


                                                    • Meetups


                                                    • Kbase


                                                    • Resources


                                                    • Glossary


                                                    • Desk Marketplace


                                                    • MVP Corner


                                                    • Word of the Day


                                                    • Ask the Experts



                                                              • Sticky Posts

                                                              • What's New in Zoho Analytics - January 2026

                                                                Hello Users! We are starting the year with a strong lineup of updates, marking the beginning of many improvements planned to enhance your analytics experience. Explore the latest improvements built to boost performance, simplify analysis, and help you
                                                              • What's New in Zoho Analytics - November 2025

                                                                We're thrilled to announce a significant update focused on expanding your data connectivity, enhancing visualization capabilities, and delivering a more powerful, intuitive, and performant analytics experience. Here’s a look at what’s new. Explore What's
                                                              • What's New in Zoho Analytics - October 2025

                                                                Hello Users! We're are back with a fresh set of updates and enhancements to make data analysis faster and more insightful. Take a quick look at what’s new and see how these updates can power up your reports and dashboards. Explore What's New! Extreme
                                                              • What’s New in Zoho Analytics – September 2025

                                                                Hello Users!! In this month’s update, we’re raising the bar across multiple touchpoints, from how you bring in data, plan and track projects to how you design and brand your dashboards. We’ve added the all-new Gantt chart for project visualization, expanded
                                                              • Announcing Agentic AI - Ask Zia!

                                                                We are delighted to roll out the new agentic AI capabilities in Ask Zia, where every stage of the BI workflow is assisted by AI. With a human-in-the-loop approach, Ask Zia ensures that you’re in command of the decision, while AI handles the complexity.


                                                              Manage your brands on social media



                                                                    Zoho TeamInbox Resources



                                                                        Zoho CRM Plus Resources

                                                                          Zoho Books Resources


                                                                            Zoho Subscriptions Resources

                                                                              Zoho Projects Resources


                                                                                Zoho Sprints Resources


                                                                                  Qntrl Resources


                                                                                    Zoho Creator Resources



                                                                                        Zoho CRM Resources

                                                                                        • CRM Community Learning Series

                                                                                          CRM Community Learning Series


                                                                                        • Kaizen

                                                                                          Kaizen

                                                                                        • Functions

                                                                                          Functions

                                                                                        • Meetups

                                                                                          Meetups

                                                                                        • Kbase

                                                                                          Kbase

                                                                                        • Resources

                                                                                          Resources

                                                                                        • Digest

                                                                                          Digest

                                                                                        • CRM Marketplace

                                                                                          CRM Marketplace

                                                                                        • MVP Corner

                                                                                          MVP Corner









                                                                                            Design. Discuss. Deliver.

                                                                                            Create visually engaging stories with Zoho Show.

                                                                                            Get Started Now


                                                                                              Zoho Show Resources

                                                                                                Zoho Writer

                                                                                                Get Started. Write Away!

                                                                                                Writer is a powerful online word processor, designed for collaborative work.

                                                                                                  Zoho CRM コンテンツ




                                                                                                    Nederlandse Hulpbronnen


                                                                                                        ご検討中の方