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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:15:01+00:00 2026-06-14T03:15:01+00:00

Here’s a challenge for a good MySQL coder. We would greatly appreciate some help

  • 0

Here’s a challenge for a good MySQL coder.
We would greatly appreciate some help on a query to create a MySQL report displayed in an html/PHP page.

We know how to collect the data and store it, and have the PHP report layout, but are lost as to how to structure the query.

We have two MySQL tables – “visitors” and “buyers”.

  1. Visitors table – for visitors to our website coming in through a
    particular URL which identifies the sales_rep and one of many
    marketing codes they create themselves.
  2. Buyers table – for those who buy something.

A visitor may visit our website through the following link, for example:

www.sales_rep.website.com/index.php?marketing=code1/code2/code3

In the “visitors” table we include three fields:

  • sales_rep
  • date
  • marketing_CODE – (which in this case would be the “code1/code2/code3” in a string, but each sales_rep could have several marketing_CODES based on their advertising activities; such as “code1a/codeTX/codeM2” and “market-z/ad-123”)

When a visitor buys something, we add them to a “buyers” table, which includes:

  • sales_rep
  • date
  • marketing_TYPE – (which can be “URL”, “referral”, or some other type)
  • marketing_VALUE – (which will the marketing_CODE if the marketing_TYPE is URL)

We need to see a report for each specific sales rep, for any specified month, sorted by marketing code, only for marketing_TYPE == “URL”.

This report needs to show a week-by-week breakdown of visitors and buyers activities for each marketing_CODE, as follows:

Table Example

To ensure clarity, in the above table, in WEEK 1, 8 Visitors came into the website through a link with “code1/code2/”, and 5 of them purchased something.

We’ve attempted to begin our query with the following, but don’t know where to go with it:

  1. $chosen_month = $_POST['chosen_month'];
  2. SELECT * FROM visitors INNER JOIN buyers ON sales_rep WHERE marketing_type = "URL" AND sales_rep = '$sales_rep' AND date('Y-m') = $'chosen_month' ;
  3. Using a while statement, we would generate the table cells for each of the records found. Using this, we know how to list every record, but we can’t figure out how display non-distinct records once, display their count, then display distinct records.

Thank you for your time and help.

  • 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-14T03:15:02+00:00Added an answer on June 14, 2026 at 3:15 am

    Here’s my suggestion for your query.

    First of all, I needed to find out the week number based on the date field so I used the answer in this question:

    SELECT  WEEK(dateField, 5) -
            WEEK(DATE_SUB(dateField, INTERVAL DAYOFMONTH(dateField) - 1 DAY), 5) + 1
    

    I’ll refer to the the result of the above as @week from now on to make things more readable 🙂

    Then, I started out with just one of the tables, trying to see how many visitors a sales rep got in each month:

    select rep, 
      , SUM(CASE @week WHEN 1 THEN 1 ELSE 0 END) as Week1
      , SUM(CASE @week WHEN 2 THEN 1 ELSE 0 END) as Week2
      , SUM(CASE @week WHEN 3 THEN 1 ELSE 0 END) as Week3
      , SUM(CASE @week WHEN 4 THEN 1 ELSE 0 END) as Week4
      , COUNT(*) as Total
    from visitors
    group by rep
    

    Now that we got this info, we’ll use the same for buyers and then join the results of these two subqueries as follows:

    select v.rep
    , v.Week1 , b.Week1
    , v.Week2 , b.Week2
    , v.Week3 , b.Week3
    , v.Week4 , b.Week4
    , v.Total, b.Total
    from (
         select rep, 
            , SUM(CASE @week WHEN 1 THEN 1 ELSE 0 END) as Week1
            , SUM(CASE @week WHEN 2 THEN 1 ELSE 0 END) as Week2
            , SUM(CASE @week WHEN 3 THEN 1 ELSE 0 END) as Week3
            , SUM(CASE @week WHEN 4 THEN 1 ELSE 0 END) as Week4
            , COUNT(*) as Total
         from visitors
         group by rep) as v
         left join (select rep, 
                     , SUM(CASE @week WHEN 1 THEN 1 ELSE 0 END) as Week1
                     , SUM(CASE @week WHEN 2 THEN 1 ELSE 0 END) as Week2
                     , SUM(CASE @week WHEN 3 THEN 1 ELSE 0 END) as Week3
                     , SUM(CASE @week WHEN 4 THEN 1 ELSE 0 END) as Week4
                     , COUNT(*) as Total
                    from visitors
                    group by rep) as b on v.rep = b.rep
    

    This should be what you’re looking for!

    Here’s the same code as above updated for your situation:

    select v.rep
        , v.Week1 , b.Week1
        , v.Week2 , b.Week2
        , v.Week3 , b.Week3
        , v.Week4 , b.Week4
        , v.Total, b.Total
    from (
            select rep
              , SUM(CASE WEEK(the_date, 5) -
                WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
                WHEN 1 THEN 1 ELSE 0 END) as Week1
          , SUM(CASE WEEK(the_date, 5) -
                WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
                WHEN 2 THEN 1 ELSE 0 END) as Week2
          , SUM(CASE WEEK(the_date, 5) -
            WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
                WHEN 3 THEN 1 ELSE 0 END) as Week3
          , SUM(CASE WEEK(the_date, 5) -
            WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
                WHEN 4 THEN 1 ELSE 0 END) as Week4
          , COUNT(*) as Total
            from visitors
            where sales_rep = '$sales_rep' AND date('Y-m') = $'chosen_month'
            group by rep ) as v
      left join (
            select rep
              , SUM(CASE WEEK(the_date, 5) -
                WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
                WHEN 1 THEN 1 ELSE 0 END) as Week1
          , SUM(CASE WEEK(the_date, 5) -
                WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
                WHEN 2 THEN 1 ELSE 0 END) as Week2
          , SUM(CASE WEEK(the_date, 5) -
            WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
                WHEN 3 THEN 1 ELSE 0 END) as Week3
          , SUM(CASE WEEK(the_date, 5) -
            WEEK(DATE_SUB(the_date, INTERVAL DAYOFMONTH(the_date) - 1 DAY), 5) + 1
                WHEN 4 THEN 1 ELSE 0 END) as Week4
          , COUNT(*) as Total
            from buyers
            where marketing_type = "URL" AND sales_rep = '$sales_rep' AND date('Y-m') = $'chosen_month'
            group by rep) as b on v.rep = b.rep
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is my SQL script CREATE TABLE tracks( track_id int NOT NULL AUTO_INCREMENT, account_id
Here's some of my production code (I had to force line breaks): task =
Here's my procedure: DROP PROCEDURE IF EXISTS `couponExpires`$$ CREATE DEFINER=`root`@`localhost` PROCEDURE `couponExpires`(IN couponID BIGINT,
Here is my problem : I have a post controller with the action create.
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here is my problem. Our tech. writer or customer service will often create new
Here are some facts about my app followed by a question My app has
Here is my problem.. I have the option to create(add) two new input fields,
Here is my error(if you need any more info just ask)- Error SQL query:

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.