Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Double query to make a view?

Posted on 2012-03-30
3
Medium Priority
?
214 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 1000 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 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

636 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