在具有多个子类别

时间:2017-07-09 01:01:35

标签: sql sql-server weighted-average

Joe拥有一家全球DVD租赁店。他在全球各地都有商店。他每个月都喜欢通过查找他的库存百分比来确定他的商店中的设备是否是最新的,并且状态为“当前”,#34; "需要更新,"和"必须去"对于每个地区。这有助于确保他的客户在他的商店中获得最佳体验。

比尔,他的助手,想要建立一个SQL查询,向Joe展示每个地区的设备百分比及其状态。在他之前的那个人使用excel来做这个,但比尔认为SQL然后一个SSRS报告允许乔选择他想要查看哪个区域会给他留下深刻印象。随着报告中的年度进展,他希望将其从几个月动态过渡到几个季度。这是比尔迄今为止能够提出的:

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)

将它与之前的excel进行比较,一切都是正确的,除了北美之外,他的SSRS完全正常运行,北美的百分比略有下降。他认为北美次区域的加权平均值会解决这个问题,以确保百分比正确,因为有些次区域比其他次区域更大。

Bill如何计算北美每个地区的加权平均值?

编辑:

打破不同的地区

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 

1 个答案:

答案 0 :(得分:0)

这个查询看起来有点像狗早餐。 主要问题:

    SELECT DISTINCT查询中不需要
  1. GROUP BY,因为值已经不同。 如果DISTINCT中的数据未规范化,我可以了解COUNTSTOT查询中TABLEIMPORT的使用情况。
  2. 解释查询意图的评论总和。
  3. 变量/别名命名非常差。
  4. 我怀疑第一个查询的问题在于:

    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())
    

    我认为上面的代码应该返回每个&#34;更大区域的季度结果&#34; [SelectRegion]基于月度数据(data)。如果是这种情况,则需要删除DISTINCT并添加GROUP BY

    我认为从所有其他查询中删除DISTINCT以便能够发现其他隐藏问题也很有帮助。