-1

i have this query :

Select
  DATE_FORMAT(calls.call_datetime, "%Y-%m-%d") as `groupdate`
from
  calls
WHERE calls.job_id =1
group by DATE_FORMAT(calls.call_datetime,
"%Y-%m-%d")

with return :

groupdate

2018-07-06

2018-07-06

there are 2 rows. how do I count the above query so that the result becomes '2' with one row ?

what i want is:

Result

2

thanks, please help me

Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34
  • 1
    Possible duplicate of [How to use count and group by at the same select statement](https://stackoverflow.com/questions/2722408/how-to-use-count-and-group-by-at-the-same-select-statement) – Alexander Jul 07 '18 at 16:11
  • @Alexander - No, That is not the same question. True, the second best answer to that question is correct for _this_ question, but it is wrong for _that_ question. – Rick James Jul 07 '18 at 20:00

4 Answers4

1

try:

Select
  Count(DATE_FORMAT(calls.call_datetime, "%Y-%m-%d")) as `groupdate`
from
  calls
WHERE calls.job_id =1
group by DATE_FORMAT(calls.call_datetime,
"%Y-%m-%d")
Coral Kashri
  • 3,436
  • 2
  • 10
  • 22
0

You should use count()

Select DATE_FORMAT(calls.call_datetime, "%Y-%m-%d") as groupdate , count(*)
from calls 
WHERE calls.job_id =1 
group by DATE_FORMAT(calls.call_datetime, "%Y-%m-%d")
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

Most simple would be to use

Select
  COUNT(*) as Result
from
  calls
WHERE calls.job_id =1
group by DATE_FORMAT(calls.call_datetime, "%Y-%m-%d")

or a delivered table approach

SELECT
 COUNT(*) as Result
FROM (
  Select
    DATE_FORMAT(calls.call_datetime, "%Y-%m-%d") as `groupdate`
  from
    calls
  WHERE calls.job_id =1 
  group by DATE_FORMAT(calls.call_datetime, "%Y-%m-%d")
) AS a

Both queries should return the same expected result you want.

Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34
0

The question is ambiguous. Is the desired answer "2" because there are 2 rows for 1 date? What if there were more than one date?

SELECT COUNT(*) FROM calls WHERE ...; -- delivers "2"

SELECT COUNT(*) FROM calls WHERE ...; GROUP BY DATE(datetime) -- also delivers "2"

SELECT COUNT(DISTINCT DATE(call_datetime)) FROM calls WHERE ...; -- delivers "1"
Rick James
  • 135,179
  • 13
  • 127
  • 222