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

  • Home
  • SEARCH
  • 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 7983409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:56:55+00:00 2026-06-04T10:56:55+00:00

In my current table i am trying to get average of columns based on

  • 0

In my current table i am trying to get average of columns based on comp_name and below is the output

    "Comp_Name"         "No_of_Rows"    "Column1_Avg"   "Column2_Avg"   "Column3_Avg"
    "Company1 Pty Ltd"  "291"           "39"            "60"            "0"
    "Company1 Pty."     "1699"          "23"            "76"            "0"
    "Company2 Ltd"      14335"          "6"             "82"            "10"
    "Company2 "         "4335"          "60"            "8"             "2"
    "Company3 Pty Ltd"  "767"           "22"            "77"            "0"
    "Company3"          "1628"          "16"            "82"            "1"

Is is possible to average “Company1 Pty Ltd” and “Company1 Pty.” (and for the other companies) but add the number of rows?

My select query is below and basically it is calculating average based on a certain value and grouping based on the company name available in the table

    SELECT Comp_Name,count(*) as No_of_Rows,
    CAST(   (COUNT(CASE WHEN Column1 < 500 then 1 else NULL end)/COUNT(mytable.ID)) * 100 AS CHAR(2))+'%' as Column1_Avg,
    CAST(   (COUNT(CASE WHEN (Column1 < 30000 AND Column1 > 500) then 1 else NULL end)/COUNT(mytable.ID)) * 100 AS CHAR(2))+'%' as Column2_Avg,
    CAST(   (COUNT(CASE WHEN (Column1 > 30000)  then 1 else NULL end)/COUNT(mytable.ID)) * 100 AS CHAR(2))+'%' as Column3_Avg
    FROM mytable
    GROUP BY Comp_Name desc

Expected output:

    "Comp_Name"         "No_of_Rows"    "Column1_Avg"   "Column2_Avg"   "Column3_Avg"
    "Company1"          "1990"          "31"            "68"            "0"
    "Company2"          "18670"         ".."            ".."            "6"
    "Company3"          "2395"          ".."            ".."            ".."

Can i use some sort of reference table with a list of company_name and it’s substitution?

  • 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-04T10:56:57+00:00Added an answer on June 4, 2026 at 10:56 am

    If you want to only take the first word in your GROUP BY clause, you can use:

    GROUP BY CASE LOCATE(' ', Comp_Name) WHEN 0 THEN Comp_Name ELSE LEFT(Comp_Name, LOCATE(' ', Comp_Name)) END
    

    Then, if you want to build a reference table, a query like this should be fine:

    SELECT DISTINCT Comp_Name, CASE LOCATE(' ', Comp_Name) WHEN 0 THEN Comp_Name ELSE LEFT(Comp_Name, LOCATE(' ', Comp_Name)) END AS Simple_Comp_Name
    FROM mytable
    ORDER BY Simple_Comp_Name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get the current longitude and latitude right before the table
How do you create a moving average in SQL? Current table: Date Clicks 2012-05-01
I have an exchange rate table. I need to get current rate and previous
I am trying to get data from my sql server data table to a
I'm trying to get my current program to take information from a dynamically created
I'm trying to get around pulling all the data from a table, and cycling
I'm trying to get the ip, user, and most recent timestamp from a table
I have a jobs table, and am trying to get a count of jobs
I was trying to get the distinct result from my table, and people said
I'm trying to get a report from my history table by hourly usage. history

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.