MySQl Subquery output with null data - mysql

I have a MySQL database with vehicles, issued_fuel and mileage data (vs issued fuel) and want to get the following desired output.
Desired Output
I want to get results including above related data as follows :
+---------------+------------+------------+---------+----------+
| registered_no | issue_date | date | mileage | fuel_qty |
+---------------+------------+------------+---------+----------+
| SP KR-3503 | 2021-02-22 | null | null | 40 |
| SP KR-3503 | 2021-02-26 | null | null | 30 |
| null | 2021-03-03 | null | null | 40 |
| null | 2021-03-15 | null | null | 45 |
| SP KR-3503 | 2021-03-18 | null | null | 40 |
| null | 2021-03-25 | null | null | 45 |
| null | 2021-04-04 | null | null | 35 |
| SP KE-6794 | 2021-04-25 | 2021-04-25 | 150 | 40 |
+---------------+------------+------------+---------+----------+
Because the table "tbl_trip_details" includes only one mileage (150km) for vehicle_no "SP KE-6794 for the date 2021-04-25.
My tables as follows :
tbl_vehicle
+------------+---------------+--------+
| vehicle_id | registered_no | status |
+------------+---------------+--------+
| 1 | SP KR-3503 | 1 |
| 2 | SP KE-6794 | 1 |
+------------+---------------+--------+
tbl_trip_details
+---------+------------+------------+---------+--------+
| trip_id | vehicle_id | date | mileage | status |
+---------+------------+------------+---------+--------+
| 1 | 1 | 2021-04-25 | 125 | 1 |
| 2 | 1 | 2021-04-26 | 100 | 1 |
| 3 | 2 | 2021-04-25 | 150 | 1 |
+---------+------------+------------+---------+--------+
tbl_direct_fuel
+----------------+---------+------------+
| direct_fuel_id | vehicle | issue_date |
+----------------+---------+------------+
| 1 | 1 | 2021-02-22 |
| 2 | 1 | 2021-02-26 |
| 3 | 3 | 2021-03-03 |
| 4 | 3 | 2021-03-15 |
| 5 | 1 | 2021-03-18 |
| 6 | 3 | 2021-03-25 |
| 7 | 3 | 2021-04-04 |
| 8 | 2 | 2021-04-25 |
+----------------+---------+------------+
tbl_direct_fuel_details
+-----------------------------+----------------+------+----------+
| tbl_deirect_fuel_details_id | direct_fuel_id | item | fuel_qty |
+-----------------------------+----------------+------+----------+
| 1 | 1 | 1 | 40 |
| 2 | 2 | 1 | 30 |
| 3 | 3 | 1 | 40 |
| 4 | 4 | 1 | 45 |
| 5 | 5 | 1 | 40 |
| 6 | 6 | 1 | 45 |
| 7 | 7 | 1 | 35 |
| 8 | 8 | 1 | 40 |
+-----------------------------+----------------+------+----------+
I used the following query
select tv.registered_no, df.issue_date, td.date, td.mileage, df.fuel_qty
from tbl_trip_details trip
left join tbl_vehicle tv on tv.vehicle_id=trip.vehicle_id
join (select tbl_direct_fuel.direct_fuel_id, tbl_vehicle.registered_no, tbl_direct_fuel.vehicle, tbl_direct_fuel.issue_date, item, tbl_direct_fuel_details.fuel_qty, tbl_direct_fuel_details.fuel_price
from tbl_direct_fuel_details
left join tbl_direct_fuel on tbl_direct_fuel_details.direct_fuel_id = tbl_direct_fuel.direct_fuel_id
join tbl_vehicle on tbl_vehicle.vehicle_id = tbl_direct_fuel.vehicle
where tbl_direct_fuel.status=1
order by tbl_vehicle.registered_no
) AS df on df.vehicle = tv.vehicle_id
join (select tbl_trip_details.trip_id, tbl_vehicle.registered_no, tbl_trip_details.vehicle_id, tbl_trip_details.date, tbl_trip_details.mileage
from tbl_trip_details
left join tbl_vehicle on tbl_vehicle.vehicle_id = tbl_trip_details.vehicle_id
where tbl_trip_details.status=1
order by tbl_vehicle.registered_no) AS td on td.vehicle_id = tv.vehicle_id
Since the date & mileage are repeated for other records and didn't get the expected result. So the query outs the following output:
+---------------+------------+------------+---------+----------+
| registered_no | issue_date | date | mileage | fuel_qty |
+---------------+------------+------------+---------+----------+
| SP KR-3503 | 2021-02-22 | 2021-04-25 | 150 | 40 |
| SP KR-3503 | 2021-02-26 | 2021-04-25 | 150 | 30 |
| null | 2021-03-03 | 2021-04-25 | 150 | 40 |
| null | 2021-03-15 | 2021-04-25 | 150 | 45 |
| SP KR-3503 | 2021-03-18 | 2021-04-25 | 150 | 40 |
| null | 2021-03-25 | 2021-04-25 | 150 | 45 |
| null | 2021-04-04 | 2021-04-25 | 150 | 35 |
| SP KE-6794 | 2021-04-25 | 2021-04-25 | 150 | 40 |
+---------------+------------+------------+---------+----------+
What may be going wrong in my query ? Can anyone help ?

