第一篇文章在這裏,你們都非常幫助我。來自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
我可能會想念它,但我不知道你的問題是什麼。 – 2014-10-18 03:17:24
我以爲我說它很clealey,數據透視表分組 – timber379 2014-10-18 03:40:58