1

I have a table TableA which has customer records. It also has a specific type called 'NAME'. Basically everyday couple of new customers are registered and this registration record is added into Database with 'NAME' type. What i currently need is just the count of the number of NAME referenceid's for every single day for AUG, SEP, OCT and NOV till 15th.

select count(referenceid)
from tableA
where type = 'NAME'

If I need the count for a whole month I do the following

select count(referenceid)
from tableA
where type = 'NAME'
  and TIMESTAMP BETWEEN TO_DATE('01-AUG-16') AND TO_DATE('31-AUG-16')

I'm not sure how to automatically get the counts for everyday for all the above mentioned months. If there is no automated way, I'll have to run the same query for almost 90+ times for every single day.

I would appreciate if somebody could help.

Sai Avinash
  • 287
  • 4
  • 7
  • 17
  • GROUP BY date-part – jarlh Nov 15 '16 at 08:48
  • 1
    Note that the solutions provided so far using only GROUP BY DATE will only provide counts for days where data exists. If for whatever reason no new users are registered on a particular day, this would not cause a count of 0 to be displayed. If you do want to show days with 0 you would need to generate a list of days and join that accordingly - heres a useful article http://stackoverflow.com/questions/510012/get-a-list-of-dates-between-two-dates – Chriz Nov 15 '16 at 08:57
  • @Chriz I did not consider that. May I use your propose in my answer? – rbr94 Nov 15 '16 at 09:11
  • @rbr94 Of course, I'm not all that familiar with how to do with within MySQL, would be good to see a solution! In MSSQL its pretty easy with the use of a recursive CTE! – Chriz Nov 15 '16 at 09:19

2 Answers2

1

If I understand you correctly, try to GROUP BY each day using your TIMESTAMP.

SELECT TO_CHAR(TIMESTAMP, 'YYYY-MM-DD') AS yourDate, COUNT(referenceid) 
FROM tableA 
WHERE type='NAME' AND TIMESTAMP BETWEEN TO_DATE('01-AUG-16') AND TO_DATE('31-AUG-16')
GROUP BY TO_CHAR(TIMESTAMP, 'YYYY-MM-DD')
ORDER BY yourDate
rbr94
  • 2,227
  • 3
  • 23
  • 39
-1
select 
    count(referenceid),
    DATENAME(MONTH,[datetimecolumn]) 
from tableA 
where type='NAME' 
GROUP BY MONTH([datetimecolumn])
rbr94
  • 2,227
  • 3
  • 23
  • 39
Ankita
  • 1
  • 1
    By the way, I don't think that fits the question. It is asked for daily values, not for the values of a whole month. And furthermore you should provide a little explanation to your proposed statement – rbr94 Nov 15 '16 at 09:07