I need to create a query that answers the following question:
I want to return the number of registrations that has a reading date of 26 days, number of entries that has a reading date of 27 days and there goes up to 31 days, the last 6 months. I need you to stay like this:
My expected output is this:
LOCALE COMPETENCE Reading_date(DAYS) INSCRIPTION (Quantity)
------ ---- ---------- ---------
CEARA JAN18 26 20
CEARA JAN18 27 02
CEARA JAN18 28 34
CEARA JAN18 29 07
CEARA JAN18 30 12
CEARA JAN18 31 11
CEARA FEV18 26 21
CEARA FEV18 27 09
(and go...)
But my table is this:
INSCRIP COMPETENCE Reading_date
00183938 201801 2018-01-02
00183938 201802 2018-02-01
00183946 201709 2017-09-01
00183946 201710 2017-10-01
00183946 201711 2017-11-01
My sql that I'm trying is this (please abstract the joins):
select top 10 LOCALE
, COMPETENCE
, lead(Reading_date, 1) over (PARTITION by Reading_date ORDER BY Reading_date) next_lecture
, datediff(day, Reading_date, next_lecture) Reading_date
, INSCRIPTION
from BASE o
join LOCALE l
on u.localidade = l.cod_localidade
group by LOCALE, COMPETENCE, Reading_date, INSCRIPTION
somebody help me please? Thanks and sorry for bad english