Try this query:
SELECT tv.registered_no, tdf.issue_date, ttd.date, ttd.mileage, tdfd.fuel_qty
FROM tbl_direct_fuel tdf
LEFT JOIN tbl_vehicle tv
ON tdf.vehicle=tv.vehicle_id
LEFT JOIN tbl_trip_details ttd
ON tdf.vehicle=ttd.vehicle_id
AND tdf.issue_date=ttd.date
LEFT JOIN tbl_direct_fuel_details tdfd
ON tdf.direct_fuel_id=tdfd.direct_fuel_id;
Demo fiddle
Rather than having two separate sub-queries, I try to directly get the result with a bunch of LEFT JOINs. From what I can see in the updated expected result, tbl_direct_fuel is enough to be the reference table for all the others. I's just a matter of figuring the correct ON condition.
Also, I mentioned about the missing tbl_direct_fuel.status and tbl_direct_fuel_details.fuel_price from the sample data. I'm not concerned about tbl_direct_fuel_details.fuel_price in particular because it wasn't included in the end SELECT result however, tbl_direct_fuel.status was being used in WHERE might have some impact. But after constructing the possible query, I assume that in this example, it's not required to include tbl_direct_fuel.status at all. Anyway, that's for you to figure out.

Related

SQL : For each ID, only display the highest value from another column (can't group by)

I have this table here :
+---------+----------+------------+------------+
| idStep | idProj | dateStart | dateEnd |
+---------+----------+------------+------------+
| 1 | 1 | 2011-07-01 | 2011-09-01 |
| 1 | 2 | 2012-05-01 | 2012-05-10 |
| 1 | 3 | 2011-11-01 | 2012-01-20 |
| 2 | 1 | 2011-09-02 | 2011-11-30 |
| 2 | 2 | 2012-05-11 | 2012-06-01 |
| 2 | 3 | 2012-01-21 | 2012-04-01 |
| 3 | 1 | 2011-12-01 | 2012-07-07 |
| 3 | 2 | 2012-06-02 | 2012-07-01 |
| 3 | 3 | 2012-04-02 | NULL |
| 4 | 1 | 2012-07-08 | NULL |
| 4 | 2 | 2012-07-01 | 2012-07-21 |
| 5 | 2 | 2012-07-22 | 2012-07-23 |
+---------+----------+------------+------------+
I need to find the current step of each project by searching for the highest idStep of each idProject without using Group By, which is where I'm completely stuck. Without GROUP BY I just cannot get there.
Basically, the output should be this :
+---------+----------+------------+------------+
| idStep | idProj | dateStart | dateEnd |
+---------+----------+------------+------------+
| 3 | 3 | 2012-04-02 | NULL |
| 4 | 1 | 2012-07-08 | NULL |
| 5 | 2 | 2012-07-22 | 2012-07-23 |
+---------+----------+------------+------------+
I want to use a Query built like this
SELECT idProj,idStep
FROM table
WHERE idStep = (SELECT max(idStep) FOR EACH idProj)
I know that FOR EACH isn't SQL, I'm only trying to make my desired query structure readable.
You want a correlated subuqery:
SELECT idProj, idStep
FROM table t
WHERE t.idStep = (SELECT max(idStep)
FROM table t2
WHERE t2.idProj = t.idProj
);

Fetch data with default rows

Suppose we have tables resource, item and price:
item
+--------+---------+
| itemId | name |
+--------+---------+
| 2743 | Product |
+--------+---------+
resource
+------------------+-----+----------------+--+
|resourceId | key | value | groupId |
+-----------+-----+----------------+---------+
| 45 | 1 | Client | 3 |
| 46 | 2 | Manufacturer | 3 |
| 69 | 1 | Delivery | 4 |
| 70 | 2 | Collection | 4 |
| 71 | 3 | Assembly | 4 |
| 72 | 4 | Client Request | 4 |
| 73 | 1 | Draft | 5 |
| 74 | 2 | Not Confirmed | 5 |
| 75 | 3 | Confirmed | 5 |
+-----------+-----+----------------+---------+
price
+---------+-----------+--------+----------+
| priceId | serviceId | itemId | price |
+---------+-----------+--------+----------+
| 294 | 4 | 0 | 20.0000 |
| 293 | 3 | 0 | 20.0000 |
| 292 | 2 | 0 | 20.0000 |
| 291 | 1 | 0 | 20.0000 |
| 290 | 1 | 2743 | 18.4000 |
| 288 | 1 | 2738 | 10.0000 |
| 267 | 4 | 2721 | 0.0000 |
| 266 | 3 | 2721 | 0.0000 |
| 265 | 2 | 2721 | 0.0000 |
+---------+-----------+--------+----------+
Rows in table resource with groupId=4 are services. They are refered to via column key from table price (serviceId). As you can see I have default prices for those services (first four rows in price with itemId=0). I need to fetch as many pirce rows as there are services for a given item. If that item doesn't have a price for a service, service's default price should be fetched. For example, the item with id = 2743 has price for service Delivery. For all other services their default prices should be returned. What I need is a result like this:
+---------+-----------+--------+----------+-----------+-----+----------------+---------+
| priceId | serviceId | itemId | price |resourceId | key | value | groupId |
+---------+-----------+--------+----------+-----------+-----+----------------+---------+
| 290 | 1 | 2743 | 18.4000 | 69 | 1 | Delivery | 4 |
| 292 | 2 | NULL | 17.0000 | 70 | 2 | Collection | 4 |
| 293 | 3 | NULL | 13.0000 | 71 | 3 | Assembly | 4 |
| 294 | 4 | NULL | 9.0000 | 72 | 4 | Client Request | 4 |
+---------+-----------+--------+----------+-----------+-----+----------------+---------+
Here is a query I tried. I get as many rows as services, but all of them have have the same price (that of the item)
SELECT *
FROM `price` AS `p`
INNER JOIN `item` ON p.id = item.id
RIGHT JOIN `resource` AS `res` ON 1 = 1
WHERE (res.groupId = 4)
AND (p.itemId = 2743);
select i.itemId, coalesce(p1.price, p0.price)
from item i
cross join price p0
left join price p1
on p1.serviceId = p0.serviceId
and p1.itemId = i.itemId
where i.itemId = 2743
and p0.itemId = 0

Get the required sql query result

mysql> select calendar.calId,calendar.cdate,lsUsers.userId,
apptActual.OPDID,count(*)
from calendar
LEFT JOIN apptActual on calendar.calId=apptActual.calendarId
LEFT JOIN lsUsers on lsUsers.userId=apptActual.OPDID
group by 1,2,3,4 order by 3
desc limit 10;
Result is:
+-------+------------+--------+-------+----------+
| calId | cdate | userId | OPDID | count(*) |
+-------+------------+--------+-------+----------+
| 76 | 2016-03-16 | 11 | 11 | 1 |
| 75 | 2016-03-15 | 11 | 11 | 1 |
| 77 | 2016-03-17 | 9 | 9 | 1 |
| 75 | 2016-03-15 | 8 | 8 | 1 |
| 75 | 2016-03-15 | 1 | 1 | 1 |
| 1279 | 2019-07-02 | NULL | NULL | 1 |
| 1287 | 2019-07-10 | NULL | NULL | 1 |
| 1295 | 2019-07-18 | NULL | NULL | 1 |
| 1303 | 2019-07-26 | NULL | NULL | 1 |
| 1311 | 2019-08-03 | NULL | NULL | 1 |
+-------+------------+--------+-------+----------+
10 rows in set (0.34 sec)
By using above query I am getting result as above, but I need result result like below (all userId's has to come.)
+-------+------------+--------+-------+----------+
| calId | cdate | userId | OPDID | count(*) |
+-------+------------+--------+-------+----------+
| 76 | 2016-03-16 | 11 | 11 | 1 |
| 75 | 2016-03-15 | 11 | 11 | 1 |
| 77 | 2016-03-17 | 9 | 9 | 1 |
| 75 | 2016-03-15 | 8 | 8 | 1 |
| 75 | 2016-03-15 | 1 | 1 | 1 |
| 1279 | 2019-07-02 | 4 | NULL | 1 |
| 1287 | 2019-07-10 | 21| NULL | 1 |
| 1295 | 2019-07-18 | 3 | NULL | 1 |
| 1303 | 2019-07-26 | 7 | NULL | 1 |
| 1311 | 2019-08-03 | 5 | NULL | 1 |
+-------+------------+--------+-------+----------+
Please suggest me how to get result as above!
If you join lsUsers on userId = appActual.OPDID you will get what you ask for: usedId (NULL) = OPDID (NULL). Is there any other way to join lsUsers?

MySQL SUM() returning spurious (?) values

Despite spending an hour on this, the solution is eluding me still. I have a complex-ish query that is returning incorrect data for the SUM(). Yet, when I strip it down to the barest form, it outputs the correct data. But why and fix, I cannot figure out.
The Problem
SELECT po.*, SUM( poo.material_qty ) AS total_items_ordered, suppliers.supplier_name
FROM `purchase_orders` po
LEFT JOIN purchase_orders_items poo ON poo.poid = po.poid
LEFT JOIN suppliers ON suppliers.supplier_id = po.supplier_id
LEFT JOIN materials_batch mb ON mb.purchase_order_no = po.poid
WHERE po_status NOT
IN (
'Fulfilled', 'Cancelled'
)
AND batch_status NOT
IN (
'Arrived', 'Cancelled', 'Refused', 'Missing', 'Damaged', 'Completed'
)
GROUP BY po.poid
ORDER BY date_expected ASC
Provides wildly incorrect data for 'total_items_ordered'.
+-------+---------------------+---------------------+-------------+--------+-------------+--------+-----------+---------+----------+--------+----+--------+-----------+---------------------+-----------------------+
| poid | date_raised | date_expected | supplier_id | job_id | job_item_id | ref_no | sub_total | VAT | total | userid | DN | manual | po_status | total_items_ordered | supplier_name |
+-------+---------------------+---------------------+-------------+--------+-------------+--------+-----------+---------+----------+--------+----+--------+-----------+---------------------+-----------------------+
| 15571 | 2014-06-24 13:32:55 | 2014-06-25 00:00:00 | 1 | 0 | 0 | | 14850.10 | 2970.02 | 17820.12 | 1 | | N | Raised | 545 | John Parker & Son Ltd |
| 15572 | 2014-06-24 13:33:26 | 2014-06-25 00:00:00 | 1 | 0 | 0 | | 997.80 | 199.56 | 1197.36 | 1 | | N | Raised | 80 | John Parker & Son Ltd |
+-------+---------------------+---------------------+-------------+--------+-------------+--------+-----------+---------+----------+--------+----+--------+-----------+---------------------+-----------------------+
2 rows in set (0.00 sec)
And yet, when I strip all the complexities out of the query and run the raw SUM(), the value is correct:
mysql> SELECT poid, SUM(material_qty) AS total_items_ordered FROM `purchase_orders_items` GROUP BY poid;
+-------+---------------------+
| poid | total_items_ordered |
+-------+---------------------+
| 15571 | 109 |
| 15572 | 20 |
+-------+---------------------+
2 rows in set (0.00 sec)
Can anyone shed any light on where I'm going wrong here?? I've included all the test table content below just in case you can spot something I've missed. Thank you!
Data Example
mysql> SELECT * FROM purchase_orders;
+-------+---------------------+---------------------+-------------+--------+-------------+--------+-----------+---------+----------+--------+----+--------+-----------+
| poid | date_raised | date_expected | supplier_id | job_id | job_item_id | ref_no | sub_total | VAT | total | userid | DN | manual | po_status |
+-------+---------------------+---------------------+-------------+--------+-------------+--------+-----------+---------+----------+--------+----+--------+-----------+
| 15571 | 2014-06-24 13:32:55 | 2014-06-25 00:00:00 | 1 | 0 | 0 | | 14850.10 | 2970.02 | 17820.12 | 1 | | N | Raised |
| 15572 | 2014-06-24 13:33:26 | 2014-06-25 00:00:00 | 1 | 0 | 0 | | 997.80 | 199.56 | 1197.36 | 1 | | N | Raised |
+-------+---------------------+---------------------+-------------+--------+-------------+--------+-----------+---------+----------+--------+----+--------+-----------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM purchase_orders_items;
+--------+-------+-------------+--------------+----------------+--------------+--------------------------------------------------+
| poi_id | poid | material_id | material_qty | material_price | material_sku | material_name |
+--------+-------+-------------+--------------+----------------+--------------+--------------------------------------------------+
| 1 | 15571 | 2 | 3 | 100.00 | PKS275282 | 406x140 White Universal Beam (S355) |
| 2 | 15571 | 5 | 10 | 17.40 | 118-64-44 | Test Item (S275) |
| 3 | 15571 | 8 | 1 | 9984.50 | 113-64-21 | A really really really big universal beam (S355) |
| 4 | 15571 | 9 | 77 | 10.00 | 12345 | A thing |
| 5 | 15571 | 10 | 18 | 201.20 | 12-34-56 | 102x230 Narrow Beam (S355) |
| 6 | 15572 | 2 | 6 | 100.00 | PKS275282 | 406x140 White Universal Beam (S355) |
| 7 | 15572 | 5 | 9 | 17.40 | 118-64-44 | Test Item (S275) |
| 8 | 15572 | 9 | 4 | 10.00 | 12345 | A thing |
| 9 | 15572 | 10 | 1 | 201.20 | 12-34-56 | 102x230 Narrow Beam (S355) |
+--------+-------+-------------+--------------+----------------+--------------+--------------------------------------------------+
9 rows in set (0.00 sec)
mysql> SELECT * FROM suppliers;
+-------------+-----------------------+--------------------+--------------+---------------------+-------------------+-----------------------+--------------------------+---------------------+----------------------+
| supplier_id | supplier_name | supplier_telephone | supplier_fax | supplier_added_date | supplier_added_by | supplier_last_updated | supplier_last_updated_by | supplier_assessed | supplier_approved_by |
+-------------+-----------------------+--------------------+--------------+---------------------+-------------------+-----------------------+--------------------------+---------------------+----------------------+
| 1 | John Parker & Son Ltd | 01227 783333 | 0800 521932 | 2014-05-04 15:57:43 | 1 | 2014-06-05 16:38:23 | 1 | 2014-05-04 15:57:43 | 2 |
| 2 | Superior Glass Ltd. | 01825 764766 | 01825 767699 | 2014-05-04 17:48:38 | 1 | 2014-06-04 20:14:16 | 1 | 2014-05-04 17:48:38 | 3 |
| 3 | DTS Origins Ltd. | 01283 3283029 | 01928 303494 | 2014-05-04 17:51:57 | 1 | 2014-05-04 17:53:08 | 1 | 2014-05-04 17:51:57 | 2 |
+-------------+-----------------------+--------------------+--------------+---------------------+-------------------+-----------------------+--------------------------+---------------------+----------------------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM materials_batch;
+-------------------+-------+---------------------+-------------------+------------------+-----+---------+------------+-------------+-------------+--------------+
| material_batch_id | poiid | rcvd_date | purchase_order_no | delivery_note_no | qty | rcvd_by | dn_scanned | material_id | supplier_id | batch_status |
+-------------------+-------+---------------------+-------------------+------------------+-----+---------+------------+-------------+-------------+--------------+
| 1 | 1 | 0000-00-00 00:00:00 | 15571 | | 3 | 0 | No | 2 | 1 | Ordered |
| 2 | 2 | 0000-00-00 00:00:00 | 15571 | | 10 | 0 | No | 5 | 1 | Ordered |
| 3 | 3 | 0000-00-00 00:00:00 | 15571 | | 1 | 0 | No | 8 | 1 | Ordered |
| 4 | 4 | 0000-00-00 00:00:00 | 15571 | | 77 | 0 | No | 9 | 1 | Ordered |
| 5 | 5 | 0000-00-00 00:00:00 | 15571 | | 18 | 0 | No | 10 | 1 | Ordered |
| 6 | 6 | 0000-00-00 00:00:00 | 15572 | | 6 | 0 | No | 2 | 1 | Ordered |
| 7 | 7 | 0000-00-00 00:00:00 | 15572 | | 9 | 0 | No | 5 | 1 | Ordered |
| 8 | 8 | 0000-00-00 00:00:00 | 15572 | | 4 | 0 | No | 9 | 1 | Ordered |
| 9 | 9 | 0000-00-00 00:00:00 | 15572 | | 1 | 0 | No | 10 | 1 | Ordered |
+-------------------+-------+---------------------+-------------------+------------------+-----+---------+------------+-------------+-------------+--------------+
The reason for the wrong results should be clear when you leave out the GROUP BY from your query. For each table you JOIN, the number of returned rows is multiplied by the number of rows found by the JOIN.
As the materials_batch table contains multiple entries per order, the resulting total_items_ordered is multiplied by 5 for order number 15571, and its multiplied by 4 for order number 15572.
Try the following:
SELECT
po.*,
(
SELECT SUM(poo.material_qty)
FROM purchase_orders_items poo
WHERE poo.poid = po.poid
) AS total_items_ordered,
suppliers.supplier_name
FROM `purchase_orders` po
LEFT JOIN suppliers ON suppliers.supplier_id = po.supplier_id
LEFT JOIN materials_batch mb ON mb.purchase_order_no = po.poid
WHERE po_status NOT
IN (
'Fulfilled', 'Cancelled'
)
AND batch_status NOT
IN (
'Arrived', 'Cancelled', 'Refused', 'Missing', 'Damaged', 'Completed'
)
GROUP BY po.poid
ORDER BY date_expected ASC

Complex MySQL query summing joined records where parent and child ids are equal

Maybe this will be an easy one for some of you MySQL masters who see this stuff like a level 3 children's book.
I have multiple tables that I'm joining to produce statistical data for a report and I'm getting tripped up at the moment trying to figure it out. It's obviously imperative the figures are correct because it impacts a number of decisions going forward.
Here's the lay of the land (not the full picture, but you'll get the point):
Affiliate Table
+----+-----------+------------+---------------------+
| id | firstname | lastname | created_date |
+----+-----------+------------+---------------------+
| 1 | Mike | Johnson | 2010-11-22 17:44:37 |
| 2 | Trevor | Wilson | 2010-12-23 16:24:24 |
| 3 | Bob | Parker | 2011-11-04 10:33:49 |
+----+-----------+------------+---------------------+
Now our query should only find results for Bob Parker (id 3) so I'll only show example results for Bob.
Affiliate Link Table
+-----+-----------+--------------+-----------+----------+---------------------+
| id | parent_id | affiliate_id | link_type | linkhash | created_date |
+-----+-----------+--------------+-----------+----------+---------------------+
| 21 | NULL | 3 | PRODUCT | fa2e82a7 | 2011-06-15 16:18:37 |
| 27 | NULL | 3 | PRODUCT | 55de2ae7 | 2011-06-23 01:03:00 |
| 28 | NULL | 3 | PRODUCT | 02cae72f | 2011-06-23 01:03:00 |
| 29 | 27 | 3 | PRODUCT | a4dfb2c8 | 2011-06-23 01:03:00 |
| 30 | 28 | 3 | PRODUCT | 72cea1b2 | 2011-06-23 01:03:00 |
| 36 | 21 | 3 | PRODUCT | fa2e82a7 | 2011-06-23 01:07:03 |
| 59 | 21 | 3 | PRODUCT | ec33413f | 2011-11-04 17:49:17 |
| 60 | 27 | 3 | PRODUCT | f701188c | 2011-11-04 17:49:17 |
| 69 | 21 | 3 | PRODUCT | 6dfb89fd | 2011-11-04 17:49:17 |
+-----+-----------+--------------+-----------+----------+---------------------+
Affiliate Stats
+--------+--------------+--------------------+----------+---------------------+
| id | affiliate_id | link_id | order_id | type | created_date |
+--------+--------------+---------+----------+----------+---------------------+
| 86570 | 3 | 21 | NULL | CLICK | 2013-01-01 00:07:31 |
| 86574 | 3 | 21 | NULL | PAGEVIEW | 2013-01-01 00:08:53 |
| 86579 | 3 | 21 | 411 | SALE | 2013-01-01 00:09:52 |
| 86580 | 3 | 36 | NULL | CLICK | 2013-01-01 00:09:55 |
| 86582 | 3 | 36 | NULL | PAGEVIEW | 2013-01-01 00:09:56 |
| 86583 | 3 | 28 | NULL | CLICK | 2013-01-01 00:11:04 |
| 86584 | 3 | 28 | NULL | PAGEVIEW | 2013-01-01 00:11:04 |
| 86586 | 3 | 30 | NULL | CLICK | 2013-01-01 00:30:18 |
| 86587 | 3 | 30 | NULL | PAGEVIEW | 2013-01-01 00:30:20 |
| 86611 | 3 | 69 | NULL | CLICK | 2013-01-01 00:40:19 |
| 86613 | 3 | 69 | NULL | PAGEVIEW | 2013-01-01 00:40:19 |
| 86619 | 3 | 69 | 413 | SALE | 2013-01-01 00:42:12 |
| 86622 | 3 | 60 | NULL | CLICK | 2013-01-01 00:46:00 |
| 86624 | 3 | 60 | NULL | PAGEVIEW | 2013-01-01 00:46:01 |
| 86641 | 3 | 60 | NULL | PAGEVIEW | 2013-01-01 00:55:58 |
| 86642 | 3 | 30 | 415 | SALE | 2013-01-01 00:56:35 |
| 86643 | 3 | 28 | NULL | PAGEVIEW | 2013-01-01 00:56:43 |
| 86644 | 3 | 60 | 417 | SALE | 2013-01-01 00:56:52 |
+--------+--------------+---------+----------+----------+---------------------+
Orders
+------+--------------+---------+---------------------+
| id | affiliate_id | total | created_date |
+------+--------------+---------+---------------------+
| 411 | 3 | 138.62 | 2013-01-01 00:09:50 |
| 413 | 3 | 312.87 | 2013-01-01 00:09:52 |
| 415 | 3 | 242.59 | 2013-01-01 00:09:55 |
| 417 | 3 | 171.18 | 2013-01-01 00:09:55 |
+------+--------------+---------+---------------------+
Now the results that I need should look like this (only show main/parent link id)
+---------+---------+
| link_id | total |
+---------+---------+
| 21 | 451.49 | <- 1 order from parent (21), 1 from child (69)
| 27 | 171.18 | <- 1 order from child (69)
| 28 | 242.59 | <- 1 order from child (30)
+---------+---------+
I'm not quite sure how to write the query so that I can sum where affiliate_link.id and affiliate_link.parent_id are combined. Is this even possible with a couple of JOINs and GROUPing?
I'm not too sure why you have denormalised affiliate_id (by placing it in each table) and, therefore, whether one can rely on all Stats and Orders that stem from a particular Link to have the same affiliate_id as that Link.
If it's possible, I'd suggest changing the AffiliateLink.parent_id column such that parent records point to themselves (rather than NULL):
UPDATE AffiliateLink SET parent_id = id WHERE parent_id IS NULL
Then it's a simple case of joining and grouping:
SELECT AffiliateLink.parent_id AS link_id,
SUM(Orders.total) AS total
FROM AffiliateLink
JOIN AffiliateStats ON AffiliateStats.link_id = AffiliateLink.id
JOIN Orders ON Orders.id = AffiliateStats.order_id
WHERE AffiliateLink.affiliate_id = 3
GROUP BY AffiliateLink.parent_id
See it on sqlfiddle.
If it's not possible to make the change, you can effectively create the resulting AffiliateLink table using UNION (but beware the performance implications, as MySQL will not be able to use indexes on the result):
(
SELECT parent_id, id, affiliate_id FROM AffiliateLink WHERE parent_id IS NOT NULL
UNION ALL
SELECT id , id, affiliate_id FROM AffiliateLink WHERE parent_id IS NULL
) AS AffiliateLink
See it on sqlfiddle.