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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:21:27+00:00 2026-05-31T18:21:27+00:00

I need a little help brainstorming on a query. I have a sales table

  • 0

I need a little help brainstorming on a query. I have a sales table that has two rows (per day) for each restaurant location. One row is AM sales, and the other is PM sales. The AM sales is literally just the AM sales, however, the PM sales is a combination of the AM sales and the PM sales. So in order to extract the PM sales, you have to subtract the AM sales row from it.

---------------------------------------------------------
   date    | id | meridiem | daily_sales | cover_counts |
---------------------------------------------------------
2012-03-22 |103 |   AM     |    2956.32  |    175       |
2012-03-22 |103 |   PM     |   12124.62  |    484       |
---------------------------------------------------------

I have a query to calculate the projected lunch per person average (which are from the AM sales), and it works great. It basically extracts the first four weeks, from the last 6 weeks of sales data, and averages them. (Note: Per person average is calculated by dividing sales by covers).

$statement = "SELECT directory.location, ROUND((SUM(sales.daily_sales / sales.cover_counts) / 4), 2) AS lppa
              FROM t_directory directory
              LEFT JOIN t_sales sales
              ON sales.site_id = directory.site_id
              AND DAYOFWEEK(sales.business_date) = DAYOFWEEK(:date1)
              AND sales.business_date < DATE_SUB(:date2, INTERVAL 14 DAY)
              AND sales.business_date >= DATE_SUB(:date3, INTERVAL 42 DAY)                
              AND sales.meridiem = 'AM'
              WHERE directory.active = 1
              GROUP BY directory.site_id
              ORDER BY directory.site_id ASC
              LIMIT :totalLocations";

$statementHandle = $this->dbHandle->prepare($statement);
$statementHandle->bindValue(':date1', $this->date, PDO::PARAM_STR);
$statementHandle->bindValue(':date2', $this->date, PDO::PARAM_STR);
$statementHandle->bindValue(':date3', $this->date, PDO::PARAM_STR);
$statementHandle->bindValue(':totalLocations', $this->totalLocations, PDO::PARAM_INT);
$statementHandle->execute();
$lppa = $statementHandle->fetchAll(PDO::FETCH_ASSOC);

Now I want to calculate the dinner per person average. In order to do this, I would have to subtract the AM sales and covers from the PM sales and covers, respectively, before any of the math takes place. Is there a somewhat simple way to implement this all into one query? I can obviously do this with PHP, but I’m just curious as to how this can be accomplished in a query. Any help or insight would be greatly appreciated. Please let me know if any more detail is needed.

  • 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-05-31T18:21:28+00:00Added an answer on May 31, 2026 at 6:21 pm

    You could do to join to t_sales again and then filter for ‘PM’

     SELECT DIRECTORY.location, 
           Round(( SUM(salesPM.daily_sales - salesAM.daily_sales / 
                       salesPM.cover_counts - salesAM.cover_counts) / 4 ), 2)
                          AS lppaPM
    FROM   t_directory DIRECTORY 
           LEFT JOIN t_sales salesAM
             ON salesAM.site_id = DIRECTORY.site_id 
                AND Dayofweek(salesAM.business_date) = Dayofweek(:date1) 
                AND salesAM.business_date < DATE_SUB(:date2, INTERVAL 14 DAY) 
                AND salesAM.business_date >= DATE_SUB(:date3, INTERVAL 42 DAY) 
                AND salesAM.meridiem = 'AM' 
           LEFT JOIN t_sales salespm 
             ON salesAM.site_id = salesPM.site_id 
                AND salesAM.business_date = salesPM.business_date 
                AND salespm.meridiem = 'PM' 
    WHERE  DIRECTORY.active = 1 
    GROUP  BY DIRECTORY.site_id 
    ORDER  BY DIRECTORY.site_id ASC 
    LIMIT  :totalLocations
    

    Note: this solution assumes that

    • {Site_ID, business_Date, meridiem} is unique
    • there’s never a PM record without an AM record
    • salesPM.cover_counts – salesAM.cover_counts never equals zero
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need a little help with a SQL / ActiveRecord query. Let's say I have
need a little help with this one. I have a form that I am
I have started to study UML standard and would need little help with two
I need a little help, I have a report that I am running that
Ok, I need a little help. I have these two PHP classes class menu
I need a little help on this subject. I have a Web application written
I need little help with inheritance in C++. I have code with same structure
I need a little help on a problem with navigation controllers. I have a
I need a little help with my new assignment. Problem: Given Two Linked List
I need a little help. I want to built a user interactive site that

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.