I am trying to calculate the total number found in my database
I have 3 tables
vehicle_make
+----+------+
| id | make |
+----+------+
| 1 | Audi |
| 2 | BMW |
+----+------+
vehicle_ads
+----+-----------+----------+------+-----------+
| id | make_code | model | year | dealer_id |
+----+-----------+----------+------+-----------+
| 1 | 2 | 5 series | 2010 | 0 |
+----+-----------+----------+------+-----------+
| 2 | 2 | 3 series | 2014 | 20 |
+----+-----------+----------+------+-----------+
dealership
+----+------------+------+
| id | dealername | make |
+----+------------+------+
| 20 | bla bla | 2 |
+----+------------+------+
Here is my sql query I tried changing this so that I can group make and dealer_id
SELECT j.make AS make, j.id AS id, sum(j.count) AS count FROM
(
(
SELECT v.make, sum(count+dealerCount)
FROM
( SELECT a.dealer_id, v.make AS make, a.make_code, COUNT(*) AS count
FROM `vehicle_make` AS v
JOIN `vehicle_ads` AS a
ON (v.id=a.make_code)
WHERE a.dealer_id>0
GROUP BY a.dealer_id, a.make_code
) AS g
JOIN
( SELECT a.dealer_id, v.make, a.make_code, COUNT(*) AS dealerCount
FROM `vehicle_make` AS v
JOIN `vehicle_ads` AS a
ON (v.id=a.make_code)
WHERE a.dealer_id>0
GROUP BY a.dealer_id
) AS gl
ON gl.dealer_id = g.dealer_id
JOIN
( SELECT a.make_code, COUNT(*) AS makeCount
FROM `vehicle_make` AS v
JOIN `vehicle_ads` AS a
ON (v.id=a.make_code)
WHERE a.dealer_id>0
GROUP BY a.make_code
) AS gg
ON gg.make_code=g.make_code
)
UNION
(
SELECT v.make AS make, v.id AS id, COUNT(*) AS count
FROM `dealership` AS d
JOIN `vehicle_make` AS v
ON (v.id=d.make)
GROUP BY d.make
)
) AS j GROUP BY j.make
I am getting this error now.
1052 - The 'make' column in the list of fields is ambiguous
The result should look like this:
+--------+----+-------+
| make | id | count |
+--------+----+-------+
| Audi | 1 | 300 |
| BMW | 2 | 150 |
| Toyota | 3 | 50 |
+--------+----+-------+
In principle, he should get the total number of blanks in the dealers table, and get the total number of brands in the vechile_ads table (group by make and dealer_id)
Any idea what I'm doing wrong here.
UPDATE:
SQL FIDDLE
NUMADS BMW 1 ( dealer_id), make, make_code, count, count - NUMADS + NUMDEALER