Solved

Dynamic Pivot Table woes

Posted on 2014-12-23
10
160 Views
Last Modified: 2014-12-26
Please excuse this newbie's ignorance...
I've looked at several examples and still can't wrap my head around Pivot tables.

I created a view:
SELECT        dbo.tblPMIInspection.PMIInspID, dbo.tblElement.Element, dbo.tblPMIReadings.Reading
FROM            dbo.tblPMIInspection INNER JOIN
                         dbo.tblPMIReadings ON dbo.tblPMIInspection.PMIInspID = dbo.tblPMIReadings.PMIInspID INNER JOIN
                         dbo.tblElement ON dbo.tblPMIReadings.ElementID = dbo.tblElement.ElementID

Open in new window


Element to be the column headings, Reading to be column result, PMIInspID is the Group By column

The column names NEED to be dynamic due to possible increase in number of elements being read etc.

How do I get from
SELECT        PMIInspID, Element, Reading
FROM            vwPMIReadings

Open in new window

to...
Pivot Table Results
0
Comment
Question by:Kurt Bergman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40516156
Dynamic PivotTables are hard, and if you are a newbie, it's possibly too hard.

Have a read of this article, and once your brain has exploded, if you have any questions, please post them (though I'm taking Christmas off): http://www.experts-exchange.com/Database/MS-SQL-Server/SQL-Server-2005/A_653-Dynamic-Pivot-Procedure-for-SQL-Server.html
0
 
LVL 18

Expert Comment

by:Simon
ID: 40516268
Philip's suggested link is a good one, or Google for "TSQL pivot dynamic column names" for other simple examples.
If you can post a sample dataset from your view (with no commercially sensitive data in it) we'll be able to provide a worked example for your data.
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40516333
Well, the number of elements is not dynamic..

Please use table alias names, this increases readability:

SELECT	I.PMIInspID, 
		E.Element, 
		R.Reading
FROM	dbo.tblPMIInspection I
	INNER JOIN dbo.tblPMIReadings R ON I.PMIInspID = R.PMIInspID 
	INNER JOIN dbo.tblElement E ON R.ElementID = E.ElementID;

Open in new window


This should work:

 
DECLARE @ColumList NVARCHAR(MAX) = N'';
DECLARE @SqlStatement NVARCHAR(MAX) = N'
	SELECT 	*
	FROM	vwPMIReadings
	PIVOT	( SUM(Reading) FOR Element IN ( @ColumnList ) ) P;
';

SELECT	@ColumnList = @ColumnList + ', ' + Element
FROM	vwPMIReadings
GROUP BY Element;

SET @SqlStatement = REPLACE(@SqlStatement, '@ColumnList', STUFF(@ColumnList, 1, 2, ''));

EXECUTE (@SqlStatement);

Open in new window

0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 18

Accepted Solution

by:
Simon earned 500 total points
ID: 40516359
@Ste5an:  Good thinking :)
one typo in line 1 of  your code:
DECLARE @ColumnList NVARCHAR(MAX) = N''; -- was missing 'n' in the variable name
0
 

Author Comment

by:Kurt Bergman
ID: 40516560
I'm on vacation until Friday I'll get back to you as soon as possible
0
 

Author Closing Comment

by:Kurt Bergman
ID: 40518610
Simon,
Thank you very much!

The Elements are dynamic because number and names will change depending on the site where the software is deployed.

Also,
There is only one 'Reading' for each Element for a given Inspection (InspID). Thus the need to use MAX (or Min) instead of SUM

SELECT       *
      FROM      vwPMIReadings
      PIVOT      ( max(Reading) FOR Element IN ( @ColumnList ) ) P;
      ';
0
 
LVL 18

Expert Comment

by:Simon
ID: 40518801
Hi ConcordCA - It really should be Ste5an getting most or all of the points here. I only pointed out a minor typo.
0
 

Author Comment

by:Kurt Bergman
ID: 40518824
Oops... I meant for it to be Ste5an
Is there a way to change it?
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40519016
I don't care that much..

Just use alias names in your queries for better readable code and I'm happy :)
0
 

Author Comment

by:Kurt Bergman
ID: 40519034
Epilogue:
This is being displayed in an Access datasheet subform.
I'm looping through the Field names in the view I created to set the Form.Control.ControlSource.  From there I can using the results from the SQL statement you provided to set the Form.Recordset.

It's working great. Thank you again!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question