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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:45:36+00:00 2026-06-13T10:45:36+00:00

I am running the following query which works except when there are duplicate records

  • 0

I am running the following query which works except when there are duplicate records (duplicate except for the id (primary key, int) field and the modified (timestamp) columns, there are many columns which get duplicated.
The dups are a result of people hitting a submit button multiple times on a form (gotta fix that too, but thats another issue).
Here is the query:

$sql = "SELECT COUNT(id) AS rcount,
    SUM(num_guests) AS gcount,
    DATE_FORMAT(modifed, '%%m-%%Y') AS adate,
    SUM(1st_visit REGEXP '^no') AS repeat_guest
    FROM reservation_stats
    WHERE establishmentid = %i
    AND num_guests > 0
    GROUP BY adate
    ORDER BY added";

I don’t want the SUM to include the duplicates. I am using mysql and php.

Thanks

PS the table structure:

 id     int(10)          
establishmentid     int(11)         
name    varchar(50)     
email   varchar(100)    
phone   varchar(30)     
num_guests  int(11)         

reservation_time    varchar(50)     
reservation_date    date            
1st_visit   varchar(10)     
data    text    
added   date        
modifed     timestamp 

PPS: My query is now after Tom’s suggestion:

 $sql = "SELECT COUNT(*) as rcount, SUM(num_guests) AS gcount, 
DATE_FORMAT(added, '%%m-%%Y') AS adate, 
1 as repeat_guest 
FROM 
( SELECT added, name, email, phone, num_guests, 
  reservation_time, reservation_date 
FROM reservation_stats where establishmentid = %i 
GROUP BY added, name, email, phone, num_guests, 
  reservation_time, reservation_date 
\) dup 
group by adate 
order by added";

Thanks Tom. This works except I lose the repeat_guest data from the original query. How can I preserve it?

PPPS: wait, how about this:

        $sql = "SELECT COUNT(*) as rcount, SUM(num_guests) AS gcount, DATE_FORMAT(added, '%%m-%%Y') AS adate,    SUM(repeat_guest)                       
            FROM
            (
             SELECT  added, name, email, phone, num_guests, reservation_time, reservation_date, SUM(1st_visit REGEXP '^no') AS repeat_guest
             FROM    reservation_stats
             where establishmentid = %i
             GROUP BY added, name, email, phone, num_guests, reservation_time, reservation_date
            ) dup
            group by adate order by added";

seems to work but is it doing what I think it is?

  • 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-13T10:45:38+00:00Added an answer on June 13, 2026 at 10:45 am

    You could do something like this, where FIELD1, FIELD2 and FIELD3 are the fields that may be duplicated (and num_guests):

    SELECT COUNT(*) rcount, SUM(num_guests) AS gcount
    
    FROM
    (
     SELECT  FIELD1, FIELD2, FIELD3, num_guests
     FROM    YOUR_TABLE
     GROUP BY FIELD1, FIELD2, FIELD3, num_guests
    ) dup
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following mysql query which I am running with php like so.
Hi I have the following script which works fine running on IIS but when
I'm running the following query on my database, which generates a sql query I
I'm running the following query, which tells me which agents have ever been scheduled
I am running following query. SELECT T1.C1, T2.C2..., IF( T1.C1<>T2.C1,Changed,1) AS NewColumn From T1
I am running following query.. Select T1.* from T1 LEFT JOIN T2 ON T1.C1
I am running following query in SQL*Plus CREATE TABLE tbl_audit_trail ( id NUMBER(11) NOT
I am running the following query and checking whether it returns any results and
I am running following LINQ query in order to group by CategoryID var test
I tried running the following query to restore a database: restore database myDatabase from

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.