Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Log id is unique so you do not need to group by it - it is pointless. Also, if you use group by, you cannot take any field without aggregate function (like SUM, MIN etc). The reason is as follows: Imagine you have 3 rows:</p> <pre><code>log_id call_date call_time agent_name agent_id customer_name nature_of_inquiry no_of_calls 3 2009-02-14 09:24:01 Lucy a3 Paul account opening 4 4 2009-02-15 09:50:01 Lucy a3 Luke account opening 2 5 2009-02-14 10:24:01 Lucy a3 jill account opening 3 </code></pre> <p>and you type something like</p> <pre><code>SELECT count(*), agent_name, SUM(no_of_calls), customer_name FROM table group by agent_name where agent_name="Lucy" </code></pre> <p>what would be your expected result?</p> <p>Clearly there is no problem in defining what shoould be <code>count(*)</code> - it should be 3. What should be <code>agent_name</code>? The one you grouped by -- Lucy. No problems with <code>sum(no_of_calls)</code> - 4+2+3 = 9. However, here the problem begins: What should be <code>customer_name</code> ?! Paul, Luke or Jill ? you do not know this. In some database you get error if you try to do such query (for example postgresql). In MySQL I guess the first value is being returned (correct me if I am wrong). But this is not intuitive and you should avoid such queries.</p> <p>That is why the correct query would be:</p> <pre><code>SELECT count(*), agent_name, SUM(no_of_calls) FROM table group by agent_name where agent_name="Lucy" </code></pre> <p>because all fields are either in group by or are wrapped in agregate function.</p> <p>There might be an error in your question and you could just wanted to ask for calls to particular customers. In such case just use <code>customer_name</code> in group by, the logic remains the same. You can group by multiple values using <code>comma</code> as separator.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload