?
Solved

Double query to make a view?

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

777 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