SQL - How to count worked days

Asked

Viewed 343 times

1

The ACESSOBROA_NOVO table records the access of company employees whenever they go through a turnstile.

I need to do a query in this table that lists the name of the employees and count the days worked in a period, for example, of 5 days.

As in this table are recorded every time he entered and left the company in the same day the following query does not work:

SELECT distinct NOME_FUN, EMPRESA,count(DATAR) as 'dias trabalhdos' 
  FROM [RDO].[dbo].[ACESSOBROA_NOVO]
  where CC=11396 and DATAR between '2015-09-21' and '2015-09-21'
  group by NOME_FUN, empresa

As I only need to check if the employee was present on the days requested and not count how many times he passed the turnstile.

Follow a sample of the table:

Amostra da tabela

  • tried to use COUNT(DISTINCT DATAR) ??

1 answer

1


I believe that by putting DISTINCT in the COUNT, you will get the expected result:

SELECT distinct NOME_FUN, EMPRESA,count(DISTINCT DATAR) as 'dias trabalhdos' 
  FROM [RDO].[dbo].[ACESSOBROA_NOVO]
  where CC=11396 and DATAR between '2015-09-21' and '2015-09-21'
  group by NOME_FUN, empresa

http://www.w3schools.com/sql/sql_func_count.asp

Browser other questions tagged

You are not signed in. Login or sign up in order to post.