2014-10-18 57 views
2

第一篇文章在這裏,你們都非常幫助我。來自MS查詢計算和分組的Excel 2010數據透視表問題

我的問題是我在Excel 2010中使用ODBC連接到SQL Server 2008 R2數據庫來構建銷售板。我有兩個行組和兩個列組。團體在父母的子女順序。

排組是產品等級和產品。列組是日期和區域(東部和西部)。價值是按地區劃分的產品價格。我試圖在每個地區組(東部價格 - 西部價格)之後添加一個小計。有時只有一個地區被報告,所以如果它是西方的話,小計就不應該存在。

我試過計算項目的組合,它幾乎可以工作,但爲每個區域添加了一列。或者在SQL Server中執行幾乎可以工作的工作,但產品等級分組將所有產品分散到每個父組中。我更願意在SQL Server中完成這項工作,原因很明顯,並且我是在Excel中構建數據的新手。我相信這是我很想念的東西。任何幫助將非常感激。

;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 
+0

我可能會想念它,但我不知道你的問題是什麼。 – 2014-10-18 03:17:24

+0

我以爲我說它很clealey,數據透視表分組 – timber379 2014-10-18 03:40:58

回答

0

我明白你想要做什麼。而且你正在計算你sql中的差異,因爲它不可能在數據透視表中顯示/隱藏小計,這取決於它是東/西還是東。所以在sql中這樣做很好。我認爲你的查詢中存在一些設計缺陷的地方是試圖將差異放在單獨的列中。

問題在於,你如何在數據透視表中顯示它?如果您的數據透視表中同時包含Price和Diff作爲您的值字段,則會完全打亂佈局。你只需要一個價值領域 - 價格。並且您希望東,西和差異成爲您的列標題,因此它們應該都是數據中的區域。

因此,在你的sql中,獲取你所做的等級,產品,區域,價格和日期,把它放在臨時表中,然後INSERT INTO該差異記錄的臨時表,區域='Diff '和Price =實際差異值,其中該產品和生效日期都存在東部和西部記錄。

你的輸出應該是這個樣子:

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 

您只需插入差排在適用情況下,所以當你的數據透視表這個數據,它會顯示無論是東+西+ DIFF或東邊而不是東+差異。

看起來你很喜歡用sql編寫上面的查詢,所以你應該能夠實現它。如果您遇到問題,請在您需要的地方找到,我會看看。

乾杯

+1

我得到它的工作。非常感謝。我之前沒有使用'Insert Into',很高興將它付諸實踐。這種方法現在對我很有幫助。對於有此問題的其他人,我會補充說,在我將它用於SQL之後,我將它變成了一個存儲過程,因爲Excel對它的運行非常挑剔。然後在Excel中調用該過程。再次感謝您的幫助和見解。完美的作品。 – timber379 2014-10-23 20:51:49

相關問題