Solved

DB2: V5R4M0: Create view and WITH statment

Posted on 2011-03-09
8
781 Views
Last Modified: 2012-05-11
Is it possible use WITH statment in a create view statment?
because the code below doesn't work with WITH...

thanks

CREATE VIEW QS36F/MATERIAL1 AS                                  
( WITH AAA AS ( SELECT HVARI2 AS MATERIAL                        
 FROM QS36F/DDSH001                                              
WHERE SUBSTRING(HVARI2, 1, 3) = '   ' AND TRIM(HVARI2)<>'' AND    
HVARI1                                                            
IN (SELECT GRoUP1 FROM QS36F/SGRoup) GROUP BY HVARI2 )          
SELECT AAA.*, MATDES.MATDES FROM AAA LEFT JOIN QS36F/MATDES ON    
MATERIAL=MATERI                                                  
)                                                                
0
Comment
Question by:bobdylan75
[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
  • 2
  • 2
  • +1
8 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 35083943
Hi Bob,

I'm not sure, but if it is you'll need to turn it around...


with AAA as (...)
CREATE VIEW myview ...


Kent
0
 

Author Comment

by:bobdylan75
ID: 35083983
thank you but, it doesn't work in this way...
I tried 1 minute ago...
0
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 35084042
why not

CREATE VIEW QS36F/MATERIAL1 AS                                  
(
           
SELECT AAA.*, MATDES.MATDES FROM
( SELECT HVARI2 AS MATERIAL                        
 FROM QS36F/DDSH001                                              
WHERE SUBSTRING(HVARI2, 1, 3) = '   ' AND TRIM(HVARI2)<>'' AND    
HVARI1 IN (SELECT GRoUP1 FROM QS36F/SGRoup) GROUP BY HVARI2 ) AAA
 LEFT JOIN QS36F/MATDES ON    
MATERIAL=MATERI                                                  
)            
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 18

Expert Comment

by:daveslash
ID: 35084046

Try removing the out parentheses.

e.g.

CREATE VIEW QS36F/MATERIAL1 AS                                   
  WITH AAA AS ( 
    SELECT HVARI2 AS MATERIAL
      FROM QS36F/DDSH001
     WHERE SUBSTRING(HVARI2, 1, 3) = '   '
       AND TRIM(HVARI2)<>'' 
       AND HVARI1 IN (SELECT GRoUP1 FROM QS36F/SGRoup) 
     GROUP BY HVARI2
  )
  SELECT AAA.*, 
         MATDES.MATDES 
    FROM AAA 
    LEFT JOIN QS36F/MATDES
      ON MATERIAL=MATERI

Open in new window

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 35084060
Ah... You're trying to put the CTE into the view.  (I thought that you were trying to define the columns from the CTE.)

I don't believe that this is supported.  You can still build a view over the joined tables but not with the CTE syntax.

CREATE VIEW QS36F/MATERIAL1 AS                                  
SELECT AAA.*, MATDES.MATDES
FROM
(
  SELECT HVARI2 AS MATERIAL                        
  FROM QS36F/DDSH001                                              
  WHERE SUBSTRING(HVARI2, 1, 3) = '   '
    AND TRIM(HVARI2)<>''
    AND HVARI1 IN (SELECT GRoUP1 FROM QS36F/SGRoup)
  GROUP BY HVARI2
) AAA
LEFT JOIN QS36F/MATDES
  ON MATERIAL=MATERI                                                  
 


Kent
0
 
LVL 18

Expert Comment

by:daveslash
ID: 35084081
Putting a CTE in a view works beautifully for me:

create view delthis as  
  with ndm as (
    select *
      from member
     where deleteRequest = 'N'
  )
  select *
    from ndm

Open in new window

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 35084138
Wow!

I learned something again today.  I had no idea that the iSeries had moved that far into the present.  :)

Thanks, Dave!


Kent
0
 

Author Closing Comment

by:bobdylan75
ID: 35084230
THANK YOU EVERYBODY!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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