I have a query that counts calls and failed calls from two different tables from Nov 11 to Nov 24, 2013 for each client.
SELECT d.id_client,
d.login,
Coalesce(c.total, 0) AS calls,
Coalesce(fc.total, 0) AS calls_failed
FROM api.clients d
LEFT OUTER JOIN (SELECT Count(*) AS total,
id_client
FROM voip.calls c
WHERE c.call_start >= '2013-11-11 00:00:00'
AND c.call_start < '2013-11-25 00:00:00'
GROUP BY id_client) c
ON d.id_client = c.id_client
LEFT OUTER JOIN (SELECT Count(*) AS total,
id_client
FROM voip.callsfailed c
WHERE c.call_start >= '2013-11-11 00:00:00'
AND c.call_start < '2013-11-25 00:00:00'
AND c.ie_error_number <> 0
GROUP BY id_client) fc
ON d.id_client = fc.id_client
WHERE d.id_client IN (SELECT e.idclient
FROM voip.invoiceclients e
WHERE e.clientnr = 'demo')
I have a separate query that provides client_balance, mobile_number, name for each client.
SELECT cr.id_client,
inv.taxid AS company,
inv.name,
inv.lastname,
inv.mobilephone,
cr.account_state
FROM clientsretail cr,
invoiceclients inv
WHERE cr.id_client = inv.idclient
AND inv.clientnr = 'demo'
ORDER BY inv.taxid,
inv.name;
How can I merge these queries to produce the following output: id_client,Company,Name,Lastname,Mobilephone,Login,Calls,Failed,calls,Balance
I tried to take some baby steps with the following query, but failed:
SELECT d.id_client, d.login,
COALESCE(c.total, 0) AS calls, COALESCE(fc.total, 0) AS calls_failed
FROM api.clients d
LEFT OUTER JOIN
(
SELECT COUNT(*) AS total, id_client
FROM voip.calls c
WHERE c.call_start >= '2013-11-11 00:00:00'
AND c.call_start < '2013-11-25 00:00:00'
GROUP BY id_client
) c ON d.id_client = c.id_client
LEFT OUTER JOIN
(
SELECT COUNT(*) AS total, id_client
FROM voip.callsfailed c
WHERE c.call_start >= '2013-11-11 00:00:00'
AND c.call_start < '2013-11-25 00:00:00'
AND c.IE_error_number <> 0
GROUP BY id_client
) fc ON d.id_client = fc.id_client
LEFT OUTER JOIN
(
SELECT c.idclient,
c.taxid,
c.name,
c.lastname,
c.mobilephone
FROM voip.invoiceclients c
) v ON d.id_client=v.idclient
WHERE d.id_client IN
(
SELECT e.idclient
FROM voip.invoiceclients e
WHERE e.clientnr='demo'
)
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
'FROM voip.invoiceclients c
) v ON d.id_client=v.idclient WHERE d.id_clie' at line 28
LEFT JOIN (subquery) v ON v.id_client = d.client_id, and reference columns from that inline view (aliased as v) in the SELECT list of the outer query...SELECT d.id_client, v.company, v.name, .... Maybe I'm missing something?,afterc.mobilephoneon your V subquery. Plus with this edit you dont need the where clause just put it on the V subquery:FROM voip.invoiceclients c WHERE c.clientnr='demo'