To answer your questions -
Outputting directly to Excel worksheet (a function with DTS).
The table that I am doing the crosstab on is one that I'm building,
based generally on a Business Area Code (10, 11, 12, 15, 20, etc) and
the Channel in which it's sold under. Channel could vary from
month-to-month, as could the Business Area Code.
It's like:
BA ShipQty DC
10 50 DL
11 25 DL
12 50 CH
14 100 WF
Report would look like:
BA DL CH WF
10 50 0 0
11 25 0 0
12 0 50 0
14 0 0 100
I may have no DL shipments next month, but could have shipments in CH in
many business areas next month.
The SP I am using is as follows:
CREATE PROC sp_CrossTab
@table AS sysname, -- Table to crosstab
@onrows AS nvarchar(128), -- Grouping key values (on rows)
@onrowsalias AS sysname = NULL, -- Alias for grouping column
@oncols AS nvarchar(128), -- Destination columns (on columns)
@sumcol AS sysname = NULL -- Data cells
AS
--LISTING 4: Step 1 of the sp_CrossTab Stored Procedure: Beginning of
the SQL String
DECLARE
@sql AS varchar(8000),
@NEWLINE AS char(1)
SET @NEWLINE = CHAR(10)
-- step 1: beginning of SQL string
SET @sql =
'SELECT' + @NEWLINE +
' ' + @onrows +
CASE
WHEN @onrowsalias IS NOT NULL THEN ' AS ' + @onrowsalias
ELSE ''
END
--LISTING 5: Step 2 of the sp_CrossTab Stored Procedure: Storing Keys in
a Temp Table
CREATE TABLE #keys(keyvalue nvarchar(100) NOT NULL PRIMARY KEY)
DECLARE @keyssql AS varchar(1000)
SET @keyssql =
'INSERT INTO #keys ' +
'SELECT DISTINCT CAST(' + @oncols + ' AS nvarchar(100)) ' +
'FROM ' + @table
EXEC (@keyssql)
--LISTING 6: Step 3 of the sp_CrossTab Stored procedure: Middle Part of
SQL String
DECLARE @key AS nvarchar(100)
SELECT @key = MIN(keyvalue) FROM #keys
WHILE @key IS NOT NULL
BEGIN
SET @sql = @sql + ',' + @NEWLINE +
' SUM(CASE CAST(' + @oncols +
' AS nvarchar(100))' + @NEWLINE +
' WHEN N''' + @key +
''' THEN ' + CASE
WHEN @sumcol IS NULL THEN '1'
ELSE @sumcol
END + @NEWLINE +
' ELSE 0' + @NEWLINE +
' END) AS c' + @key
SELECT @key = MIN(keyvalue) FROM #keys
WHERE keyvalue > @key
END
--LISTING 7: Step 4 of the sp_CrossTab Stored Procedure: End of SQL String
SET @sql = @sql + @NEWLINE +
'FROM ' + @table + @NEWLINE +
'GROUP BY ' + @onrows + @NEWLINE +
'ORDER BY ' + @onrows
--PRINT @sql + @NEWLINE -- For debug
EXEC (@sql)
GO
And gives me the output I am looking for. I just need the column
headings for that month as well, since they are subject to change from
month to month. I may have DL absent from the report next month, and
may have 3 new ones not on the report this month. The columns would
always be shifting back & forth, and I either need a way to keep them
static & put the appropriate info in the appropriate place (and do this
without hardcoding them), or to have them dynamically show up & vanish
from month to month (while retaining the "key" to the columns).
The SP above is called by:
sp_crosstab 'sales_worktable',level_1_1,'BA',distr_channel,ship_val
where level_1_1 corresponds to the BA above, 'BA' is what the column is
called in the report, distr_channel is the column header, and ship_val
is what I am totalling up.
BC
[quoted text, click to view] JXStern wrote:
> On Mon, 15 Jan 2007 11:56:53 -0500, Blasting Cap
> <goober@christian.net> wrote:
>
>> I have a crosstab query that I have set up to return a variable number
>> of divisions (channels) in any given month.
>>
>> Since my intention is to set up a job to throw the results of this
>> crosstab query into an Excel sheet & mail it, I need to have matching
>> headers for the columns. However, they are subject to change from month
>> to month as different channels may be seasonal.
>>
>> How can I get the column headings to stay with the output of this
>> crosstab query, so that I can put it into a DTS job & forget about it?
>
> Interesting problem.
>
> You output as .csv?
>
> What is your data source, a table that might have different columns in
> any given run? An SP ditto?
>
> J.