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 83 84 85 86
| declare @matable TABLE(
AGENCE varchar(20),
REGION varchar(20),
INGENIEUR varchar(20),
Nb_Devis varchar(20),
Nb_Affaires varchar(20),
taux_Reussite decimal(38,2),
Montant_Cdes decimal(38,2),
Sous_traitance decimal(38,2),
Montant_Facturation decimal(38,2))
INSERT INTO @matable(
REGION,
AGENCE,
INGENIEUR,
Nb_Devis,
Nb_Affaires,
taux_Reussite,
Montant_Cdes,
Sous_traitance)
WITH test AS
(
SELECT
Région,
[Global Dimension 1 Code] AS Agence,
No_,
case
when No_ LIKE 'DE.%' then '1'
end AS Nb_Devis,
case
when No_ LIKE 'AF.%' then '1'
end AS Nb_Affaires,
case
when No_ LIKE 'D.%' then [Creation Date]
when No_ LIKE 'F.%' then [Date de commande]
end AS Date,
[Code Ingénieur]AS Ingenieur,
[Date de commande] AS Commandes,
[Montant Devis] AS Montant,
[Montant Sous-Traitance] AS Sous_traitance
FROM dbo.[societe$Job]
WHERE Région = 'SUD'
)
SELECT
max(Région) AS Région,
Agence,
Ingenieur,
count(Nb_Devis) AS Nb_Devis,
count(Nb_Affaires) AS Nb_Affaires,
(NULLIF(count(Nb_Affaires), 0)*100 /count(Nb_Devis)) AS taux_Reussite,
sum(Montant) AS Montant,
sum(Sous_traitance) AS Sous_traitance
FROM test
WHERE date BETWEEN '01/01/2010' AND '31/12/2010'
GROUP BY Agence,Ingenieur
ORDER BY Agence
)
INSERT INTO @matable(
REGION,
AGENCE,
INGENIEUR,
Montant_Facturation)
SELECT
Région,
Agence,
[Code Ingénieur]AS Ingenieur,
cast(([Phase A]+[Phase C]+[Phase D]+[Phase B complet])AS float) AS Montant
FROM dbo.[societe$Statistiques]
WHERE [Date Facture] BETWEEN '01/01/2010' AND '31/12/2010'
AND [Région] = 'SUD'
SELECT
REGION,
AGENCE,
INGENIEUR,
Nb_Devis,
Nb_Affaires,
taux_Reussite,
Montant_Cdes,
Sous_traitance,
Montant_Facturation
FROM @matable
end |
Partager