1

First of all, Excuse me as you may have seen that question before, but I've already tried the solutions around here. but it's not matching with my need. I have two tables. one of them for sales section agents, other for technical support agents. I need to SUM the values of Received calls in both sections per day.

My Table Structure and sample data:

enter image description here [enter image description here

example : date column will remain as is, Received column = sales.received + tech.received , sales.answered + tech.answered I came through this :

SELECT   
    (SELECT SUM(`Received_Calls`) FROM sales) + (SELECT SUM(`Received_Calls`) FROM tech
    ) FROM DUAL

But it's showing the total only.. without showing the daily calculation for every day.

expected outcome:

---------------------------------------------
Date       | Received | Answered | Abandoned
---------------------------------------------
|2014-11-14|   8406   |   8363   |    43
|2014-11-15|   9909   |   9792   |    116
---------------------------------------------

and there is no certain dates available on a table without the other, every day's date is available on both tables with no exceptions.

any help? Thanks :)

6
  • 1) You show a single table's content, but your query gets data from 2 tables. Pls update the question with details about the 2 tables, how they are joined, and sample data from both tables. 2) Pls provide an expected outcome based on the sample data. 3) sum() being an aggregate function collapses the resultset into a single record in the absence of a group by. If you have 1 record per day, then you just need to add the columns to each other, no need for the sum(). Commented Nov 10, 2016 at 17:44
  • the screenshot attached applies on both tables. as they have the exact same structure. but anyway. i will edit the question with the expected outcome Commented Nov 10, 2016 at 17:48
  • Post the other table with data please, not just structure. Commented Nov 10, 2016 at 17:55
  • 1
    Also, is it possible to have data for certain dates in one of the tables but not in the other one? If yes, is this possible for both of the tables, or just for one of them? Commented Nov 10, 2016 at 17:58
  • 1
    no it's not possible as all the dates are available in both tables. question updated, sorry shadow for being not clear enough :) Commented Nov 10, 2016 at 18:12

1 Answer 1

3

Assuming the dates are unique (per table), you can join according to it and just add the appropriate values:

SELECT s.`date`, 
       s.received_calls + t.received_calls AS received,
       s.answered_calls + t.answered_calls AS answered,
       s.abandoned_calls + t.abandoned_calls AS abandoned
FROM   sales s
JOIN   tech t ON s.`date` = t.`date`
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for you help, but strangely the code you mentioned doesn't seem to work with me, it gets me an error "An alias was previously found (near "t" at position 45)"
problem was with phpmyadmin version. removed, installed again. working now. Thanks.

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.