简体   繁体   中英

customer lifetime value with mysql php

I have spent a week trying to figure this out, it is somewhat working by combining things from various sources but not fully working yet.

Basically I have an orders table which I'm trying to group customers by their first order date, then show the total spent by this group up to now.

This is my SQL query:

SELECT DISTINCT email, billing_name,
FORMAT(SUM(total),2) AS total,
DATE_FORMAT(MIN(orderdate), '%Y-%m') AS firstorder,
DATE_FORMAT(MAX(orderdate), '%Y-%m') AS lastorder
FROM orders
GROUP BY email
ORDER BY firstorder ASC

and with PHP I am doing:

$rows = array();
while($row = mysql_fetch_array($query))
$rows[] = $row;
foreach($rows as $row) {
    $currMonth = $row['firstorder'];
    $total += $row['total'];
    if ($currMonth != $prevMonth) {
            echo $currMonth.' = $'.$total';
            $prevMonth = $currMonth;
            $total = 0;
        }
    }

this gives me a list like:

    2010-05 = $230.49
    2010-06 = $557.32
    2010-08 = $223.38

but the numbers don't add up, what am i doing wrong? and how can I display how much a group have spent in other months? this is how I eventually want to show the data, http://www.quickcohort.com/

Please help! thanks!!

Depends on what you're really after and what your data looks like.

If data looks like:

email          |BILLING NAME|Total  |OrderDate
----------------------------------------------
john@gmail.com |John Smith  |200.00 |15/05/2010
john@gmail.com |John Smith  | 15.49 |19/10/2010
john@gmail.com |Rico Swavez | 15.00 |10/08/2010
jane@gmail.com |Jane Doe    |250.00 |23/06/2010
jane@gmail.com |Jane Doe    |307.32 |27/10/2010
juan@gmail.com |Juan Valdez |223.38 |30/08/2010

Then...

SELECT email, billing_name,
FORMAT(SUM(total),2) AS total,
DATE_FORMAT(MIN(orderdate), '%Y-%m') AS firstorder,
DATE_FORMAT(MAX(orderdate), '%Y-%m') AS lastorder
FROM orders
GROUP BY email, billing_name
ORDER BY firstorder ASC

Will return

EMAIL          | BILLING NAME |TOTAL |FIRSTORDER | LASTORDER 
------------------------------------------------------------
john@gmail.com | John Smith   |215.49|2010-05    | 2010-10
jane@gmail.com | Jane Doe     |557.32|2010-06    | 2010-10
john@gmail.com | Rico Swavez  | 15.00|2010-08    | 2010-08
Juan@gmail.com | Juan Valdez  |223.38|2010-08    | 2010-08

Run your query first in mysql are you getting the results you want? if not, then the problem is in the SQL, not the PHP. If the SQL is returning what you want, then the problem is in the PHP

The following query should do the trick:

SELECT 
    FORMAT(SUM(z.`totalSpent`),2) AS cohortRevenueToDate, 
    z.`firstOrderMonth`,
    GROUP_CONCAT(`email`) 
FROM 
(
    SELECT 
        `email`,
        SUM(`total`) AS totalSpent, 
        DATE_FORMAT(MIN(`orderdate`), '%Y-%m') AS firstOrderMonth 
    FROM `orders`
    GROUP BY `email`
) AS z
GROUP BY z.`firstOrderMonth`
ORDER BY z.`firstOrderMonth` ASC

I have included a GROUP_CONCAT in case you are interested on each cohort composition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM