Do not use on any
shared computer
August 29, 2008 08:12pm pdt
 
[x]
Attachment Details

SQL Server 2000: dynamic SQL to build crosstab matrix

Tags: sql, server, 2000, dynamic, crosstab
Fellow Experts,

I need a dynamic SQL statement to help me build a crosstab matrix in SQL Server 2000.

My firm's software product allows me to store any number of attributes and custom fields
for client work centers.  I am trying to build a sproc that will allow me to see a matrix of
attribute/custom field values.

So far, my sproc is able to create a temp table like this:

WC_Code           Attribute      Value
00002           Branch Type      Traditional
00002           Driveup Type      Back to Back
00002           Is Branch      Yes
00002           State                     OH
00002           Status                     Open
00004           Branch Type      In-Store
00004           Driveup Type      Other
00004           Is Branch      Yes
00004           State                      TX
00004           Status                     Closed

My sproc has these stored in a temp table #tmp.

I want the final output to generate one row per unique WC_Code, with columns for WC_Code
plus an additional column for each unique Attribute.  The column values would then be the
Value associated with that attribute for that work center.  Following the above example,
the results would be:


WC_Code        Branch Type      Driveup Type      Is Branch          State          Status
00002                      Traditional      Back to Back      Yes          OH          Open
00004                      In-Store                      Other                      Yes          TX          Closed


If I knew ahead of time what the attributes are, I could do this:

SELECT DISTINCT t1.WC_Code,
    (SELECT t2.Value FROM #tmp t2 WHERE t2.WC_Code = t1.WC_Code AND t2.Attribute = 'Branch Type') AS [Branch Type],
    (SELECT t2.Value FROM #tmp t2 WHERE t2.WC_Code = t1.WC_Code AND t2.Attribute = 'Driveup Type') AS [Driveup Type],
    (SELECT t2.Value FROM #tmp t2 WHERE t2.WC_Code = t1.WC_Code AND t2.Attribute = 'Is Branch') AS [Is Branch],
    (SELECT t2.Value FROM #tmp t2 WHERE t2.WC_Code = t1.WC_Code AND t2.Attribute = 'State') AS [State],
    (SELECT t2.Value FROM #tmp t2 WHERE t2.WC_Code = t1.WC_Code AND t2.Attribute = 'Status') AS [Status]
FROM #tmp t1
ORDER BY t1.WC_Code


The trouble is, of course, that I will not know how many attributes there will be ahead of time, nor will I
know their names necessarily, thus I need to build the SQL dynamically.

The sproc code so far:


ALTER PROCEDURE dbo.GrabWorkCenterAttribs
      @model_id uniqueidentifier
AS
      /* grab work center group values */
      SELECT w.work_center_code AS WC_Code, v.work_center_group_name AS Attribute,
            v.work_center_group_value AS Value
      INTO #tmp
      FROM dbo.work_center w INNER JOIN
            dbo.work_center_group_value v ON w.work_center_id = v.work_center_id
      WHERE w.model_id = @model_id

      /* grab custom field values */
      INSERT INTO #tmp (WC_Code, Attribute, Value)
      SELECT w.work_center_code, c.attribute_name, c.attribute_value
      FROM dbo.work_center w INNER JOIN
            dbo.custom_fields c ON w.work_center_id = c.context_id
      WHERE c.context_type = 'Work Center' AND w.model_id = @model_id

GO



Regards,

Patrick
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Database
Question Asked By: matthewspatrick
Solution Provided By: dqmq
Participating Experts: 5
Solution Grade: A
Views: 14
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by harfang

Rank: Guru

Expert Comment by harfang:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by matthewspatrick
Author Comment by matthewspatrick:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by puppydogbuddy
Expert Comment by puppydogbuddy:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by nmcdermaid

Rank: Genius

Expert Comment by nmcdermaid:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by dqmq

Rank: Sage

Expert Comment by dqmq:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by dqmq

Rank: Sage

Accepted Solution by dqmq:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by dbbishop

Rank: Wizard

Expert Comment by dbbishop:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Assisted Solution by dbbishop

Rank: Wizard

Assisted Solution by dbbishop:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by dbbishop

Rank: Wizard

Expert Comment by dbbishop:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by dbbishop

Rank: Wizard

Expert Comment by dbbishop:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by matthewspatrick
Author Comment by matthewspatrick:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by dbbishop

Rank: Wizard

Expert Comment by dbbishop:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by matthewspatrick
Author Comment by matthewspatrick:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by dqmq

Rank: Sage

Expert Comment by dqmq:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by dbbishop

Rank: Wizard

Expert Comment by dbbishop:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by matthewspatrick
Author Comment by matthewspatrick:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by nmcdermaid

Rank: Genius

Expert Comment by nmcdermaid:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Assisted Solution by dbbishop

Rank: Wizard

Assisted Solution by dbbishop:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by dqmq

Rank: Sage

Expert Comment by dqmq:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Open Discussion
Open Discussion
 
Comment by matthewspatrick
Dennis and Doug,

Thank you gents!  I feel very silly now for missing those parentheses :)

nmcdermaid: for now, it is most likely that my client's DBA will run that for them from time
to time.  Eventually, I expect that we will be extending the functionality of our application
to allow for the ability for designated application end users to run it themselves.

Regards,

Patrick
 
 
20080723-EE-VQP-34 / EE_QW_1_20070628