Joe possède un magasin de location de DVD dans le monde entier. Il a des magasins dans le monde entier dans différentes régions. Chaque mois, il aime voir si son équipement dans ses magasins est à jour en trouvant le pourcentage de son inventaire avec un certain statut de "Actuel", "Besoins Mise à Jour" et "Doit Partir" pour chaque région. Cela permet à ses clients d'avoir la meilleure expérience dans ses magasins. Bill, son assistant, veut construire une requête SQL qui montrera à Joe le pourcentage d'équipement dans chaque région et leur statut. Le type avant lui a utilisé un excel pour faire cela mais Bill pense SQL et ensuite un rapport de SSRS qui permettrait à Joe de choisir quelle région il veut voir l'impressionnerait. Il veut faire passer dynamiquement de mois en trimestres à mesure que l'année avance dans le rapport. C'est ce que le projet de loi a été en mesure de trouver à ce jour:Obtenir la moyenne pondérée dans une catégorie avec plusieurs sous-catégories
with
COUNTS as (
select distinct
count(distinct cnts.[Asset ID]) [Equipment Counts]
,cnts.[Month]
,cnts.[Equipment Status]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(cnts.[Month]) [Year]
,cnts.[Region]
from TABLEIMPORT cnts
group by cnts.[Month], cnts.[Equipment Status], cnts.[Region]
),
TOT as (
select distinct
count(distinct tot.[Asset ID]) [Equipmnet Total]
,tot.[Month]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(tot.[Month]) [Year]
from TABLEIMPORT tot
group by tot.[Month]
),
DATA as (
select
c.[Month]
,c.[Equipment Counts]/cast(t.[Equipment Total] as decimal (18,2)) [Percents]
,c.[Equipment Status]
,c.Quarter
,c.Year
,c.[Region]
from counts c
inner join TOT t on t.[Month] = c.[Month]
),
QUARTERS as (
select distinct
d.Quarter [Month]
,avg(d.Percents) [Percents]
,d.[Equipment Status]
,d.Quarter
,d.Year
from data d
group by d.[Equipment Status], d.Quarter, d.Year
),
OWN as (
select distinct
o.[Region]
,case
when o.[Region] in ('Northeast',
'Southeast',
'Midwest',
'Northwest',
'Southwest',
'Mexico',
'Canada',)
then 'North America'
when o.[Region] like 'Europe'
then 'Europe'
when o.[Region] like 'Africa'
then 'Africa'
when o.[Region] like 'Asia'
then 'Asia'
when o.[Region] like 'Australia'
then 'Australia'
else isnull(o.[Region], 'No Region')
end [SelectRegion]
from TABLEIMPORT o
),
QTRMNTH as (
select distinct
q.Quarter [Month]
,q.Percents [Percents]
,d.[Equipment Status]
,d.Quarter
,d.Year
,o.SelectRegion
from data d
inner join OWN o on o.[Region] = d.[Region]
inner join QUARTERS q on q.Quarter = d.Quarter and q.year = d.Year and q.[Equipment Status] = d.[Equipment Status]
where d.[Month] < DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, -1) and d.[Month] > dateadd(MONTH,-12,GETDATE())
UNION
select distinct
convert(varchar(3),datename(month, d.[Month])) [Month]
,d.Percents [Percents]
,d.[Equipment Status]
,d.Quarter
,d.Year
,o.SelectRegion
from data d
inner join OWN o on o.[Region] = d.[Region]
where d.[Month] > DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, -1) and d.Year = year(getdate())
)
select *
from QTRMNTH
--where QTRMNTH.SelectOrg in (@SelectOrg)
comparaison à Excel d'avant, tout est correct et son SSRS fonctionne parfaitement tous, sauf pour l'Amérique du Nord, qui a des pourcentages qui sont légèrement de. Il pense qu'une moyenne pondérée pour les sous-régions d'Amérique du Nord permettrait de corriger ce pourcentage pour que le pourcentage soit correct parce que certaines sous-régions sont plus grandes que d'autres.
Comment Bill procéderait-il pour calculer les moyennes pondérées de chacune des régions en Amérique du Nord?
EDIT:
Briser les différentes régions
with
OWN as (
select distinct
o.[Region]
,case
when o.[Region] in ('Northeast', 'Southeast', 'Midwest', 'Southwest',
'Mexico', 'Canada',)
then 'North America'
when o.[Region] like 'Europe'
then 'Europe'
when o.[Region] like 'Africa'
then 'Africa'
when o.[Region] like 'Asia'
then 'Asia'
when o.[Region] like 'Australia'
then 'Australia'
else isnull(o.[Region], 'No Region')
end [SelectRegion]
from TABLEIMPORT o
),
COUNTS as (
select distinct
count(distinct cnts.[Asset ID]) [Equipment Counts]
,cnts.[Month]
,cnts.[Equipment Status]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(cnts.[Month]) [Year]
,o.[Region]
from TABLEIMPORT cnts
inner join OWN o on o.[Area] = cnts.[Region]
group by cnts.[Month], cnts.[Equipment Status], o.[Region]
),
COUNTNE as (
select distinct
count(distinct cnts.[Asset ID]) [Equipment Counts]
,cnts.[Month]
,cnts.[Equipment Status]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(cnts.[Month]) [Year]
,o.[Region]
from TABLEIMPORT cnts
inner join OWN o on o.[Region] = cnts.[Region]
where o.[Region] like '%Northeast%'
group by cnts.[Month], cnts.[Equipment Status], o.[Region]
),
COUNTSE as (
select distinct
count(distinct cnts.[Asset ID]) [Equipment Counts]
,cnts.[Month]
,cnts.[Equipment Status]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(cnts.[Month]) [Year]
,o.[Region]
from TABLEIMPORT cnts
inner join OWN o on o.[Region] = cnts.[Region]
where o.[Region] like '%Southeast%'
group by cnts.[Month], cnts.[Equipment Status], o.[Region]
),
COUNTMW as (
select distinct
count(distinct cnts.[Asset ID]) [Equipment Counts]
,cnts.[Month]
,cnts.[Equipment Status]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(cnts.[Month]) [Year]
,o.[Region]
from TABLEIMPORT cnts
inner join OWN o on o.[Region] = cnts.[Region]
where o.[Region] like '%Midwest%'
group by cnts.[Month], cnts.[Equipment Status], o.[Region]
),
COUNSW as (
select distinct
count(distinct cnts.[Asset ID]) [Equipment Counts]
,cnts.[Month]
,cnts.[Equipment Status]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(cnts.[Month]) [Year]
,o.[Region]
from TABLEIMPORT cnts
inner join OWN o on o.[Region] = cnts.[Region]
where o.[Region] like '%Southwest%'
group by cnts.[Month], cnts.[Equipment Status], o.[Region]
),
COUNTNw as (
select distinct
count(distinct cnts.[Asset ID]) [Equipment Counts]
,cnts.[Month]
,cnts.[Equipment Status]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(cnts.[Month]) [Year]
,o.[Region]
from TABLEIMPORT cnts
inner join OWN o on o.[Region] = cnts.[Region]
where o.[Region] like '%Northwest%'
group by cnts.[Month], cnts.[Equipment Status], o.[Region]
),
COUNTMX as (
select distinct
count(distinct cnts.[Asset ID]) [Equipment Counts]
,cnts.[Month]
,cnts.[Equipment Status]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(cnts.[Month]) [Year]
,o.[Region]
from TABLEIMPORT cnts
inner join OWN o on o.[Region] = cnts.[Region]
where o.[Region] like '%Mexico%'
group by cnts.[Month], cnts.[Equipment Status], o.[Region]
),
COUNTCN as (
select distinct
count(distinct cnts.[Asset ID]) [Equipment Counts]
,cnts.[Month]
,cnts.[Equipment Status]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(cnts.[Month]) [Year]
,o.[Region]
from TABLEIMPORT cnts
inner join OWN o on o.[Region] = cnts.[Region]
where o.[Region] like '%Canada%'
group by cnts.[Month], cnts.[Equipment Status], o.[Region]
),
COUNTEU as (
select distinct
count(distinct cnts.[Asset ID]) [Equipment Counts]
,cnts.[Month]
,cnts.[Equipment Status]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(cnts.[Month]) [Year]
,o.[Region]
from TABLEIMPORT cnts
inner join OWN o on o.[Region] = cnts.[Region]
where o.SelectRegion like 'Europe'
group by cnts.[Month], cnts.[Equipment Status], o.[Region]
),
COUNTAS as (
select distinct
count(distinct cnts.[Asset ID]) [Equipment Counts]
,cnts.[Month]
,cnts.[Equipment Status]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(cnts.[Month]) [Year]
,o.[Region]
from TABLEIMPORT cnts
inner join OWN o on o.[Region] = cnts.[Region]
where o.SelectRegion like 'Asia'
group by cnts.[Month], cnts.[Equipment Status], o.[Region]
),
COUNTAF as (
select distinct
count(distinct cnts.[Asset ID]) [Equipment Counts]
,cnts.[Month]
,cnts.[Equipment Status]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(cnts.[Month]) [Year]
,o.[Region]
from TABLEIMPORT cnts
inner join OWN o on o.[Region] = cnts.[Region]
where o.SelectRegion like 'Africa'
group by cnts.[Month], cnts.[Equipment Status], o.[Region]
),
COUNTaus as (
select distinct
count(distinct cnts.[Asset ID]) [Equipment Counts]
,cnts.[Month]
,cnts.[Equipment Status]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(cnts.[Month]) [Year]
,o.[Region]
from TABLEIMPORT cnts
inner join OWN o on o.[Region] = cnts.[Region]
where o.SelectRegion like 'Australia'
group by cnts.[Month], cnts.[Equipment Status], o.[Region]
),
TOT as (
select distinct
count(distinct tot.[Asset ID]) [Equipment Total]
,tot.[Month]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(tot.[Month]) [Year]
,o.[Region]
from TABLEIMPORT tot
inner join OWN o on o.[Region] = tot.[Region]
group by tot.[Month], o.[Region]
),
TOTNA as (
select distinct
count(distinct tot.[Asset ID]) [Equipment Total]
,tot.[Month]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(tot.[Month]) [Year]
,o.[Region]
from TABLEIMPORT tot
inner join OWN o on o.[Region] = tot.[Region]
where o.SelectRegion like 'North America'
group by tot.[Month], o.[Region]
),
TOTEU as (
select distinct
count(distinct tot.[Asset ID]) [Equipment Total]
,tot.[Month]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(tot.[Month]) [Year]
,o.[Region]
from TABLEIMPORT tot
inner join OWN o on o.[Region] = tot.[Region]
where o.SelectRegion like 'Europe'
group by tot.[Month], o.[Region]
),
TOTAS as (
select distinct
count(distinct tot.[Asset ID]) [Equipment Total]
,tot.[Month]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(tot.[Month]) [Year]
,o.[Region]
from TABLEIMPORT tot
inner join OWN o on o.[Region] = tot.[Region]
where o.SelectRegion like 'Asia'
group by tot.[Month], o.[Region]
),
TOTAF as (
select distinct
count(distinct tot.[Asset ID]) [Equipment Total]
,tot.[Month]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(tot.[Month]) [Year]
,o.[Region]
from TABLEIMPORT tot
inner join OWN o on o.[Region] = tot.[Region]
where o.SelectRegion like 'Africa'
group by tot.[Month], o.[Region]
),
TOTAUS as (
select distinct
count(distinct tot.[Asset ID]) [Equipment Total]
,tot.[Month]
,'Q' + convert(varchar(20),datepart(qq,[Month])) [Quarter]
,year(tot.[Month]) [Year]
,o.[Region]
from TABLEIMPORT tot
inner join OWN o on o.[Region] = tot.[Region]
where o.SelectRegion like 'Australia'
group by tot.[Month], o.[Region]
),
DATANE as (
select distinct
cne.[Month]
,cne.[Equipment Counts]/cast(T.[Equipment Total] as decimal (18,2)) [Percents]
,cne.[Equipment Status]
,cne.Quarter
,cne.Year
,cne.[Region]
from COUNTNE cne
inner join TOTNA t on t.[Month] = cne.[Month]
),
DATASE as (
select distinct
cse.[Month]
,cse.[Equipment Counts]/cast(T.[Equipment Total] as decimal (18,2)) [Percents]
,cse.[Equipment Status]
,cse.Quarter
,cse.Year
,cse.[Region]
from COUNTSE cse
inner join TOTNA t on t.[Month] = cse.[Month]
),
DATAMW as (
select distinct
cmw.[Month]
,cmw.[Equipment Counts]/cast(T.[Equipment Total] as decimal (18,2)) [Percents]
,cmw.[Equipment Status]
,cmw.Quarter
,cmw.Year
,cmw.[Region]
from COUNTMW cmw
inner join TOTNA t on t.[Month] = cmw.[Month]
),
DATASW as (
select distinct
csw.[Month]
,csw.[Equipment Counts]/cast(T.[Equipment Total] as decimal (18,2)) [Percents]
,csw.[Equipment Status]
,csw.Quarter
,csw.Year
,csw.[Region]
from COUNTSW csw
inner join TOTNA t on t.[Month] = csw.[Month]
),
DATANW as (
select distinct
cnw.[Month]
,cnw.[Equipment Counts]/cast(T.[Equipment Total] as decimal (18,2)) [Percents]
,cnw.[Equipment Status]
,cnw.Quarter
,cnw.Year
,cnw.[Region]
from COUNTNW cnw
inner join TOTNA t on t.[Month] = cnw.[Month]
),
DATAMX as (
select distinct
cmx.[Month]
,cmx.[Equipment Counts]/cast(T.[Equipment Total] as decimal (18,2)) [Percents]
,cmx.[Equipment Status]
,cmx.Quarter
,cmx.Year
,cmx.[Region]
from COUNTMX cmx
inner join TOTna t on t.[Month] = cmx.[Month]
),
DATACN as (
select distinct
ccn.[Month]
,ccn.[Equipment Counts]/cast(T.[Equipment Total] as decimal (18,2)) [Percents]
,ccn.[Equipment Status]
,ccn.Quarter
,ccn.Year
,ccn.[Region]
from COUNTCN ccn
inner join TOTna t on t.[Month] = ccn.[Month]
),
DATAEU as (
select distinct
ceu.[Month]
,ceu.[Equipment Counts]/cast(t.[Equipment Total] as decimal (18,2)) [Percents]
,ceu.[Equipment Status]
,ceu.Quarter
,ceu.Year
,ceu.[Region]
from COUNTEU ceu
inner join TOTEU t on t.[Month] = ceu.[Month]
),
DATAAS as (
select distinct
cas.[Month]
,cas.[Equipment Counts]/cast(t.[Equipment Total] as decimal (18,2)) [Percents]
,cas.[Equipment Status]
,cas.Quarter
,cas.Year
,cas.[Region]
from COUNTAS cas
inner join TOTAS t on t.[Month] = cas.[Month]
),
DATAAF as (
select distinct
caf.[Month]
,caf.[Equipment Counts]/cast(t.[Equipment Total] as decimal (18,2)) [Percents]
,caf.[Equipment Status]
,caf.Quarter
,caf.Year
,caf.[Region]
from COUNTAF caf
inner join TOTAF t on t.[Month] = caf.[Month]
),
DATAAUS as (
select distinct
caus.[Month]
,caus.[Equipment Counts]/cast(t.[Equipment Total] as decimal (18,2)) [Percents]
,caus.[Equipment Status]
,caus.Quarter
,caus.Year
,caus.[Region]
from COUNTAUS caus
inner join TOTAUS t on t.[Month] = caus.[Month]
),
DATA as (
select *
from DATANE dne
UNION
select *
from DATAse dse
UNION
select *
from DATAmw dmw
UNION
select *
from DATAsw dsw
UNION
select *
from DATAnw dnw
UNION
select *
from DATAcn dcn
UNION
select *
from DATAmx dmx
UNION
select *
from DATAeu deu
UNION
select *
from DATAas das
UNION
select *
from DATAaf daf
UNION
select *
from DATAaus daus
),
QUARTERS as (
select distinct
d.Quarter [Month]
,avg(d.Percents) [Percents]
,d.[Equipment Status]
,d.Quarter
,d.Year
from data d
group by d.[Equipment Status], d.Quarter, d.Year
),
QTRMNTH as (
select distinct
q.Quarter [Month]
,q.Percents [Percents]
,d.[Equipment Status]
,d.Quarter
,d.Year
,o.SelectRegion
from data d
inner join OWN o on o.[Region] = d.[Region]
inner join QUARTERS q on q.Quarter = d.Quarter and q.year = d.Year and q.[Equipment Status] = d.[Equipment Status]
where d.[Month] < DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, -1) and d.[Month] > dateadd(MONTH,-12,GETDATE())
UNION
select distinct
convert(varchar(3),datename(month, d.[Month])) [Month]
,d.Percents [Percents]
,d.[Equipment Status]
,d.Quarter
,d.Year
,o.SelectRegion
from data d
inner join OWN o on o.[Region] = d.[Region]
where d.[Month] > DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, -1) and d.Year = year(getdate())
)
select *
from QTRMNTH
Comment retravailleriez-vous ceci afin qu'il ne semble pas être devoirs? Qu'est-ce que Bill a essayé de faire pour résoudre le problème? Où est cette tentative de requête? –
Ajout d'une tentative de publication – programr
@programr Vous pouvez publier le schéma de vos tables? échantillon de données et résultats? –