1

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
3
  • 2
    Seems like you'd just wrap that second query in parens, and 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? Commented Nov 26, 2013 at 1:56
  • ON your edit you have left a , after c.mobilephone on 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' Commented Nov 26, 2013 at 3:43
  • I end up getting way too many results if I moved the where clause. Commented Nov 26, 2013 at 5:20

1 Answer 1

1

Your code is absolutely correct,There is a small syntax error only.Just remove the comma(',') after c.mobilephone in the select statement,your syntax error problem will be solved and query will run.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.