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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:59:17+00:00 2026-06-10T09:59:17+00:00

I need to add an extra column to my query results, The column needs

  • 0

I need to add an extra column to my query results,

The column needs to be called percent,
If col1 == 0 then percent should contain NIS else percent should contain ceil(col2 / col1 * 100)

So I believe the following should work:

IF(col1 = 0, 'NIS', ceil(col2 / col1 * 100)) as percent

But I run into an issue as col1 and col2 are also composite’s.

COUNT(distinct i.id) as col1
COUNT(distinct q.id) as col2

So I get hit with

Unknown column ‘col1’ in ‘field list’

I could get around that issue with
IF(COUNT(distinct i.id) = 0, 'NIS', ceil(COUNT(distinct q.id) / COUNT(distinct i.id) * 100)) as percent

But that just seems like a bunch of extra processing to me, surely there is a better way around that?

Full Query

SELECT 
  `t`.*,
  `r`.`name` AS region_name,
  GROUP_CONCAT(DISTINCT p.ptype_id SEPARATOR "|") AS ptype_ids,
  COUNT(DISTINCT q.id) AS quoted,
  COUNT(DISTINCT i.id) AS intended,
  COUNT(DISTINCT qa.id) AS awarded,
  IF(
    intended = 0,
    `"NIS"`,
    CEIL(quoted / intended * 100)
  ) AS percent 
FROM
  (`tradesmen` t) 
  LEFT JOIN `regions` r 
    ON `r`.`id` = `t`.`region_id` 
  LEFT JOIN `quotes` q 
    ON `t`.`id` = `q`.`tradesman_id` 
  LEFT JOIN `quote_intentions` i 
    ON `t`.`id` = `i`.`tradesman_id` 
  LEFT JOIN `quotes` qa 
    ON `q`.`tradesman_id` = `qa`.`tradesman_id` 
    AND qa.accepted = 1 
  LEFT JOIN `ptypes_tradesmen` p 
    ON `p`.`tradesman_id` = `t`.`id` 
GROUP BY `t`.`id` 
LIMIT 20 
  • 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-10T09:59:18+00:00Added an answer on June 10, 2026 at 9:59 am

    As mentioned in SELECT Syntax:

    It is not permissible to refer to a column alias in a WHERE clause, because the column value might not yet be determined when the WHERE clause is executed. See Section C.5.5.4, “Problems with Column Aliases”.

    Whilst the manual doesn’t explicitly say so, the same reasoning applies to referring to a column alias within a select_expr.

    You could place your query in a subquery to an outer one that calculates percent using the column aliases:

    SELECT *, IF(intended, CEIL(quoted / intended * 100), NULL) AS percent FROM (
      SELECT 
        `t`.*,
        `r`.`name` AS region_name,
        GROUP_CONCAT(DISTINCT p.ptype_id SEPARATOR "|") AS ptype_ids,
        COUNT(DISTINCT q.id)  AS quoted,
        COUNT(DISTINCT i.id)  AS intended,
        COUNT(DISTINCT qa.id) AS awarded
      FROM
        (`tradesmen` t) 
        LEFT JOIN `regions` r 
          ON `r`.`id` = `t`.`region_id` 
        LEFT JOIN `quotes` q 
          ON `t`.`id` = `q`.`tradesman_id` 
        LEFT JOIN `quote_intentions` i 
          ON `t`.`id` = `i`.`tradesman_id` 
        LEFT JOIN `quotes` qa 
          ON `q`.`tradesman_id` = `qa`.`tradesman_id` 
          AND qa.accepted = 1 
        LEFT JOIN `ptypes_tradesmen` p 
          ON `p`.`tradesman_id` = `t`.`id` 
      GROUP BY `t`.`id` 
      LIMIT 20
    ) t
    

    However, I don’t think this is really worthwhile as I believe (am looking for a reference that I can cite, but nothing forthcoming yet) MySQL will only calculate each COUNT() once and use the cached result in each reference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to add extra validation to my DateField in Admin to make sure
I use a jquery validation plugin and I need to add some extra checking,
I need to use the org.osgi.framework.system.packages.extra property to add service interfaces at runtime. These
I need add Auto complete on apex Tabular Form. there is a column as
I have just changed a column (called time) from t.string to t.datetime and then
I am working on a report and need to add a column to one
I want know how I can add an extra column to a Join based
I need add a new user group for mediawiki. The new group has more
I can't access a element with its href. Like this example, i need add
Hї! I have wrote test for my application. I need add item to database

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.