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
Solved

DB2: V5R4M0: Create view and WITH statment

Posted on 2011-03-09
8
778 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

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 (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

792 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