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?
You could do something like this, where FIELD1, FIELD2 and FIELD3 are the fields that may be duplicated (and num_guests):