Important alert: (current site time 7/15/2013 8:06:37 PM EDT)
 

VB icon

Pivot Data based on Unknown Field Items

Email
Submitted on: 7/9/2001 9:26:15 PM
By: James Travis  
Level: Advanced
User Rating: By 6 Users
Compatibility: SQL Server 7.0
Views: 20135
author picture
(About the author)
 
     I was posed a question to pivot data on a column without knowing exactly what values exist.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Pivot Data based on Unknown Field Items
-- Description:I was posed a question to pivot data on a column without knowing exactly what values exist.
-- By: James Travis
--
-- Inputs:You need to change so fields but they are noted by name of what they do.
--
-- Returns:A pivot table based on values you did not have to know.
--
-- Assumes:There is a downside to this in that the query so far can be only 8000 characters at most I am looking at other was to increase this by using multiple variables then combine in execute but have not gotten to test yet.
--
-- Side Effects:Depending on the number of unknowns it will crash if it is larger than 8000 characters of data. Also as this uses a cursor it can be slow so test it before you use in production. You may just want to use to build the query once a day and output to someplace it can be used from.
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=322&lngWId=5--for details.--**************************************

DECLARE @SQLState VARCHAR(8000) --This is where your sql string will be built
DECLARE @FieldPivotBasedOn VARCHAR(100) --This will hold each value when we pull from cursor
/* No comma's here as we may have no products and we want each new output to 
* add its own , so we do not have to cut the last character off if when we loop thru.
*/
Set @SQLState = 'SELECT FieldColumnsAreFrom'
Print (@SQLState)
/* We are getting all the possible values for Product elimating duplicates. */
DECLARE cur_Cases CURSOR FOR SELECT DISTINCT FieldPivotBasedOn FROM tblUse 
OPEN cur_Cases --Open the cursor
/* Get the next value from the cursor and put in variable. */
FETCH NEXT FROM cur_Cases INTO @FieldPivotBasedOn
WHILE @@FETCH_STATUS = 0 --As Long as we got data keep going.
BEGIN
/* Each time thru we will add another product as a possiblity for this pivot. */
SET @SQLState = @SQLState + ', SUM(CASE FieldPivotBasedOn WHEN ''' + @FieldPivotBasedOn + ''' THEN ValueIfCase ELSE ValueIfNotCase END) as [' + @FieldPivotBasedOn + ']'
/* Get the next value from the cursor and put in variable. */
FETCH NEXT FROM cur_Cases INTO @FieldPivotBasedOn
END 
CLOSE cur_Cases --We no longer need cursor so close
DEALLOCATE cur_Cases --and free memory
SET @SQLState = @SQLState + ' FROM tblUse GROUP BY FieldColumnsAreFrom'
--Print (@SQLState) --This line is commented out, just uncomment to output the query this built for debugging.
EXEC (@SQLState) --This line will execute the sql statement we built in @SQLState, add -- to front to comment out.


Other 20 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/16/2006 3:22:44 AMPerke

For this work site admin should predict few stars more ...
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.