2017-07-09 375 views
0

喬擁有全球DVD租賃商店。他在全球不同地區有店面。每個月他都希望通過查找他的庫存百分比來確定他的商店中的設備是否是最新的,並且每個區域的「當前」,「需要更新」和「必須執行」等特定狀態。這有助於確保他的顧客在他的商店中獲得最佳體驗。在具有多個子類別的類別中獲得加權平均數

他的助手比爾想要建立一個SQL查詢,它會顯示喬在每個地區的設備百分比及其狀態。他面前的人使用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是可以正常使用的所有除北美地區,其中有一些稍微的百分比關閉。他認爲北美次區域的加權平均值可以解決這個問題,因爲一些分區域比其他分區域大,所以百分比正確。

比爾將如何計算北美每個地區的加權平均值?

編輯:

打破了不同地區

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

添加嘗試發佈 – programr

+0

@programr可以發佈表的模式嗎?樣本數據集和結果? –

回答

0

這個查詢看起來有點像狗的早餐。 存在的主要問題:

  1. SELECT DISTINCT不需要在GROUP BY查詢,值已經截然不同。 如果TABLEIMPORT中的數據沒有標準化,我可以理解COUNTSTOT查詢中DISTINCT的用法。
  2. 解釋查詢意圖的註釋的運氣總數。
  3. 變量/別名命名非常差。

我懷疑與第一查詢的問題是在這裏:

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

我想上面的代碼應該返回每個基於月度數據(data)「大區」 [SelectRegion]季度業績。如果是這種情況,那麼你需要刪除DISTINCT並添加GROUP BY

我認爲從所有其他查詢中刪除DISTINCT以便能夠發現其他隱藏問題也會有所幫助。

相關問題