Solved

Double query to make a view?

Posted on 2012-03-30
3
210 Views
Last Modified: 2012-03-30
I made a query that pivots a table using names from another table.  I asked the IT folks to make it into a view because there's a need to be able to query the pivoted result.  The query, which works fine, is below.  The response was that it couldn't be made into a view because it's actually 2 queries.  Is this true?  Is there a way it could be redone so that it could be used to make a view?

DECLARE @NPDColumns   VARCHAR (8000)

SELECT @NPDColumns =
          COALESCE (@NPDColumns + ',[' + cast (Description AS VARCHAR) + ']',
                    '[' + cast (Description AS VARCHAR) + ']')
  FROM [RKBBackEnd].[dbo].[lkpNPDBatch]
GROUP BY Description

DECLARE @query   VARCHAR (8000)
SET @query = '

DECLARE @NPD TABLE
             (  BatchCode     NVARCHAR (20),
                Description   NVARCHAR (20),
                Actual        FLOAT
             )

INSERT INTO @NPD (BatchCode, Description, Actual)
   SELECT a.BatchCode, b.Description, a.Actual
     FROM    [RKBBackEnd].[dbo].[tblNPDBatch] AS a
          INNER JOIN
             [RKBBackEnd].[dbo].[lkpNPDBatch] AS b
          ON a.BatchAttributeID = b.BatchAttributeID
    --WHERE BatchCode LIKE '+ CHAR(39) + '201203%' + CHAR(39)+ '

SELECT c.BatchAcceptDateTime, d.*
  FROM    (SELECT BatchCode, BatchAcceptDateTime
             FROM [RKBBackEnd].[dbo].[tblBatchInfo]) AS c
       JOIN
          (SELECT *
             FROM @NPD PIVOT (SUM (Actual)
                       FOR Description
                       IN ('
                  + @NPDColumns
                  + ')) AS P) AS d
       ON c.BatchCode = d.BatchCode
ORDER BY BatchCode'
EXECUTE (@query)

Open in new window

0
Comment
Question by:ChiMudGuy
[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
3 Comments
 
LVL 27

Accepted Solution

by:
tliotta earned 250 total points
ID: 37789834
I'm not aware of any reasonable way to arrive at a VIEW through that. It doesn't seem as if a VIEW is appropriate.

I'd suggest going back to IT and asking for objects that give you the results that you need instead of asking for a specific kind of object. You might get a procedure back or a similar object. But it likely won't be a VIEW.

Tom
0
 
LVL 22

Assisted Solution

by:Thomasian
Thomasian earned 250 total points
ID: 37789954
>> Is there a way it could be redone so that it could be used to make a view?

No, it is not possible to have dynamic column names in a view.
0
 

Author Closing Comment

by:ChiMudGuy
ID: 37790006
Ok, thanks.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

688 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