代碼:Pivot.zip
數(shù)據(jù)透視表提供的數(shù)據(jù)三維視圖效果,在Microsoft Excel能創(chuàng)建數(shù)據(jù)透視表,但是,它并不會總是很方便使用Excel。您可能希望在Web應(yīng)用程序中創(chuàng)建一個數(shù)據(jù)透視報表。創(chuàng)建一個簡單的數(shù)據(jù)透視表可能是一件非常復(fù)雜的任務(wù)。所以,我打算不但為你提供一個非常有用的工具創(chuàng)建簡單和高級的數(shù)據(jù)透視表,而且為你移除一些籠罩他們的神秘面紗。
目標是:我們想要有能力將datatable中的二維的數(shù)據(jù)轉(zhuǎn)換成三維視圖。
在大多數(shù)情況下,你會從數(shù)據(jù)庫的查詢數(shù)據(jù)填充數(shù)據(jù)表,例如
SELECT SalesPeople.FullName AS [Sales Person] , Products.FullName AS [Product] , SUM(Sales.SalesAmount) AS [Sale Amount] , SUM(Sales.Qty) AS [Quantity] FROM Sales JOIN SalesPeople WITH (NOLOCK) ON SalesPeople.SalesPersonID = Sales.SalesPersonID JOIN Products WITH (NOLOCK) ON Products.ProductCode = Sales.ProductCode GROUP BY SalesPeople.FullName , Products.FullName
該查詢會產(chǎn)生下面的數(shù)據(jù)表:
Sales Person |
Product |
Quantity |
Sale Amount |
John |
Pens |
200 |
350 |
John |
Pencils |
400 |
500 |
John |
Notebooks |
100 |
300 |
John |
Rulers |
50 |
100 |
John |
Calculators |
120 |
1200 |
John |
Back Packs |
75 |
1500 |
Jane |
Pens |
225 |
393.75 |
Jane |
Pencils |
335 |
418.75 |
Jane |
Notebooks |
200 |
600 |
Jane |
Rulers |
75 |
150 |
Jane |
Calculators |
80 |
800 |
Jane |
Back Packs |
97 |
1940 |
Sally |
Pens |
202 |
353.5 |
Sally |
Pencils |
303 |
378.75 |
Sally |
Notebooks |
198 |
600 |
Sally |
Rulers |
98 |
594 |
Sally |
Calculators |
80 |
800 |
Sally |
Back Packs |
101 |
2020 |
Sarah |
Pens |
112 |
196 |
Sarah |
Pencils |
245 |
306.25 |
Sarah |
Notebooks |
198 |
594 |
Sarah |
Rulers |
50 |
100 |
Sarah |
Calculators |
66 |
660 |
Sarah |
Back Packs |
50 |
2020 |
正如你所看到的,這是一個二維表,它不是一個非常有用的報表。因此,我們得改變,將它變成更可讀的數(shù)據(jù)表。
出處:朱祁林
責任編輯:bluehearts
上一頁 下一頁 ASP.NET實現(xiàn)類似Excel的數(shù)據(jù)透視表 [2]
◎進入論壇網(wǎng)絡(luò)編程版塊參加討論
|