简体   繁体   English

来自MS查询计算和分组问题的Excel 2010数据透视表

[英]Excel 2010 Pivot Table from MS Query Calculation and Grouping Issue

First post here, you all have helped me so much. 在这里的第一篇文章,你们都对我有很大帮助。

My issue is that I am building a Sales Board in Excel 2010 using ODBC connection to a SQL Server 2008 R2 database. 我的问题是,我正在使用与SQL Server 2008 R2数据库的ODBC连接在Excel 2010中构建销售委员会。 I have two row groups and two column groups. 我有两个行组和两个列组。 Groups are in parent child order. 组按父子顺序排列。

Row groups are Product Grade and Product. 行组是产品等级和产品。 Column groups are Date and Region(East and West). 列组是日期和地区(东和西)。 Value is product price by region. 价值是按地区划分的产品价格。 I'm trying to add a subtotal after each region group which gives (East price - West price). 我正在尝试在每个区域组之后添加小计,以给出(东价格-西价格)。 Sometimes only one region is reported so if it is West the subtotal shouldn't exist. 有时只报告一个地区,因此如果是西部,则小计不应该存在。

I've tried a combination of calculated items, which almost works but adds a column for each region. 我已经尝试过计算项目的组合,这几乎可以工作,但是为每个区域添加一列。 Or doing the work in SQL Server which almost works except the Product Grade grouping spreads all products in every parent group. 或在SQL Server中执行几乎可以工作的工作,但“产品等级”分组会将所有产品分散到每个父组中。 I'd prefer to make this work in SQL Server for obvious reasons and I am new to building data in Excel. 出于明显的原因,我更希望在SQL Server中进行此工作,而我是在Excel中构建数据的新手。 I'm sure it is something very simple I'm missing. 我确定这是我所缺少的非常简单的东西。 Any help would be much appreciated. 任何帮助将非常感激。

;with t as 
(  
   select  
      info.protype,  
      info.product,  
      case when info.pricecode = 'x' then 'East' else 'West' end as Region, 
      SUM(isnull(info.price,0)) as Price,
      case when r1.product=r2.product then SUM(x.price-y.price) else 0 end as Diff,
      info.effectdate 
   from   
      (select 
          protype, product, pricecode, price, effectdate    
      from 
          prc_table     
      where 
          pricecode in ('x','y')    
          and protype = 'z') as info  
   left join 
      prc_table r1 on info.protype = r1.protype 
                   and info.product = r1.product 
                   and info.effectdate = r1.effectdate  
                   and r1.pricecode = 'x'  
   left join 
      prc_table r2 on info.protype = r2.protype 
                   and info.product = r2.product 
                   and info.effectdate = r2.effectdate  
                   and r2.pricecode = 'y'      
   where 
      info.effectdate >= DATEADD(MM, -3, GETDATE()) 
      and info.effectdate <= GETDATE()  
   group by     
      info.effectdate, info.protype, info.product,
      r1.product, r2.product, info.pricecode, r1.price, r2.price
)          
select   
   c.codedesc as [Grade],   
   r.product as [Product],  
   r.Region as [Region],  
   r.Price as [Price],
   r.Diff as [E-W],
   r.effectdate as [Date]
from 
   t r 
inner join 
   pro_item i on r.protype = i.protype and r.product = i.product   
inner join 
   pro_duct p on i.protype = p.protype and i.product = p.product and i.1 = p.1
(product grade join)
inner join 
   xxx_codes c on p.desc3 = c.code and c.prefix = 'xxx'    
where   
    i.protype = 'z'    
    and i.loc = 'loc'    
    and p.desc4 = ''    
    and i.branch = 'm'  
order by 
    r.effectdate desc, codedesc, product

I get what you're trying to do. 我明白你的意思。 And you're on the right track to calculate the differences in your sql, because it's not possible to show/hide the subtotals in the pivot table depending on whether it's east/west or just east. 而且您正在正确计算sql中的差异,因为无法显示/隐藏数据透视表中的小计,具体取决于它是东西方还是仅东西方。 So doing it in sql is good. 因此,在sql中这样做是好的。 Where I think you have a bit of a design flaw in your query is trying to put the difference in a separate column. 我认为查询中存在一些设计缺陷的地方是尝试将差异放在单独的列中。

The problem with that is, how do you present it in your pivot table? 问题是,如何在数据透视表中显示它? If you have both Price and Diff as your value field in your pivot table, it will botch the layout completely. 如果您在数据透视表中同时使用Price和Diff作为值字段,它将完全破坏布局。 You want only one value field - Price. 您只需要一个值字段-价格。 And you want East, West, AND Diff to be your column headings, so they should all be Regions in your data. 而且您希望将East,West和Diff用作列标题,因此它们都应该是数据中的Regions。

So, in your sql, get the grade, product, region, price, and date like you're doing, put it in a temp table, then INSERT INTO that temp table the difference records, with region='Diff' and Price= the actual difference value, where both an east and a west record exist for that product and effective date. 因此,在您的sql中,像执行操作一样获取等级,产品,地区,价格和日期,将其放入临时表中,然后插入该临时表的差异记录中,并使用region ='Diff'和Price =实际差值,该产品和生效日期同时存在东方记录和西方记录。

Your output should look something like this: 您的输出应如下所示:

Grade   Product Region  Price   Effdt                               
A       P1      East    1.5     31-Oct-14                                   
A       P2      East    3       31-Oct-14                                   
B       P3      East    5       31-Oct-14                                   
B       P4      East    2.44    31-Oct-14                                   
C       P5      East    3.67    31-Oct-14                                   
C       P6      East    10.3    31-Oct-14                                   
B       P3      West    5.5     31-Oct-14                                   
B       P4      West    2.7     31-Oct-14                                   
C       P5      West    3.8     31-Oct-14                                   
C       P6      West    10.7    31-Oct-14                                   
A       P1      East    1.5     30-Nov-14                                   
A       P2      East    3       30-Nov-14                                   
B       P3      East    5       30-Nov-14                                   
B       P4      East    2.44    30-Nov-14                                   
C       P5      East    3.67    30-Nov-14                                   
C       P6      East    10.3    30-Nov-14                                   
B       P3      Diff    -0.5    31-Oct-14                                   
B       P4      Diff    -0.26   31-Oct-14                                   
C       P5      Diff    -0.13   31-Oct-14                                   
C       P6      Diff    -0.4    31-Oct-14   

You only insert difference rows where applicable, so when you pivottable this data, it will show either east+west+diff or just east but not east+diff. 您仅在适用的情况下插入差异行,因此,当您对数据进行数据透视化时,它将显示east + west + diff或仅显示east而不显示east + diff。

It looks like you're pretty comfortable with sql to write the above query so you should be able to implement that. 看起来您对sql很满意,可以编写上述查询,因此您应该能够实现该查询。 If you are having trouble with it post where you get to and i'll have a look. 如果您在使用它时遇到麻烦,请前往,我来看一下。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM