1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| with note_unique_co_auteur as (
select nca.id_note
from annuaire a2
join note_coauteur nca on nca.id_co_auteur = a2.id_annuaire
group by nca.id_note
having count(distinct a2.service) = 1
)
, note_auteur_est_seul_co_auteur as (
select nca.id_note
from annuaire a2
join note_coauteur nca on nca.id_co_auteur = a2.id_annuaire
where -- Seul co auteur
exists (select 1
from note_unique_co_auteur x
where x.id_note = nca.id_note)
-- co auteur est aussi auteur
and exists (select 1
from annuaire a
join note n on n.id_auteur = a.id_annuaire
where n.id_note = nca.id_note
and a.service = a2.service)
)
, col3 as (
select datepart(year, n.ladate) as annee
, datepart(month, n.ladate) as mois
, a.service
, count(*) as nb_aut_seul_co_aut
from annuaire a
join note n on n.id_auteur = a.id_annuaire
where exists (select 1
from note_auteur_est_seul_co_auteur x
where x.id_note = n.id_note)
group by datepart(year, n.ladate)
, datepart(month, n.ladate)
, a.service
)
, col1 as (
select datepart(year, n.ladate) as annee
, datepart(month, n.ladate) as mois
, a.service
, count(n.id_note) as nb_note_auteur
from annuaire a
left join note n on n.id_auteur = a.id_annuaire
group by datepart(year, n.ladate)
, datepart(month, n.ladate)
, a.service
)
, col2 as (
select datepart(year, n2.ladate) as annee
, datepart(month, n2.ladate) as mois
, a2.service
, count(distinct nca.id_note) as nb_dist_co_aut_pas_aut
from annuaire a2
join note_coauteur nca on nca.id_co_auteur = a2.id_annuaire
join note n2 on n2.id_note = nca.id_note
where not exists (select 1
from annuaire a
join note n on n.id_auteur = a.id_annuaire
where n.id_note = nca.id_note
and a.service = a2.service)
group by datepart(year, n2.ladate)
, datepart(month, n2.ladate)
, a2.service
)
select c1.annee
, c1.mois
, c1.service
, c1.nb_note_auteur
, coalesce (c2.nb_dist_co_aut_pas_aut,0)
, coalesce (c3.nb_aut_seul_co_aut, 0)
from col1 c1
left join col2 c2
on c2.annee = c1.annee
and c2.mois = c1.mois
and c2.service = c1.service
left join col3 c3
on c3.annee = c1.annee
and c3.mois = c1.mois
and c3.service = c1.service
order by c1.annee
, c1.mois
, c1.service; |
Partager