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

The Archive Base Latest Questions

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

I’m having trouble with a mySQL query. We have a table that tracks the

  • 0

I’m having trouble with a mySQL query.

We have a table that tracks the amount of times a user clicks. It is a single integer field that increases by 1 each click.

Schema:

+------------+--------------+------+-----+
| Field      | Type         | Null | Key | 
+------------+--------------+------+-----+
| id         | int(11)      | NO   | PRI | 
| arrival_id | int(11)      | NO   |     |
| clickouts  | int(11)      | NO   |     | 
+------------+--------------+------+-----+

Example Data:

id        arrival_id        clickouts
 5            22                0
 6            23                1
 7            24                7

If I want to determine the percentage of arrivals that generated at least one click, I can write the following query. (Note – not every arrival has a clickout, hence the LEFT JOIN):

SELECT SUM(click_tracker.clickouts > 0)/COUNT(DISTINCT arrivals.id) 
FROM arrivals 
LEFT JOIN click_tracker ON arrivals.id = click_tracker.arrival_id;

The issue comes in when I also LEFT JOIN arrivals to another table (not click_tracker) during the same query.

I have another table called advertisements, that tracks various types of ads a user will see. The ads are tied to an arrival, and an arrival can have many ads. The schema is as follows:

+------------+--------------+------+-----+
| Field      | Type         | Null | Key | 
+------------+--------------+------+-----+
| id         | int(11)      | NO   | PRI | 
| arrival_id | int(11)      | NO   |     |
| ad_type    | varchar(255) | NO   |     | 
+------------+--------------+------+-----+

Now, I want to find the percentage of arrivals with ad_type = “ZXY” along with the percentage of arrivals that generated at least one click (regardless of the ad_type). Doing the following won’t work:

SELECT COUNT(DISTINCT advertisements.id)/COUNT(DISTINCT arrivals.id) AS ad_pct,
SUM(click_tracker.clickouts > 0)/COUNT(DISTINCT arrivals.id) AS click_pct
FROM arrivals
LEFT JOIN advertisements ON arrivals.id = advertisements.arrival_id
LEFT JOIN click_tracker ON arrivals.id = click_tracker.arrival_id

The LEFT JOIN of the advertisements table creates a result set duplicate arrival id’s. Thus, when I subsequently LEFT JOIN onto the click_tracker table, I get duplicate click_tracker id’s as well.

I need a way to do SUM(click_tracker.clickouts > 0) for distinct click_tracker rows only. I tried SUM(DISTINCT click_tracker.clickouts > 0) but that did not give me the correct results.

  • 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-17T07:59:29+00:00Added an answer on June 17, 2026 at 7:59 am

    I hate to say it, but those two calculations are completely independent, and I think you’ll need to execute two queries for them. Here they are (and an sqlfiddle with the data I used):

    Percentage of arrivals by ad_type:

    SELECT
      ad_type,
      (COUNT(arrival_id) / x.total) as ad_pct
      FROM advertisements, 
           (SELECT COUNT(id) AS total FROM arrivals) x
      GROUP BY ad_type;
    

    (Or you could change the GROUP BY to a WHERE clause for a single ad_type.)

    Percentage of arrivals with at least one click:

    SELECT
      (COUNT(id) / x.total) AS click_pct
      FROM click_tracker,
           (SELECT COUNT(id) AS total FROM arrivals) x
      WHERE click_tracker.clickouts > 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.