itrain-home BASTA! Spring 2012  
home
 Aktuelle Seite:  knowhow sql tsql pivot pivotsample.asp 
 



 

Cross-tab Queries with Transact-SQL

How to use sp_transform

The sample procedure sp_transform dynamically generates a Transact SQL Query that returns a resultset similar to the Cross-Tab Queries in Access or Excel.

Here is a short example (using the database Northwind), that shows how to use this stored procedure

Prerequisites:

Before you can run this example create the stored procedure sp_transform either in your master-database or locally in your Northwind database. This demo will run with Microsoft SQL Server Version 7.0 und Version 2000

Examples

The examples use a crosstab-query to show the total and average amount of freight costs per year by Employee.

In the first step we create a view, that combines the information from the orders and the employees table using an INNER JOIN.

  
   
 CREATE VIEW vFreight AS  
  SELECT lastname,   
                Year(orderdate) as theYear ,   
                freight   
         FROM orders o   
         INNER JOIN employees e  
         ON o.employeeid = e.employeeid  
   
 GO  
 

The result of this view will look similar to the following list:

lastnametheYearfreight
Buchanan199632.38
Suyama199611.61
Peacock199665.83
Leverling199641.34
Peacock199651.30
Leverling199658.17
Peacock199655.09
Peacock19963.05
Callahan199648.29
Dodsworth1996146.06
Suyama19963.67
Fuller199655.28
Leverling199625.73
.........
.........

The Cross-Tab Query (using sp_transform)

Using this view as a starting point, we will now generate a resultset that shows the Total freight cost per employee per year.

First example

In the first example we will use lastname as the row-header and theYear as the column-header, freight is the value field and 'SUM' the aggregate function:

EXEC sp_transform  
  @TableOrView_name = 'vFreight',   
  @Aggregate_Column = 'freight',   
  @Select_Column    = 'lastname',  -- row header  
  @pivot_column     = 'theYear'    -- column header

The result:

lastname199619971998Total
Buchanan1365.37001244.53001308.81003918.7100
Callahan1326.28002898.62003262.98007487.8800
Davolio1967.48004488.03002381.13008836.6400
Dodsworth532.84001046.09001747.33003326.2600
Fuller1129.15003690.30003876.96008696.4100
King664.32003245.53002755.59006665.4400
Leverling880.03006918.34003086.370010884.7400
Peacock2042.38006574.81002728.950011346.1400
Suyama766.10002114.1700900.20003780.4700
Total10673.950032220.420022048.320064942.6900

The second example just shows what the result looks like when you swap the row-header and column-header fields.

EXEC sp_transform  
         @TableOrView_name = 'vFreight',   
         @Aggregate_Column = 'freight',   
         @Select_Column    = 'theyear', -- row header  
         @pivot_column     = 'lastname' -- column header

The result:

theyearBuchananCallahanDavolioDodsworthFullerKingLeverlingPeacockSuyamaTotal
19961365.37001326.28001967.4800532.84001129.1500664.3200880.03002042.3800766.100010673.9500
19971244.53002898.62004488.03001046.09003690.30003245.53006918.34006574.81002114.170032220.4200
19981308.81003262.98002381.13001747.33003876.96002755.59003086.37002728.9500900.200022048.3200
Total3918.71007487.88008836.64003326.26008696.41006665.440010884.740011346.14003780.470064942.6900

The next example shows the same Query using the AVG-function

EXEC sp_transform  
         @Aggregate_Function = 'AVG',   
         @TableOrView_name   = 'vFreight',   
         @Aggregate_Column   = 'freight',   
         @Select_Column      = 'lastname',  -- row header  
         @pivot_column       = 'theYear'    -- column header

The result:

lastname199619971998Total
Buchanan32.508829.631631.162193.3026
Callahan12.752627.871331.374871.9988
Davolio15.995736.488019.358771.8426
Dodsworth12.391624.327640.635577.3548
Fuller11.761938.440640.385090.5876
King9.226645.076838.272092.5755
Leverling6.929354.475124.302185.7066
Peacock13.092142.146217.493272.7316
Suyama11.434331.554713.435856.4249
Total12.860138.819726.564278.2442

Leerraum

Dokument zum Drucken anzeigen
English Pages
Link-Tipp zum Thema "Microsoft SQL Server": www.vbxml.com/conference/wrox/2000_vegas/html/content/briank_dts.asp

Umfangreiche Site zu den Themen XML, SQL, ASP.Net etc.