sql - Query to check number of records created in a month. -


my table creates new record timestamp daily when integration successful. trying create query check (preferably automated) number of days in month vs number of records in table within time frame.

for example, january has 31 days, know how many days in january process not successful. if number of records less 31, know job failed 31 - x times.

i tried following not getting far:

select count (distinct completedate) table completedate between '01/01/2015' , '01/31/2015' 

every 7 days system executes job twice, 2 records on same day, trying determine number of days nothing happened (failures), assume truncation of date field needed?!

one way use calendar/date table main source of dates in range , left join , count number of null values.

in absence of proper date table can generate range of dates using number sequence 1 found in master..spt_values table:

select count(*) failed  (      select dateadd(day, number, '2015-01-01') date       master..spt_values type='p' , number < 365 ) left join your_table b on a.date = b.completedate b.completedate null , a.date between '01/01/2015' , '01/31/2015' 

sample sql fiddle (with count grouped month)


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -