分组设置可能会丢弃一组

时间:2018-03-06 16:52:47

标签: sql sql-server

我有一个包含以下列的表格:

 manufact nameplat model modelyea newseg newsubseg region sales
bmw        bmw    230I   2016     x      y         west   13
bmw        bmw    230I   2017     x      q         west   20
bmw        bmw    230I   2018     x      r         south  12

我需要在具有不同连接条件的许多客户端的所有级别上执行聚合。目前,我有一个动态SQL,它分别为每个级别进行聚合,构建group by并动态连接,因为这里有很多其他因素。这个过程很慢,并且运行了几个小时。

我正在寻找分组集来做同样的事情。虽然它肯定更快,但它并没有聚合一些集合。

问题是查询没有打印出该集的结果:

(period,newseg,modelyea,map_region.region)

grouping_id(period,newseg,modelyea,map_region.region)有一个值。

知道我可能做错了吗?

select period
,case  when GROUPING_ID(period,manufact,nameplat,dma.model,newsegment,newsubseg,modelyea,map_region.region) > 0 AND ( GROUPING_ID(period,manufact,nameplat,newsegment,newsubseg,modelyea,map_region.Region) = 32 OR GROUPING_ID(period,manufact,nameplat,newsegment,newsubseg,modelyea,map_region.Region) = 34) then 'Nameplate' 
when GROUPING_ID(period,manufact,nameplat,dma.model,newsegment,newsubseg,modelyea,map_region.region) = 0 OR GROUPING_ID(period,manufact,nameplat,dma.model,newsegment,newsubseg,modelyea,map_region.region) = 2 then 'Model'
when (GROUPING_ID(period,manufact,nameplat,dma.model,newsegment,newsubseg,modelyea,map_region.region) > 0 AND GROUPING_ID(period,manufact,nameplat,dma.model,newsegment,newsubseg,modelyea,map_region.region) > 0 AND GROUPING_ID(period,manufact,modelyea,map_region.region) = 0 ) OR (GROUPING_ID(period,manufact,nameplat,dma.model,newsegment,newsubseg,modelyea,map_region.region) > 0 AND GROUPING_ID(period,manufact,nameplat,dma.model,newsegment,newsubseg,modelyea,map_region.region) > 0 AND GROUPING_ID(period,manufact,modelyea,map_region.region) > 0 ) then 'Manufacturer' 
else 'N/A' end as Granularity 
,isnull(manufact,'N/A') as manufact
,isnull(nameplat,'N/A') as nameplat
,isnull(dma.model,'N/A') as model
,isnull(dma.newsegment,'N/A') as seg1
,isnull(dma.newsubseg,'N/A') as seg2
,isnull(modelyea,'All') as modelyea
,map_region.region
,GROUPING_ID(period,manufact,nameplat,dma.model,newsegment,newsubseg,modelyea,map_region.region) as Grouping_ID_Model
,GROUPING_ID(period,manufact,nameplat,newseg,newsubseg,modelyea,map_region.Region) as GID_Nameplate
,GROUPING_ID(period,manufact,modelyea,map_region.region) as GID_Manu
,GROUPING_ID(period,newseg,modelyea,map_region.region) as GID_Seg
,sum(sales) as sales
from dma
join region_map map_region ON dma.dma = map_region.dma
where monthofs= 201801
and dma.dma = map_region.dma
and dma.model = '230I'
group by grouping sets (
(period,manufact,nameplat,dma.model,newsegment,newsubseg,modelyea,map_region.region) --model,modelyea
,(period,manufact,nameplat,dma.model,newsegment,newsubseg,map_region.region) --model all MY
,(period,nameplat,superseg,newsegment,newsubseg,modelyea,map_region.region) --nameplat,modelyea
,(period,nameplat,superseg,newsegment,newsubseg,map_region.region) -- nameplat all MY
,(period,manufact,modelyea,map_region.region) --manufact modelyea
,(period,manufact,map_region.region) --manufact all my
,(period,newsegment,modelyea,map_region.region) --segment modelyea
)

1 个答案:

答案 0 :(得分:0)

SQL中唯一带有标识符“newseg”的地方位于'GID_Nameplate'和'GID_Seg'的grouping_ids中。这应该是'newsegment'吗?