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 8671425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:56:50+00:00 2026-06-12T18:56:50+00:00

I am trying to populate a single average based off a CASE statement utilizing

  • 0

I am trying to populate a single average based off a CASE statement utilizing a WHERE clause with MONTHS of a COUNT, however it is returning an invalid use of group function Error Code 1111:

SELECT 
count(ORDER_NUMBER) as cnt,
a.ITEM,
MONTH(TRANS_DATE) as Month,
AVG(CASE
WHEN count(ORDER_NUMBER) IS NULL THEN 0
WHEN count(ORDER_NUMBER) < 1 AND a.CAT != 'E' THEN 0
WHEN count(ORDER_NUMBER) < 1 AND a.CAT = 'E' THEN .05
WHEN count(ORDER_NUMBER) >= 1 AND count(ORDER_NUMBER) < 4 AND a.CAT = 'E' THEN .5
WHEN count(ORDER_NUMBER) >= 4 AND a.CAT = 'E' THEN 1
WHEN count(ORDER_NUMBER) <= 5 AND a.CAT != 'E' THEN .05
WHEN count(ORDER_NUMBER) >= 6 AND count(ORDER_NUMBER) < 11 AND a.CAT != 'E' THEN .25
WHEN count(ORDER_NUMBER) >= 11 AND count(ORDER_NUMBER) < 21 AND a.CAT != 'E' THEN .5
WHEN count(ORDER_NUMBER) >= 21 AND a.CAT != 'E' THEN 1 END) as TSCORE
FROM trx.TRX_Import t2
    JOIN ATTRIBUTES a
    ON a.ITEM = t2.ITEM
WHERE `TRANS_DATE` BETWEEN DATE_SUB(CURDATE(), INTERVAL 365 DAY) AND CURDATE()
AND t2.ITEM = 'WARI-BB150'

When I add the additional GROUP BY MONTH It correctly identifies each months TSCORE…I would rather it just spit out one AVERAGE of all those TSCORES.

With GROUP BY MONTH, it spits out:

   cnt     ITEM        Month   TSCORE
    1   WARI-BB150      1       0.5
    2   WARI-BB150      2       0.5
    1   WARI-BB150      3       0.5
    2   WARI-BB150      4       0.5
    2   WARI-BB150      5       0.5
    2   WARI-BB150      6       0.5
    4   WARI-BB150      7       1
    2   WARI-BB150      10      0.5
    3   WARI-BB150      11      0.5
    1   WARI-BB150      12      0.5

I would prefer the data to spit out just the average TSCORE of an item based of the above table (so only 1 row of info):

cnt   ITEM          TSCORE
20    WARI-BB150     0.55
  • 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-06-12T18:56:51+00:00Added an answer on June 12, 2026 at 6:56 pm

    I had to use a subquery in the FROM clause and create an AVG on that:

    SELECT
    t2.ITEM as Item,
    AVG(t2.TSCORE) as AvgTSCORE
    FROM 
    (SELECT count(ORDER_NUMBER) as cnt, t3.ITEM, MONTH(TRANS_DATE) as Month, 
    (CASE
    WHEN count(ORDER_NUMBER) IS NULL THEN 0
    WHEN count(ORDER_NUMBER) < 1 AND a.CAT != 'E' THEN 0
    WHEN count(ORDER_NUMBER) < 1 AND a.CAT = 'E' THEN .05
    WHEN count(ORDER_NUMBER) >= 1 AND count(ORDER_NUMBER) < 4 AND a.CAT = 'E' THEN .5
    WHEN count(ORDER_NUMBER) >= 4 AND a.CAT = 'E' THEN 1
    WHEN count(ORDER_NUMBER) <= 5 AND a.CAT != 'E' THEN .05
    WHEN count(ORDER_NUMBER) >= 6 AND count(ORDER_NUMBER) < 11 AND a.CAT != 'E' THEN .25
    WHEN count(ORDER_NUMBER) >= 11 AND count(ORDER_NUMBER) < 21 AND a.CAT != 'E' THEN .5
    WHEN count(ORDER_NUMBER) >= 21 AND a.CAT != 'E' THEN 1 END) as TSCORE
    FROM trx.TRX_Import t3
    JOIN ATTRIBUTES a
        ON a.ITEM = t3.ITEM
    WHERE `TRANS_DATE` BETWEEN DATE_SUB(CURDATE(), INTERVAL 365 DAY) AND CURDATE()
    AND t3.ITEM = 'WARI-BB150'
    GROUP BY MONTH
    ) t2
    

    Spits out:

    Item         AvgTSCORE
    WARI-BB150   0.55000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to populate a single record from a dataset into a listbox .
I have 2 1d arrays and im trying to populate them into a single
So, what I'm trying to do is populate a table, in this case called
I am trying to use CoreData in a non-document based (single-coordinator) application. I've searched
I am trying populate H3 with the sum of column E for rows that
I trying to populate the dropdown list when page was loaded.But it is not
I am trying to populate a List<Image> by reading the name of the each
I am trying to populate a ListView ....but I cant add 2 string values
I am trying to populate an array with the text value of a json
I am trying to populate an array with a path to a file. <?php

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.